breadshot: expand ~ in save_dir, align Cargo.toml version with tag
- config.rs: expand a leading ~ in the save_dir config value on load. The documented example config (save_dir = "~/Pictures/Screenshots") was previously taken literally, silently creating ./~/Pictures/... in the current working directory. Added unit tests. - Cargo.toml: 1.0.0 -> 0.1.0, matching the only existing tag (v0.1.0). breadshot --version previously reported nine releases ahead of the actual release history.
This commit is contained in:
parent
1fb9770a77
commit
08c27d1b22
3 changed files with 59 additions and 4 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -84,7 +84,7 @@ checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "breadshot"
|
name = "breadshot"
|
||||||
version = "1.0.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "breadshot"
|
name = "breadshot"
|
||||||
version = "1.0.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
authors = ["Breadway"]
|
authors = ["Breadway"]
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,10 @@ impl Config {
|
||||||
}
|
}
|
||||||
let content = std::fs::read_to_string(path)
|
let content = std::fs::read_to_string(path)
|
||||||
.with_context(|| format!("reading {}", path.display()))?;
|
.with_context(|| format!("reading {}", path.display()))?;
|
||||||
toml::from_str(&content)
|
let mut config: Self = toml::from_str(&content)
|
||||||
.with_context(|| format!("parsing {}", path.display()))
|
.with_context(|| format!("parsing {}", path.display()))?;
|
||||||
|
config.save_dir = expand_tilde(config.save_dir);
|
||||||
|
Ok(config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,3 +51,56 @@ pub fn default_path() -> PathBuf {
|
||||||
.join("breadshot")
|
.join("breadshot")
|
||||||
.join("config.toml")
|
.join("config.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Expand a leading `~` (or `~/...`) to the user's home directory, the way a
|
||||||
|
/// shell would. `PathBuf`'s `Deserialize` does no such expansion, so a
|
||||||
|
/// documented config value like `save_dir = "~/Pictures/Screenshots"` would
|
||||||
|
/// otherwise be taken literally and create a `./~/Pictures/Screenshots`
|
||||||
|
/// directory relative to the current working directory.
|
||||||
|
fn expand_tilde(path: PathBuf) -> PathBuf {
|
||||||
|
let Some(s) = path.to_str() else {
|
||||||
|
return path;
|
||||||
|
};
|
||||||
|
if s == "~" {
|
||||||
|
return dirs::home_dir().unwrap_or(path);
|
||||||
|
}
|
||||||
|
if let Some(rest) = s.strip_prefix("~/") {
|
||||||
|
if let Some(home) = dirs::home_dir() {
|
||||||
|
return home.join(rest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expand_tilde_prefix() {
|
||||||
|
let home = dirs::home_dir().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
expand_tilde(PathBuf::from("~/Pictures/Screenshots")),
|
||||||
|
home.join("Pictures/Screenshots")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expand_tilde_bare() {
|
||||||
|
let home = dirs::home_dir().unwrap();
|
||||||
|
assert_eq!(expand_tilde(PathBuf::from("~")), home);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expand_tilde_absolute_untouched() {
|
||||||
|
let p = PathBuf::from("/var/tmp/shots");
|
||||||
|
assert_eq!(expand_tilde(p.clone()), p);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expand_tilde_no_expansion_mid_path() {
|
||||||
|
// Only a leading ~ is special, matching shell behavior.
|
||||||
|
let p = PathBuf::from("/home/user/~weird");
|
||||||
|
assert_eq!(expand_tilde(p.clone()), p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue