From 9b7f8662728ed388cc8b36e957848ae164d0e3d0 Mon Sep 17 00:00:00 2001 From: Jakub Martisko Date: Fri, 20 Oct 2023 16:55:03 +0200 Subject: [PATCH] Add: auto and set commands --- Cargo.lock | 82 ++++++++++++++++++++++++++++++++++- src/main.rs | 122 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 180 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70f00dd..653e26c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,8 @@ version = "0.1.0" dependencies = [ "assert_cmd", "clap", + "serde", + "serde_yaml", ] [[package]] @@ -137,7 +139,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.15", ] [[package]] @@ -170,6 +172,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.1" @@ -191,6 +199,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hashbrown" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" + [[package]] name = "heck" version = "0.4.1" @@ -203,6 +217,16 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "indexmap" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "io-lifetimes" version = "1.0.10" @@ -235,6 +259,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "libc" version = "0.2.144" @@ -325,11 +355,44 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + [[package]] name = "serde" version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "serde_yaml" +version = "0.9.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a49e178e4452f45cb61d0cd8cebc1b0fafd3e41929e996cef79aa3aca91f574" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] [[package]] name = "strsim" @@ -337,6 +400,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.15" @@ -360,6 +434,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "unsafe-libyaml" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa" + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/src/main.rs b/src/main.rs index ba07811..4ed8493 100644 --- a/src/main.rs +++ b/src/main.rs @@ -136,6 +136,49 @@ impl AlternativeGroup { } } + fn remove_alternative (&mut self, path: String) { + let mut alternatives = match self { + AlternativeGroup::DropIn (group) => &mut group.alternatives, + AlternativeGroup::BuiltIn (group) => &mut group.alternatives, + }; + //TODO Later: use extract_if instead -> currently marked as "nightly-only experimental API" + //_ = alternatives.extract_if(|x| x.get_path() == path); + let mut i = 0; + while i < alternatives.len() { + if alternatives[i].get_path().clone() == path { + _ = alternatives.remove(i); + } else { + i += 1; + } + } + + } + + fn set_auto_mode (&mut self) { + match self { + AlternativeGroup::DropIn (_) => {}, + AlternativeGroup::BuiltIn (ref mut group) => group.mode = "Auto".to_string(), + }; + } + + fn set_manual_mode (&mut self, path: String) { + match self { + AlternativeGroup::DropIn (_) => {}, + AlternativeGroup::BuiltIn (ref mut group) => { + group.mode = "Manual".to_string(); + group.manualPath = path; + }, + }; + } + + fn get_manual_path (&self) -> String { + let rv = match self { + AlternativeGroup::DropIn (_) => "".to_string(), + AlternativeGroup::BuiltIn (group) => group.manualPath.clone(), + }; + return rv.clone(); + } + fn get_mode (&self) -> String { let rv = match self { AlternativeGroup::DropIn (_) => "Auto".to_string(), @@ -254,7 +297,7 @@ fn filter_buildins(name: &String, groups: Vec) -> Option) -> Option<&Alternative> { } } +fn manual_prio(alts: &Vec, path: String) -> Option<&Alternative>{ + for alt in alts { + if *alt.get_path() == path { + return Some(alt); + } + } + return None; +} + /* * This is a top level function that deals with symlink updates for an alternative group set to auto * 1. read the config files, if there's no match in hte builtin file, create a new alternative group * */ -fn auto_update (name: &String, buildins_path: &String, dropins_path: &String) { +fn update_links (name: &String, buildins_path: &String, dropins_path: &String) { let builtins = filter_buildins(name, read_config(buildins_path)); let dropins = merge_dropins(name, read_dropins(dropins_path)); match builtins { None => {panic!("Reached the unreachable!");} //This should be unreachable branch Some(builtin) => { - if builtin.get_mode().to_lowercase() == "auto".to_string() { - let mut alternatives = builtin.get_alternatives(); - if let Some(dropins) = dropins { // are there any dropins? - alternatives.append(&mut dropins.get_alternatives()); //append them - } - println!("List of all alternatives (Builtins + dropins): {:?}",alternatives); //TODO debug text - let best_alt = highest_prio(&alternatives); - for (fst,snd) in best_alt.expect("No alternative found for the alternative group").create_links() { - create_symlinks(fst,snd); - } + let mut alternatives = builtin.get_alternatives(); + if let Some(dropins) = dropins { // are there any dropins? + alternatives.append(&mut dropins.get_alternatives()); //append them + } + println!("List of all alternatives (Builtins + dropins): {:?}",alternatives); //TODO debug text + let best_alt: Option<&Alternative> = if builtin.get_mode().to_lowercase() == "auto".to_string() { + // group is in auto mode + highest_prio(&alternatives) } else { - //do nothing - group is set to manual + // Group is in the manual mode + manual_prio(&alternatives, builtin.get_manual_path()) + }; + for (fst,snd) in best_alt.expect("No alternative found for the alternative group").create_links() { + create_symlinks(fst,snd); } } @@ -424,8 +478,6 @@ fn main() { * 4. Write the modified DB file, if the the alternative group is set to manual then end else: * 5. Run the quivalent of the AUTO branch */ - println!("In the Install Branch!"); - println!("Name: {:?}, Path: {:?}", name, path); //Parse the arguments let followers = unwrap_followers(follower); let alt = Alternative::new(link.to_string(),path.to_string(),*priority,followers); @@ -439,7 +491,6 @@ fn main() { // Maybe TODO check for duplicit entries in the same alternative group for a_g in &mut built_in_db { if a_g.get_name() == name.to_string() { - println!("Alternative Group Found!: {:?}", a_g); // TODO debug a_g.append_alternative (alt.clone()); done = true; break; @@ -450,12 +501,11 @@ fn main() { if done == false { let new_alt_group = AlternativeGroup::new_builtin(name.to_string(), [alt.clone()].to_vec()); built_in_db.push(new_alt_group); - println!("Alternative Group Not Found!: {:?}", built_in_db); //TODO debug - } + write_config(&BUILT_IN_DB_PATH.to_string(),built_in_db); // if the alternative group is se to auto - check the priorites and update the symlinks - auto_update(name, &BUILT_IN_DB_PATH.to_string(), &DROP_IN_DIR_PATH.to_string()); + update_links(name, &BUILT_IN_DB_PATH.to_string(), &DROP_IN_DIR_PATH.to_string()); } Commands::Display { ref name, @@ -466,12 +516,38 @@ fn main() { println!("Drop ins:\n {:?}", merge_dropins(name, dropins)); } Commands::Auto { ref name} => { - println!("In the Auto Branch!"); - auto_update(name, &BUILT_IN_DB_PATH.to_string(), &DROP_IN_DIR_PATH.to_string()); + let mut built_in_db = read_config(&BUILT_IN_DB_PATH.to_string()); + for a_g in &mut built_in_db { + if a_g.get_name() == name.to_string() { + a_g.set_auto_mode(); + } + } + write_config(&BUILT_IN_DB_PATH.to_string(),built_in_db); + + update_links(name, &BUILT_IN_DB_PATH.to_string(), &DROP_IN_DIR_PATH.to_string()); + } + Commands::Set { ref name, ref path} => { + let mut built_in_db = read_config(&BUILT_IN_DB_PATH.to_string()); + for a_g in &mut built_in_db { + if a_g.get_name() == name.to_string() { + a_g.set_manual_mode(path.clone()); + } + } + write_config(&BUILT_IN_DB_PATH.to_string(),built_in_db); + + update_links(name, &BUILT_IN_DB_PATH.to_string(), &DROP_IN_DIR_PATH.to_string()); } Commands::Remove { ref name, ref path } => { - println!("In the remove Branch!"); - println!("Name: {:?}, Path: {:?}", name, path) + let mut built_in_db = read_config(&BUILT_IN_DB_PATH.to_string()); + + for a_g in &mut built_in_db { + if a_g.get_name() == name.to_string() { + a_g.remove_alternative (path.clone()); + } + } + write_config(&BUILT_IN_DB_PATH.to_string(),built_in_db); + update_links(name, &BUILT_IN_DB_PATH.to_string(), &DROP_IN_DIR_PATH.to_string()); + } _ => println!("We've got a problem"), };