clone_lib/projectmanager/nvim/
mod.rs1use std::fs;
2use std::path::PathBuf;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7#[serde(default)]
8pub struct Entry {
9 commandexit: Option<String>,
10 commandstart: Option<String>,
11 path: PathBuf,
12 name: String,
13 current: u8,
14}
15
16impl Default for Entry {
17 fn default() -> Self {
18 Entry {
19 name: "".to_string(),
20 path: PathBuf::from(""),
21 current: 0,
22 commandstart: Some("NvimTreeToggle".to_string()),
23 commandexit: Some("".to_string()),
24 }
25 }
26}
27
28pub fn add_project(
29 target_path: PathBuf,
30 workspace: PathBuf,
31 host: String,
32 group: String,
33 name: String,
34 debug: bool,
35) {
36 let json_data = fs::read_to_string(&target_path).unwrap_or("[]".to_string());
38
39 let mut entries: Vec<Entry> =
40 serde_json::from_str(&json_data).expect("JSON was not well-formed");
41 let must_add = !entries.iter().any(|e| e.path == workspace);
42
43 if debug && !must_add {
44 println!("Entry already exist {:?}.", workspace);
45 }
46 if must_add {
48 let new_entry = Entry {
50 name: format!("{}/{}/{}", host, group, name),
51 path: workspace,
52 ..Entry::default()
53 };
54
55 entries.push(new_entry.clone());
57
58 let updated_data =
60 serde_json::to_string_pretty(&entries).expect("Failed to serialize JSON");
61 let _ = fs::create_dir_all(target_path.parent().unwrap());
62 fs::write(&target_path, updated_data).expect("Unable to write Nvim JSON file");
63 if debug {
64 println!("Entry added {:#?}", new_entry);
65 }
66 }
67}