provisionally updated to remove regex; home expansion will be moved to a helper function later

This commit is contained in:
Valerie Wolfe 2023-06-19 17:11:16 -04:00
parent ab58437131
commit ce6273d98f
5 changed files with 6 additions and 66 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
Cargo.lock
/target /target

58
Cargo.lock generated
View file

@ -1,58 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "config"
version = "0.2.0"
dependencies = [
"regex",
"toml",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
[[package]]
name = "serde"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]

View file

@ -6,7 +6,6 @@ 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
[dependencies] [dependencies]
regex = "1.5.6"
toml = "0.5.9" toml = "0.5.9"
[profile.release] [profile.release]

View file

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

View file

@ -9,7 +9,6 @@ use std::{
} }
}; };
use regex::Regex;
use toml::{ use toml::{
Value, Value,
value::Map value::Map
@ -19,7 +18,6 @@ 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");
@ -101,14 +99,14 @@ fn main() -> Result<(), Box<dyn Error>>{
error::no_property(&fullname, "path"); error::no_property(&fullname, "path");
} }
let raw_path = prop_path.unwrap().as_str().unwrap(); let raw_path = prop_path.unwrap().as_str().unwrap();
let expanded = home_regex.replace(raw_path, &home_dir); let expanded = if raw_path.starts_with("~") { raw_path.replacen("~", &home_dir, 1) } else { raw_path.to_string() };
let file_path = PathBuf::from(expanded.to_string()); let file_path = PathBuf::from(expanded.to_string());
if file_path.is_dir() { if file_path.is_dir() {
println!("{}", expanded); println!("{}", expanded);
return Ok(()); return Ok(());
} else { } else {
let i_parent = file_path.parent().unwrap().to_str().unwrap(); let i_parent = file_path.parent().unwrap().to_str().unwrap();
let parent = home_regex.replace(i_parent, home_dir); let parent = if i_parent.starts_with("~") { i_parent.replacen("~", &home_dir, 1) } else { i_parent.to_string() };
println!("{}", parent); println!("{}", parent);
return Ok(()); return Ok(());
} }
@ -118,7 +116,7 @@ fn main() -> Result<(), Box<dyn Error>>{
error::no_property(&fullname, "path"); error::no_property(&fullname, "path");
} }
let i_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); let file_path = if i_file_path.starts_with("~") { i_file_path.replace("~", &home_dir) } else { i_file_path.to_string() };
println!("{}", file_path); println!("{}", file_path);
return Ok(()); return Ok(());
} }
@ -133,7 +131,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 expansion = home_regex.replace(raw_command.as_str(), home_dir); let expansion = if raw_command.starts_with("~") { raw_command.replace("~", &home_dir) } else { raw_command.to_string() };
let mut parts = expansion.split_whitespace(); let mut parts = expansion.split_whitespace();
let command = parts.next().unwrap(); let command = parts.next().unwrap();