Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform tilde expansion on output page (from config) #51

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub fn new() -> eyre::Result<BaseDirs> {
Ok(BaseDirs)
}

pub fn home_dir() -> Option<PathBuf> {
::dirs::home_dir()
}

impl BaseDirs {
pub fn place_config_file<P: AsRef<Path>>(&self, path: P) -> eyre::Result<PathBuf> {
::dirs::config_dir()
Expand Down
73 changes: 70 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,24 @@ async fn try_main() -> eyre::Result<bool> {

let config = Config::read(cli.config_path)?;

// Ensure output directory exists
let output_dir = cli.output_path.or_else(|| config.rsspls.output.map(|ref path| PathBuf::from(path)))
.ok_or_else(|| eyre!("output directory must be supplied via --output or be present in configuration file"))?;
// Determine output directory
let output_dir = match cli.output_path {
Some(path) => Some(path),
None => config
.rsspls
.output
.map(|ref path| {
dirs::home_dir()
.ok_or_else(|| eyre!("unable to determine home directory"))
.map(|home| expand_tilde(path, home))
})
.transpose()?,
}
.ok_or_else(|| {
eyre!("output directory must be supplied via --output or be present in configuration file")
})?;

// Ensure output directory exists
if !output_dir.exists() {
fs::create_dir_all(&output_dir).wrap_err_with(|| {
format!(
Expand Down Expand Up @@ -204,3 +218,56 @@ pub fn version_string() -> String {
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

fn expand_tilde<P: Into<PathBuf>>(path: P, mut home: PathBuf) -> PathBuf {
let path = path.into();

// NOTE: starts_with only considers whole path components
if path.starts_with("~") {
if path == Path::new("~") {
home
} else {
home.push(path.strip_prefix("~").unwrap());
home
}
} else {
path
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(not(windows))]
fn test_home() {
let expanded = expand_tilde("asdf", PathBuf::from("/home/foo"));
assert_eq!(expanded, Path::new("asdf"));

let expanded = expand_tilde("~asdf", PathBuf::from("/home/foo"));
assert_eq!(expanded, Path::new("~asdf"));

let expanded = expand_tilde("~/some/where", PathBuf::from("/home/foo"));
assert_eq!(expanded, Path::new("/home/foo/some/where"));

let expanded = expand_tilde("~/some/where", PathBuf::from("/"));
assert_eq!(expanded, Path::new("/some/where"));
}

#[test]
#[cfg(windows)]
fn test_home_windows() {
let expanded = expand_tilde("asdf", PathBuf::from(r"C:\Users\Foo"));
assert_eq!(expanded, Path::new("asdf"));

let expanded = expand_tilde("~asdf", PathBuf::from(r"C:\Users\Foo"));
assert_eq!(expanded, Path::new("~asdf"));

let expanded = expand_tilde(r"~\some\where", PathBuf::from(r"C:\Users\Foo"));
assert_eq!(expanded, Path::new(r"C:\Users\Foo\some\where"));

let expanded = expand_tilde(r"~\some\where", PathBuf::from(r"C:\"));
assert_eq!(expanded, Path::new(r"C:\some\where"));
}
}
12 changes: 11 additions & 1 deletion src/xdg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::{Arc, Mutex};
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};

use eyre::WrapErr;
use simple_eyre::eyre;
Expand All @@ -9,3 +12,10 @@ pub fn new() -> eyre::Result<xdg::BaseDirectories> {
xdg::BaseDirectories::with_prefix("rsspls")
.wrap_err("unable to determine home directory of current user")
}

pub fn home_dir() -> Option<PathBuf> {
// This module only supports Unix, and the behavior of `std::env::home_dir()` is only
// problematic on Windows.
#[allow(deprecated)]
std::env::home_dir()
}