From d8659e277ed7ef758915aa9adf3ac1e7a0dfca20 Mon Sep 17 00:00:00 2001 From: Joshua Ferguson Date: Sat, 30 Dec 2023 17:32:31 -0600 Subject: [PATCH] handle home directory expansion --- Cargo.lock | 98 ++++++++++++++++++++++++++++++++++++++++++++++++------ Cargo.toml | 1 + src/lib.rs | 26 ++++++++++++--- 3 files changed, 110 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bff233..b7c2478 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,7 +131,7 @@ checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" dependencies = [ "errno-dragonfly", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -315,6 +315,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "ignore" version = "0.4.21" @@ -408,6 +417,7 @@ version = "23.12.0" dependencies = [ "eyre", "gumdrop", + "home", "ignore", "lazy_static", "matter", @@ -618,7 +628,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -754,7 +764,7 @@ dependencies = [ "fastrand", "redox_syscall", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -915,7 +925,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", ] [[package]] @@ -924,13 +943,28 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] [[package]] @@ -939,42 +973,84 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + [[package]] name = "yansi" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 50cebe0..7ff3829 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ doc = false [dependencies] eyre = "0.6.9" gumdrop = "0.8.1" +home = "0.5.9" ignore = "0.4.21" lazy_static = "1.4.0" matter = "0.1.0-alpha4" diff --git a/src/lib.rs b/src/lib.rs index bb3f7e5..d9dce08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,12 +23,13 @@ use rayon::prelude::*; use references::*; use slug::slugify; use snafu::{ResultExt, Snafu}; -use std::ffi::OsString; +use std::borrow::Cow; +use std::ffi::{OsStr, OsString}; use std::fmt; use std::fs::{self, File}; use std::io::prelude::*; use std::io::ErrorKind; -use std::path::{Path, PathBuf}; +use std::path::{Component, Path, PathBuf}; use std::str; use unicode_normalization::UnicodeNormalization; @@ -266,9 +267,11 @@ impl<'a> Exporter<'a> { /// Create a new exporter which reads notes from `root` and exports these to /// `destination`. pub fn new(root: PathBuf, destination: PathBuf) -> Exporter<'a> { + let root = expand_home_dir(root); + let destination = expand_home_dir(destination); Exporter { start_at: root.clone(), - root, + root: root, destination, frontmatter_strategy: FrontmatterStrategy::Auto, walk_options: WalkOptions::default(), @@ -327,7 +330,7 @@ impl<'a> Exporter<'a> { /// Export notes using the settings configured on this exporter. pub fn run(&mut self) -> Result<()> { - if !self.root.exists() { + if !self.root.canonicalize().unwrap().exists() { return Err(ExportError::PathDoesNotExist { path: self.root.clone(), }); @@ -889,6 +892,21 @@ fn codeblock_kind_to_owned<'a>(codeblock_kind: CodeBlockKind) -> CodeBlockKind<' } } +/// Handles ~ +fn expand_home_dir<'a, P: Into>(path: P) -> PathBuf { + let path = path.into(); + + if !path.starts_with("~") { + return path; + } + + lazy_static! { + static ref HOME_DIR: PathBuf = home::home_dir().expect("Unable to find home directory"); + } + + HOME_DIR.join(path.strip_prefix("~").unwrap()) +} + #[cfg(test)] mod tests { use super::*;