diff --git a/Cargo.lock b/Cargo.lock index 2da759d..2134704 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,206 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "fld" version = "0.1.0" +dependencies = [ + "clap", + "thiserror", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml index 0332875..e6a464a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4.5.54", features = ["derive"] } +thiserror = "2.0.18" diff --git a/src/main.rs b/src/main.rs index e7a11a9..0883cca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,62 @@ -fn main() { - println!("Hello, world!"); +use std::path::PathBuf; + +use clap::{Args, Parser}; + +use crate::mapping::Mapping; + +mod mapping; + +#[derive(Parser, Debug, PartialEq, Eq, Clone, Ord, PartialOrd, Hash)] +#[command(version, about, long_about = None)] +struct Cli { + dir: String, + subdirs: Option>, + #[command(flatten)] + editor: Option, +} + +#[derive(Args, Debug, PartialEq, Eq, Clone, Ord, PartialOrd, Hash)] +#[group(required = false, multiple = false)] +struct Editor { + #[arg(long, short = 'c')] + code: bool, + #[arg(long, short = 'z')] + zed: bool, +} + +fn main() -> Result<(), ()> { + let args = Cli::parse(); + let mapping = match Mapping::load() { + Ok(val) => val, + Err(err) => { + eprintln!("Error loading mapping: {}", err); + return Err(()); + } + }; + + let Some(target) = mapping.get(&args.dir) else { + eprintln!("{} is not a recognized directory in fld.mapping", args.dir); + return Err(()); + }; + + println!("cd {:?}", target); + + if let Some(editor) = args.editor { + if editor.code { + println!("code ."); + } else { + println!("zed ."); + }; + }; + + if let Some(subdirs) = args.subdirs { + let paths = subdirs.into_iter().map(|x| PathBuf::from(x)); + let mut path = PathBuf::from("./"); + for entry in paths { + path.push(entry); + } + println!("cd {:?}", path); + }; + + Ok(()) } diff --git a/src/mapping.rs b/src/mapping.rs new file mode 100644 index 0000000..cb62e11 --- /dev/null +++ b/src/mapping.rs @@ -0,0 +1,77 @@ +use std::{ + collections::HashMap, + env, + fs::File, + io::Read, + path::{Path, PathBuf}, + sync::LazyLock, +}; + +use thiserror::Error; + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct Mapping { + internal: HashMap, +} + +#[derive(Error, Debug)] +pub enum Error { + #[error("IO Error: {}", .0)] + IO(#[from] std::io::Error), +} + +static HOME: LazyLock = + LazyLock::new(|| PathBuf::from(env::var("HOME").unwrap_or(String::from("/")))); + +static XDG_CONFIG_HOME: LazyLock = LazyLock::new(|| { + env::var("XDG_CONFIG_HOME") + .map(|x| PathBuf::from(x)) + .unwrap_or(HOME.clone()) +}); + +fn create_config_file() -> Result { + let path: PathBuf = XDG_CONFIG_HOME.join(Path::new("fld.mapping")); + File::create(path) +} + +impl Mapping { + pub fn load() -> Result { + let path: PathBuf = XDG_CONFIG_HOME.join(Path::new("fld.mapping")); + let mut file = File::open(path) + .map(|ok| Ok(ok)) + .unwrap_or_else(|_| create_config_file())?; + let mut str = String::new(); + file.read_to_string(&mut str)?; + + let pairs = str.lines(); + let pairs = pairs.map(|x| x.split("\t").filter(|x| x != &"").collect::>()); + + let pairs = pairs.filter_map(|x| { + if x.len() == 2 { + Some((x[0], x[1])) + } else { + None + } + }); + + let pairs = pairs.map(|(k, v)| { + ( + String::from(k), + if v.starts_with("~") { + PathBuf::from(String::from(HOME.to_string_lossy()) + &v[1..]) + } else { + PathBuf::from(v) + }, + ) + }); + + let pairs = pairs.map(|(k, v)| (k.clone(), v.canonicalize().unwrap_or(v))); + let pairs = pairs.collect(); + + Ok(Mapping { internal: pairs }) + } + + pub fn get(&self, key: &str) -> Option<&PathBuf> { + self.internal.get(&String::from(key)) + } +}