diff --git a/Cargo.lock b/Cargo.lock index 74eb4c8..19a18d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1675,6 +1675,7 @@ dependencies = [ "async-stream", "async-trait", "bsp-server", + "crossbeam-channel", "dirs", "lazy_static", "libc", diff --git a/Cargo.toml b/Cargo.toml index e037866..f728289 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,17 @@ daemon = [ "simctl", "process-stream" ] -server = [ "serial", "logging", "dirs", "bsp-server", "url", "wax", "shell-words", "compilation" ] +server = [ + "serial", + "logging", + "dirs", + "bsp-server", + "url", + "wax", + "shell-words", + "compilation", + "crossbeam-channel" + ] lua = [ "mlua", "serial", "simctl" ] serial = [ "serde", "serde_json", "serde_yaml" ] compilation = [ "serial", "lazy_static", "shell-words", "xcode" ] @@ -87,3 +97,4 @@ parity-tokio-ipc = { version = "0.9.0", optional = true } simctl = { path = "../../../sources/simctl/", optional = true } thiserror = "1.0.31" libc = "0.2.126" +crossbeam-channel = { version = "0.5.4", optional = true } diff --git a/src/error.rs b/src/error.rs index fca99bd..e90288b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -36,6 +36,15 @@ pub enum Error { Watch(#[from] WatchError), #[error("[Error] (Message) {0}")] Message(String), + #[error("[Error] (send) {0}")] + #[cfg(feature = "server")] + Channel(#[from] crossbeam_channel::SendError), + #[cfg(feature = "server")] + #[error("[Error] (BSP) {0}")] + BspServer(#[from] bsp_server::ProtocolError), + #[cfg(feature = "server")] + #[error("[Error] (Lock) {0}")] + Lock(String), } #[derive(ThisError, Debug)] diff --git a/src/server.rs b/src/server.rs index fd72a63..b33c331 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,8 @@ //! Module for communicating with SourceKit Build Server Protocol. mod extensions; -use anyhow::{Context, Result}; +use crate::{Error, Result}; +use anyhow::Context; use bsp_server::{types::*, Connection, Message, Request, RequestId, Response}; use serde_json::{json, Value}; use std::{fs::read_to_string, path::PathBuf}; @@ -71,8 +72,8 @@ impl BuildServer { let notification: Message = OptionsChangedNotification::new(params.uri, flags, root).try_into()?; - conn.send(notification) - .context("notify registration for changes") + conn.send(notification)?; + Ok(()) } /// List of compiler options necessary to compile a file. @@ -90,8 +91,9 @@ impl BuildServer { let flags = self.file_flags(filepath)?; let response = OptionsResponse::new(flags, root).as_response(id); - conn.send(response) - .context("Respond to textDocument/sourceKitOptions") + conn.send(response)?; + + Ok(()) } /// Process Workspace BuildTarget request @@ -100,8 +102,8 @@ impl BuildServer { tracing::debug!("Processing"); let response = WorkspaceBuildTargetsResult::new(vec![]); - conn.send((id, response)) - .context("Respond to workspace/buildTargets") + conn.send((id, response))?; + Ok(()) } /// Process BuildTarget output paths @@ -114,8 +116,9 @@ impl BuildServer { ) -> Result<()> { tracing::debug!("Processing"); let response = BuildTargetOutputPathsResponse::new(vec![]).as_response(id); - conn.send(response) - .context("Respond to buildTarget/outputPaths") + conn.send(response)?; + + Ok(()) } /// Process BuildTarget Sources Request @@ -128,8 +131,8 @@ impl BuildServer { ) -> Result<()> { tracing::debug!("Processing"); let response = BuildTargetSourcesResult::new(vec![]); - conn.send((id, response)) - .context("Respond to buildTarget/outputPaths") + conn.send((id, response))?; + Ok(()) } /// Return Default response for unhandled requests. @@ -146,8 +149,8 @@ impl BuildServer { id.clone(), 123, format!("unhandled method {method}"), - )) - .context("Fail to respond") + ))?; + Ok(()) } /// Handle Shutdown Request @@ -171,8 +174,9 @@ impl BuildServer { .to_vec() .pipe(Result::Ok), Err(err) => { - tracing::error!("fail to get file flags {err}"); - anyhow::bail!("fail to get file flags {err}") + let msg = format!("fail to get file flags {err}"); + tracing::error!("{}", msg); + return Err(Error::Lock(msg)); } } } diff --git a/src/server/bin.rs b/src/server/bin.rs index 22a9d57..a02f863 100644 --- a/src/server/bin.rs +++ b/src/server/bin.rs @@ -1,6 +1,6 @@ -use anyhow::Result; use bsp_server::{Connection, Message, Request}; use tracing::Level; +use xbase::Result; use xbase::{ server::{BuildServer, BuildTargetOutputPathsRequest, OptionsChangedRequest, OptionsRequest}, util::tracing::install_tracing,