From 4e25e594225d64619f8f44cebe1e671a4791a991 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Wed, 26 Jun 2024 16:46:58 -0700 Subject: [PATCH] feat: switch to new identifiers method instead of machines --- TODO.md | 7 ++++++ src/cli.rs | 2 +- src/dartfile.rs | 62 +++++++++++++++++++++---------------------------- src/main.rs | 6 ++--- 4 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..f9febf7 --- /dev/null +++ b/TODO.md @@ -0,0 +1,7 @@ +# P1 + +Features which need to be created before v0.1.0 release. + +- Improve graceful error handling when parsing files +- Finalize configuration file scheme +- Implement identifiers diff --git a/src/cli.rs b/src/cli.rs index e1fd41b..801e0cf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -13,7 +13,7 @@ struct Cli { fn fire() { println!("Attempting to find and read `dartfile.toml`."); - let gun = dartfile::parse(Path::new("./dartgun.toml")); + let gun = dartfile::parse(Path::new("./dartgun.toml"), Path::new("./machine.toml")); println!("Writing symlinks..."); match gun.create_symlinks() { Ok(_) => println!("Symlinks created successfully!"), diff --git a/src/dartfile.rs b/src/dartfile.rs index de4ab13..15348f5 100644 --- a/src/dartfile.rs +++ b/src/dartfile.rs @@ -15,8 +15,7 @@ struct DotfileRaw { location: String, destination: String, strategy: String, - machines: Vec, - applies_to: String, + identifiers: Vec, } impl DotfileRaw { @@ -38,8 +37,7 @@ impl DotfileRaw { location: location_path, destination: destination_path, strategy: self.determine_strategy()?, - machines: self.machines.clone(), - applies_to: self.applies_to.clone(), + identifiers: self.identifiers.clone(), }) } // TODO: improve error handling for parsing from raw TOML @@ -68,56 +66,46 @@ impl DotfileRaw { .as_str() .unwrap() .to_string(); - let machines = table - .get("machines") - .ok_or("Missing 'machines' field.") + let identifiers = table + .get("identifiers") + .ok_or("Missing 'identifiers' field.") .unwrap() .as_array() .unwrap() .iter() .map(|x| x.as_str().unwrap().to_string()) .collect(); - let applies_to = table - .get("applies_to") - .ok_or("Missing 'applies_to' field.") - .unwrap() - .as_str() - .unwrap() - .to_string(); DotfileRaw { location, destination, strategy, - machines, - applies_to, + identifiers, } } } /// The configuration object parsed from the `config` field in `dartgun.toml` #[derive(Debug)] -pub struct Config { - machine: String, - available: Vec, +pub struct Machine { + identifiers: Vec, } -impl Config { +impl Machine { // TODO: improve error handling for parsing config /// Generate a `Config` from a raw `dartfile.toml`. /// Will panic! if the format is invalid. - fn from_table(table: Table) -> Config { - let machine = table.get("machine").unwrap().as_str().unwrap().to_string(); - let available = table - .get("available") + fn from_table(table: Table) -> Machine { + let identifiers = table + .get("identifiers") .unwrap() .as_array() .unwrap() .iter() .map(|x| x.as_str().unwrap().to_string()) .collect(); - Config { available, machine } + Machine { identifiers } } } @@ -125,7 +113,7 @@ impl Config { /// struct can assume that the dartfile is at least semantically valid #[derive(Debug)] pub struct Dartfile { - pub config: Config, + pub machine: Machine, pub dots: Vec, } @@ -155,8 +143,7 @@ pub struct Dotfile { pub location: PathBuf, pub destination: PathBuf, pub strategy: Strategy, - pub machines: Vec, - pub applies_to: String, + pub identifiers: Vec, } impl Dotfile { @@ -174,14 +161,19 @@ impl Dotfile { /// Takes a path to a `dartgun.toml` and produces a well-typed Dartfile object. /// Currently crashes on any parse errors, but this behavior will likely change in the future. -pub fn parse(path: &Path) -> Dartfile { - let raw_data = fs::read_to_string(path).expect("Couldn't read the file."); - let value: Table = raw_data.parse::().expect("Couldn't parse the TOML."); +pub fn parse(dartgun_path: &Path, machine_path: &Path) -> Dartfile { + let dartgun_toml = fs::read_to_string(dartgun_path) + .expect("Couldn't read dartgun.toml") + .parse::
() + .expect("Couldn't parse the TOML."); + let machine_toml = fs::read_to_string(machine_path) + .expect("Couldn't read machine.toml") + .parse::
() + .expect("Couldn't parse the TOML."); - let config_raw = value.get("config").unwrap().as_table().unwrap(); - let dots_raw = value.get("dots").unwrap().as_array().unwrap(); + let dots_raw = dartgun_toml.get("dots").unwrap().as_array().unwrap(); - let config = Config::from_table(config_raw.clone()); + let machine = Machine::from_table(machine_toml); let dots = dots_raw .iter() .map(|x| { @@ -192,5 +184,5 @@ pub fn parse(path: &Path) -> Dartfile { }) .collect::>(); - Dartfile { config, dots } + Dartfile { machine, dots } } diff --git a/src/main.rs b/src/main.rs index f723355..831d3ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,9 @@ mod dartfile; mod dartgun; fn main() { - let test_path = Path::new("./dartgun.toml"); - let test_dotfile = parse(test_path); - println!("{:?}", parse(test_path)); + let test_dartfile_path = Path::new("./dartgun.toml"); + let test_machine_path = Path::new("./machine.toml"); + let test_dotfile = parse(test_dartfile_path, test_machine_path); match test_dotfile.validate() { Ok(_) => println!("Dotfile seems valid!"), Err(_) => println!("Dotfile is invalid!"),