very annoying relearning async things

Phillip Davis created

Change summary

Cargo.toml    |  1 +
src/config.rs | 20 +++++++++++++++++++-
2 files changed, 20 insertions(+), 1 deletion(-)

Detailed changes

Cargo.toml 🔗

@@ -5,6 +5,7 @@ edition = "2024"
 
 [dependencies]
 anyhow = "1.0.100"
+async-gen = "0.2.3"
 async-lsp = "0.2.2"
 async-process = "2.3"
 clap = { version = "4.5.48", features = ["derive"] }

src/config.rs 🔗

@@ -1,6 +1,8 @@
+use async_gen::{AsyncIter, r#gen};
+use anyhow::Context;
 use clap::Parser;
 use rmcp::serde_json;
-use std::path::PathBuf;
+use std::{fs::FileType, path::{Path, PathBuf}};
 
 #[derive(Parser, Debug)]
 #[command(version, about)]
@@ -18,6 +20,8 @@ pub struct Command {
 #[derive(serde::Deserialize, Debug)]
 pub struct Config {
     pub lsp_server_command: Command,
+
+	/// An absolute path pointing to the root of the project on disk.
     pub project_root: PathBuf,
 }
 
@@ -27,4 +31,18 @@ impl Config {
         let config: Config = serde_json::from_str(&config_content)?;
         Ok(config)
     }
+
+    /// Returns a `Some` containing an absolute `` to the file if it is in the
+    /// project, otherwise returns `None`
+    pub async fn path_in_project<P>(&self, path: P) -> anyhow::Result<Option<PathBuf>>
+    where
+        P: AsRef<Path>,
+    {
+		let joined = self.project_root.join(&path);
+		match tokio::fs::try_exists(joined.as_path()).await {
+			Ok(true) => anyhow::Ok(Some(joined)),
+			Ok(false) => anyhow::Ok(None),
+			Err(err) => anyhow::bail!("Could not look for path {:?} in project {:?}, error: {}", path.as_ref(), self.project_root, err)
+		}
+	}
 }