refactor: remove many unwrap calls

This commit is contained in:
Nova
2023-01-25 11:50:53 -05:00
parent b5e87d5911
commit 529b86fa91
26 changed files with 163 additions and 133 deletions

View File

@@ -66,7 +66,7 @@ pub struct Client {
pub startup_settings: Option<StartupSettings>,
}
impl Client {
pub fn from_connection(connection: UnixStream) -> Arc<Self> {
pub fn from_connection(connection: UnixStream) -> Result<Arc<Self>> {
let pid = connection.peer_cred().ok().and_then(|c| c.pid());
let env = pid.and_then(|pid| get_env(pid).ok());
let exe = pid.and_then(|pid| fs::read_link(format!("/proc/{}/exe", pid)).ok());
@@ -98,15 +98,15 @@ impl Client {
startup_settings,
});
let _ = client.scenegraph.client.set(Arc::downgrade(&client));
let _ = client.root.set(Root::create(&client));
hmd::make_alias(&client);
spatial::create_interface(&client);
fields::create_interface(&client);
drawable::create_interface(&client);
data::create_interface(&client);
items::create_interface(&client);
input::create_interface(&client);
startup::create_interface(&client);
let _ = client.root.set(Root::create(&client)?);
hmd::make_alias(&client)?;
spatial::create_interface(&client)?;
fields::create_interface(&client)?;
drawable::create_interface(&client)?;
data::create_interface(&client)?;
items::create_interface(&client)?;
input::create_interface(&client)?;
startup::create_interface(&client)?;
let pid_printable = pid
.map(|pid| pid.to_string())
@@ -160,7 +160,7 @@ impl Client {
)
});
client
Ok(client)
}
#[inline]

View File

@@ -6,6 +6,7 @@ use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use tokio::net::UnixListener;
use tokio::task::JoinHandle;
use tracing::error;
pub static FRAME: AtomicU64 = AtomicU64::new(0);
@@ -20,7 +21,9 @@ impl EventLoop {
let join_handle = task::new(|| "event loop", async move {
loop {
let Ok((socket, _)) = socket.accept().await else { continue };
Client::from_connection(socket);
if let Err(e) = Client::from_connection(socket) {
error!(?e, "Unable to create client from connection");
}
}
})?;
let event_loop = Arc::new(EventLoop { join_handle });

View File

@@ -18,8 +18,8 @@ pub struct Scenegraph {
}
impl Scenegraph {
pub fn get_client(&self) -> Arc<Client> {
self.client.get().unwrap().upgrade().unwrap()
pub fn get_client(&self) -> Option<Arc<Client>> {
self.client.get()?.upgrade()
}
pub fn add_node(&self, node: Node) -> Arc<Node> {
@@ -51,10 +51,11 @@ impl Scenegraph {
impl scenegraph::Scenegraph for Scenegraph {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
let Some(client) = self.get_client() else {return Err(ScenegraphError::SignalNotFound)};
debug_span!("Handle signal", path, method).in_scope(|| {
self.get_node(path)
.ok_or(ScenegraphError::NodeNotFound)?
.send_local_signal(self.get_client(), method, data)
.send_local_signal(client, method, data)
})
}
fn execute_method(
@@ -63,10 +64,11 @@ impl scenegraph::Scenegraph for Scenegraph {
method: &str,
data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> {
let Some(client) = self.get_client() else {return Err(ScenegraphError::MethodNotFound)};
debug_span!("Handle method", path, method).in_scope(|| {
self.get_node(path)
.ok_or(ScenegraphError::NodeNotFound)?
.execute_local_method(self.get_client(), method, data)
.execute_local_method(client, method, data)
})
}
}