Skip to content

Commit

Permalink
perf(editor): improve
Browse files Browse the repository at this point in the history
  • Loading branch information
saying121 committed Jun 10, 2024
1 parent 5d3c289 commit 459ffb5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
7 changes: 7 additions & 0 deletions crates/lcode/cargo/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "my-leetcode"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = { version = "0.8.5" }
41 changes: 19 additions & 22 deletions crates/lcode/src/editor.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 459ffb5

Please sign in to comment.