From 449ac3f6a04c310082b9bb4b2d1a855514b527ce Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Thu, 27 Jun 2024 13:30:29 -0700 Subject: [PATCH] feat: better error messages and default strategy --- TODO.md | 1 + src/dartfile.rs | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/TODO.md b/TODO.md index 57c4900..f3d3a68 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,4 @@ Features which need to be created before v0.1.0 release. - [ ] Finalize configuration file scheme - [x] Implement identifiers - [ ] Handle case when files already exist +- [ ] Implement hardlink functionality diff --git a/src/dartfile.rs b/src/dartfile.rs index 1670a64..6f881ce 100644 --- a/src/dartfile.rs +++ b/src/dartfile.rs @@ -43,35 +43,36 @@ impl DotfileRaw { // TODO: improve error handling for parsing from raw TOML /// Create a new `DotfileRaw` from a `toml::Table`, which is a `Vec`. - /// Will panic if the table does not contain the fields specified. + /// Will panic if the table does not contain the fields specified, except for 'strategy', + /// which is `symlink` by default. fn from_table(table: Table) -> DotfileRaw { let location = table .get("location") .ok_or("Missing 'location' field.") .unwrap() .as_str() - .unwrap() + .expect("Location field must be a string pointing to a valid path!") .to_string(); let destination = table .get("destination") .ok_or("Missing 'destination' field.") .unwrap() .as_str() - .unwrap() - .to_string(); - let strategy = table - .get("strategy") - .ok_or("Missing 'strategy' field.") - .unwrap() - .as_str() - .unwrap() + .expect("Destination field must be a string pointing to a valid path!") .to_string(); + let strategy = match table.get("strategy") { + Some(val) => val + .as_str() + .expect("Strategy field must be a string!") + .to_string(), + None => "symlink".to_string(), + }; let identifiers = table .get("identifiers") .ok_or("Missing 'identifiers' field.") .unwrap() .as_array() - .unwrap() + .expect("Identifiers field must be an array of strings!") .iter() .map(|x| x.as_str().unwrap().to_string()) .collect();