fixed home directory expansion regex and made path and dir flags expand home dir

This commit is contained in:
Valerie Wolfe 2022-06-28 16:01:28 -04:00
parent 9778cfd7f4
commit 2449af240c
4 changed files with 15 additions and 7 deletions

2
Cargo.lock generated
View file

@ -13,7 +13,7 @@ dependencies = [
[[package]] [[package]]
name = "config" name = "config"
version = "0.1.2" version = "0.1.4"
dependencies = [ dependencies = [
"regex", "regex",
"toml", "toml",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "config" name = "config"
version = "0.1.3" version = "0.1.4"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

5
README.md Normal file
View file

@ -0,0 +1,5 @@
# config
`config` is a configuration manager written in Rust.

View file

@ -18,6 +18,8 @@ mod error;
fn main() -> Result<(), Box<dyn Error>>{ fn main() -> Result<(), Box<dyn Error>>{
let home_dir = var("HOME").unwrap(); let home_dir = var("HOME").unwrap();
let home_regex = Regex::new("^~")?;
let mut config_path = PathBuf::from(&home_dir); let mut config_path = PathBuf::from(&home_dir);
config_path.push(".config/config.toml"); config_path.push(".config/config.toml");
if !config_path.exists() { if !config_path.exists() {
@ -94,8 +96,9 @@ fn main() -> Result<(), Box<dyn Error>>{
if file_path.is_dir() { if file_path.is_dir() {
path = true; path = true;
} else { } else {
let parent = file_path.parent().unwrap(); let i_parent = file_path.parent().unwrap().to_str().unwrap();
println!("{}", parent.to_str().unwrap()); let parent = home_regex.replace(i_parent, home_dir);
println!("{}", parent);
return Ok(()); return Ok(());
} }
} }
@ -103,7 +106,8 @@ fn main() -> Result<(), Box<dyn Error>>{
if prop_path.is_none() { if prop_path.is_none() {
error::no_property(entry, "path"); 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); println!("{}", file_path);
return Ok(()); return Ok(());
} }
@ -118,8 +122,7 @@ fn main() -> Result<(), Box<dyn Error>>{
} else { } else {
raw_command = prop_command.unwrap().as_str().unwrap().to_string(); raw_command = prop_command.unwrap().as_str().unwrap().to_string();
} }
let regex = Regex::new("[^~]~")?; let expansion = home_regex.replace(raw_command.as_str(), home_dir);
let expansion = regex.replace(raw_command.as_str(), home_dir);
let mut parts = expansion.split_whitespace(); let mut parts = expansion.split_whitespace();
let command = parts.next().unwrap(); let command = parts.next().unwrap();