feat: switch to new identifiers method instead of machines

This commit is contained in:
Youwen Wu 2024-06-26 16:46:58 -07:00
parent 7efe4aef0d
commit 4e25e59422
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3
4 changed files with 38 additions and 39 deletions

7
TODO.md Normal file
View file

@ -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

View file

@ -13,7 +13,7 @@ struct Cli {
fn fire() { fn fire() {
println!("Attempting to find and read `dartfile.toml`."); 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..."); println!("Writing symlinks...");
match gun.create_symlinks() { match gun.create_symlinks() {
Ok(_) => println!("Symlinks created successfully!"), Ok(_) => println!("Symlinks created successfully!"),

View file

@ -15,8 +15,7 @@ struct DotfileRaw {
location: String, location: String,
destination: String, destination: String,
strategy: String, strategy: String,
machines: Vec<String>, identifiers: Vec<String>,
applies_to: String,
} }
impl DotfileRaw { impl DotfileRaw {
@ -38,8 +37,7 @@ impl DotfileRaw {
location: location_path, location: location_path,
destination: destination_path, destination: destination_path,
strategy: self.determine_strategy()?, strategy: self.determine_strategy()?,
machines: self.machines.clone(), identifiers: self.identifiers.clone(),
applies_to: self.applies_to.clone(),
}) })
} }
// TODO: improve error handling for parsing from raw TOML // TODO: improve error handling for parsing from raw TOML
@ -68,56 +66,46 @@ impl DotfileRaw {
.as_str() .as_str()
.unwrap() .unwrap()
.to_string(); .to_string();
let machines = table let identifiers = table
.get("machines") .get("identifiers")
.ok_or("Missing 'machines' field.") .ok_or("Missing 'identifiers' field.")
.unwrap() .unwrap()
.as_array() .as_array()
.unwrap() .unwrap()
.iter() .iter()
.map(|x| x.as_str().unwrap().to_string()) .map(|x| x.as_str().unwrap().to_string())
.collect(); .collect();
let applies_to = table
.get("applies_to")
.ok_or("Missing 'applies_to' field.")
.unwrap()
.as_str()
.unwrap()
.to_string();
DotfileRaw { DotfileRaw {
location, location,
destination, destination,
strategy, strategy,
machines, identifiers,
applies_to,
} }
} }
} }
/// The configuration object parsed from the `config` field in `dartgun.toml` /// The configuration object parsed from the `config` field in `dartgun.toml`
#[derive(Debug)] #[derive(Debug)]
pub struct Config { pub struct Machine {
machine: String, identifiers: Vec<String>,
available: Vec<String>,
} }
impl Config { impl Machine {
// TODO: improve error handling for parsing config // TODO: improve error handling for parsing config
/// Generate a `Config` from a raw `dartfile.toml`. /// Generate a `Config` from a raw `dartfile.toml`.
/// Will panic! if the format is invalid. /// Will panic! if the format is invalid.
fn from_table(table: Table) -> Config { fn from_table(table: Table) -> Machine {
let machine = table.get("machine").unwrap().as_str().unwrap().to_string(); let identifiers = table
let available = table .get("identifiers")
.get("available")
.unwrap() .unwrap()
.as_array() .as_array()
.unwrap() .unwrap()
.iter() .iter()
.map(|x| x.as_str().unwrap().to_string()) .map(|x| x.as_str().unwrap().to_string())
.collect(); .collect();
Config { available, machine } Machine { identifiers }
} }
} }
@ -125,7 +113,7 @@ impl Config {
/// struct can assume that the dartfile is at least semantically valid /// struct can assume that the dartfile is at least semantically valid
#[derive(Debug)] #[derive(Debug)]
pub struct Dartfile { pub struct Dartfile {
pub config: Config, pub machine: Machine,
pub dots: Vec<Dotfile>, pub dots: Vec<Dotfile>,
} }
@ -155,8 +143,7 @@ pub struct Dotfile {
pub location: PathBuf, pub location: PathBuf,
pub destination: PathBuf, pub destination: PathBuf,
pub strategy: Strategy, pub strategy: Strategy,
pub machines: Vec<String>, pub identifiers: Vec<String>,
pub applies_to: String,
} }
impl Dotfile { impl Dotfile {
@ -174,14 +161,19 @@ impl Dotfile {
/// Takes a path to a `dartgun.toml` and produces a well-typed Dartfile object. /// 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. /// Currently crashes on any parse errors, but this behavior will likely change in the future.
pub fn parse(path: &Path) -> Dartfile { pub fn parse(dartgun_path: &Path, machine_path: &Path) -> Dartfile {
let raw_data = fs::read_to_string(path).expect("Couldn't read the file."); let dartgun_toml = fs::read_to_string(dartgun_path)
let value: Table = raw_data.parse::<Table>().expect("Couldn't parse the TOML."); .expect("Couldn't read dartgun.toml")
.parse::<Table>()
.expect("Couldn't parse the TOML.");
let machine_toml = fs::read_to_string(machine_path)
.expect("Couldn't read machine.toml")
.parse::<Table>()
.expect("Couldn't parse the TOML.");
let config_raw = value.get("config").unwrap().as_table().unwrap(); let dots_raw = dartgun_toml.get("dots").unwrap().as_array().unwrap();
let dots_raw = value.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 let dots = dots_raw
.iter() .iter()
.map(|x| { .map(|x| {
@ -192,5 +184,5 @@ pub fn parse(path: &Path) -> Dartfile {
}) })
.collect::<Vec<Dotfile>>(); .collect::<Vec<Dotfile>>();
Dartfile { config, dots } Dartfile { machine, dots }
} }

View file

@ -7,9 +7,9 @@ mod dartfile;
mod dartgun; mod dartgun;
fn main() { fn main() {
let test_path = Path::new("./dartgun.toml"); let test_dartfile_path = Path::new("./dartgun.toml");
let test_dotfile = parse(test_path); let test_machine_path = Path::new("./machine.toml");
println!("{:?}", parse(test_path)); let test_dotfile = parse(test_dartfile_path, test_machine_path);
match test_dotfile.validate() { match test_dotfile.validate() {
Ok(_) => println!("Dotfile seems valid!"), Ok(_) => println!("Dotfile seems valid!"),
Err(_) => println!("Dotfile is invalid!"), Err(_) => println!("Dotfile is invalid!"),