diff --git a/crates/lcode/cargo/default.toml b/crates/lcode/cargo/default.toml new file mode 100644 index 0000000..4776cbf --- /dev/null +++ b/crates/lcode/cargo/default.toml @@ -0,0 +1,7 @@ +[package] +name = "my-leetcode" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = { version = "0.8.5" } diff --git a/crates/lcode/src/editor.rs b/crates/lcode/src/editor.rs index a4a17f1..5df3f9a 100644 --- a/crates/lcode/src/editor.rs +++ b/crates/lcode/src/editor.rs @@ -1,6 +1,5 @@ use std::process::Command; -use futures::StreamExt; use lcode_config::global::{G_CONFIG_PATH, G_LOG_PATH, G_USER_CONFIG}; use leetcode_api::{ dao::{query::Query, save_info}, @@ -41,39 +40,37 @@ pub async fn integr_cargo(id: &str, code_path: &str) -> Result<()> { let metadata = fs::metadata(&cargo_path) .await .into_diagnostic()?; - if metadata.len() == 0 { - f.write_all( - r#"[package] -name = "my-leetcode" -version = "0.1.0" -edition = "2021" - -[dependencies] -rand = { version = "0.8.5" } -"# - .as_bytes(), - ) - .await - .into_diagnostic()?; + if metadata.len() == 0 { + f.write_all(include_bytes!("../cargo/default.toml")) + .await + .into_diagnostic()?; + // return early + return append_bin(id, code_path, &mut f).await; } let cargo_str = fs::read_to_string(&cargo_path) .await .into_diagnostic()?; - let cont = futures::stream::iter(cargo_str.split('\n')) - .any(|f| async { f.contains(&format!("\"{id}\"")) }) - .await; + + let cont = cargo_str + .split('\n') + .any(|f| f.contains(&format!("\"{id}\""))); if !cont { - let append = format!("[[bin]]\nname = \"{}\"\npath = \"./{}\"\n", id, code_path); - f.write_all(append.as_bytes()) - .await - .into_diagnostic()?; + append_bin(id, code_path, &mut f).await?; } Ok(()) } +async fn append_bin(id: &str, code_path: &str, f: &mut fs::File) -> Result<(), miette::Error> { + let append = format!("\n[[bin]]\nname = \"{}\"\npath = \"./{}\"", id, code_path); + f.write_all(append.as_bytes()) + .await + .into_diagnostic()?; + Ok(()) +} + #[derive(Clone, Copy)] #[derive(Debug)] #[derive(Default)]