From 2449af240ccfa883c95662450e8a2fb50b932c1b Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 28 Jun 2022 16:01:28 -0400 Subject: [PATCH] fixed home directory expansion regex and made path and dir flags expand home dir --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 5 +++++ src/main.rs | 13 ++++++++----- 4 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 README.md diff --git a/Cargo.lock b/Cargo.lock index 018e882..3b220df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "config" -version = "0.1.2" +version = "0.1.4" dependencies = [ "regex", "toml", diff --git a/Cargo.toml b/Cargo.toml index 818284f..cd5408e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "config" -version = "0.1.3" +version = "0.1.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..33aa0db --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ + +# config + +`config` is a configuration manager written in Rust. + diff --git a/src/main.rs b/src/main.rs index 81a7267..3ce9e2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,8 @@ mod error; fn main() -> Result<(), Box>{ let home_dir = var("HOME").unwrap(); + let home_regex = Regex::new("^~")?; + let mut config_path = PathBuf::from(&home_dir); config_path.push(".config/config.toml"); if !config_path.exists() { @@ -94,8 +96,9 @@ fn main() -> Result<(), Box>{ if file_path.is_dir() { path = true; } else { - let parent = file_path.parent().unwrap(); - println!("{}", parent.to_str().unwrap()); + let i_parent = file_path.parent().unwrap().to_str().unwrap(); + let parent = home_regex.replace(i_parent, home_dir); + println!("{}", parent); return Ok(()); } } @@ -103,7 +106,8 @@ fn main() -> Result<(), Box>{ if prop_path.is_none() { error::no_property(entry, "path"); } - let file_path = prop_path.unwrap().as_str().unwrap(); + let i_file_path = prop_path.unwrap().as_str().unwrap(); + let file_path = home_regex.replace(i_file_path, home_dir); println!("{}", file_path); return Ok(()); } @@ -118,8 +122,7 @@ fn main() -> Result<(), Box>{ } else { raw_command = prop_command.unwrap().as_str().unwrap().to_string(); } - let regex = Regex::new("[^~]~")?; - let expansion = regex.replace(raw_command.as_str(), home_dir); + let expansion = home_regex.replace(raw_command.as_str(), home_dir); let mut parts = expansion.split_whitespace(); let command = parts.next().unwrap();