Skip to content

Commit 0d982cc

Browse files
committed
Remove directories crate for dirs and introduce xdg feature to allow configs to be stored in xdg defaults
1 parent a41f72d commit 0d982cc

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ edition = "2024"
1111

1212
[dependencies]
1313
ron = { version = "0.10.1", optional = true }
14-
directories = "6"
14+
etcetera = "0.10.0"
1515
serde = "^1.0"
1616
serde_yaml = { version = "0.9", optional = true }
1717
thiserror = "2.0"
@@ -24,6 +24,7 @@ toml_conf = ["toml"]
2424
basic_toml_conf = ["basic-toml"]
2525
yaml_conf = ["serde_yaml"]
2626
ron_conf = ["ron"]
27+
xdg = []
2728

2829
[[example]]
2930
name = "simple"

examples/simple.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() -> Result<(), confy::ConfyError> {
4141
name: "Test".to_string(),
4242
..cfg
4343
};
44-
confy::store("confy_simple_app",None, &cfg)?;
44+
confy::store("confy_simple_app", None, &cfg)?;
4545
println!("The updated toml file content is:");
4646
let mut content = String::new();
4747
std::fs::File::open(&file)
@@ -53,7 +53,6 @@ fn main() -> Result<(), confy::ConfyError> {
5353
name: "Test".to_string(),
5454
..cfg
5555
};
56-
std::fs::remove_dir_all(file.parent().unwrap())
57-
.expect("Failed to remove directory");
56+
std::fs::remove_dir_all(file.parent().unwrap()).expect("Failed to remove directory");
5857
Ok(())
5958
}

src/lib.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
//!
7474
//! ### Tip
7575
//! to add this crate to your project with the default, toml config do the following: `cargo add confy`, otherwise do something like: `cargo add confy --no-default-features --features yaml_conf`, for more info, see [cargo docs on features]
76-
//!
76+
//!
7777
//! [cargo docs on features]: https://docs.rust-lang.org/cargo/reference/resolver.html#features
78-
//!
78+
//!
7979
//! feature | file format | description
8080
//! ------- | ----------- | -----------
8181
//! **default**: `toml_conf` | [toml] | considered a reasonable default, uses the standard-compliant [`toml` crate]
@@ -94,8 +94,12 @@
9494
mod utils;
9595
use utils::*;
9696

97-
use directories::ProjectDirs;
98-
use serde::{de::DeserializeOwned, Serialize};
97+
#[cfg(feature = "xdg")]
98+
use etcetera::app_strategy::choose_app_strategy;
99+
#[cfg(not(feature = "xdg"))]
100+
use etcetera::app_strategy::choose_native_strategy;
101+
use etcetera::{AppStrategy, AppStrategyArgs};
102+
use serde::{Serialize, de::DeserializeOwned};
99103
use std::fs::{self, File, OpenOptions, Permissions};
100104
use std::io::{ErrorKind::NotFound, Write};
101105
use std::path::{Path, PathBuf};
@@ -109,8 +113,8 @@ use toml::{
109113

110114
#[cfg(feature = "basic_toml_conf")]
111115
use basic_toml::{
112-
from_str as toml_from_str, to_string as toml_to_string_pretty, Error as TomlDeErr,
113-
Error as TomlSerErr,
116+
Error as TomlDeErr, Error as TomlSerErr, from_str as toml_from_str,
117+
to_string as toml_to_string_pretty,
114118
};
115119

116120
#[cfg(not(any(
@@ -469,23 +473,49 @@ pub fn get_configuration_file_path<'a>(
469473
config_name: impl Into<Option<&'a str>>,
470474
) -> Result<PathBuf, ConfyError> {
471475
let config_name = config_name.into().unwrap_or("default-config");
472-
let project = ProjectDirs::from("rs", "", app_name).ok_or_else(|| {
473-
ConfyError::BadConfigDirectory("could not determine home directory path".to_string())
474-
})?;
476+
let project;
477+
478+
#[cfg(not(feature = "xdg"))]
479+
{
480+
project = choose_native_strategy(AppStrategyArgs {
481+
top_level_domain: "rs".to_string(),
482+
author: "".to_string(),
483+
app_name: app_name.to_string(),
484+
})
485+
.map_err(|e| {
486+
ConfyError::BadConfigDirectory(format!("could not determine home directory path: {e}"))
487+
})?;
488+
}
489+
490+
#[cfg(feature = "xdg")]
491+
{
492+
project = choose_app_strategy(AppStrategyArgs {
493+
top_level_domain: "rs".to_string(),
494+
author: "".to_string(),
495+
app_name: app_name.to_string(),
496+
})
497+
.map_err(|e| {
498+
ConfyError::BadConfigDirectory(format!("could not determine home directory path: {e}"))
499+
})?;
500+
}
475501

476502
let config_dir_str = get_configuration_directory_str(&project)?;
477503

478-
let path = [config_dir_str, &format!("{config_name}.{EXTENSION}")]
504+
let path = [config_dir_str, format!("{config_name}.{EXTENSION}")]
479505
.iter()
480506
.collect();
481507

482508
Ok(path)
483509
}
484510

485-
fn get_configuration_directory_str(project: &ProjectDirs) -> Result<&str, ConfyError> {
486-
let path = project.config_dir();
487-
path.to_str()
488-
.ok_or_else(|| ConfyError::BadConfigDirectory(format!("{path:?} is not valid Unicode")))
511+
fn get_configuration_directory_str<'a>(project: &impl AppStrategy) -> Result<String, ConfyError> {
512+
let path = if cfg!(feature = "xdg") {
513+
project.config_dir()
514+
} else {
515+
project.data_dir()
516+
};
517+
518+
Ok(format!("{}", path.display()))
489519
}
490520

491521
#[cfg(test)]
@@ -601,10 +631,12 @@ mod tests {
601631

602632
store_path_perms(path, &config, permissions).expect("store_path_perms failed");
603633

604-
assert!(fs::metadata(path)
605-
.expect("reading metadata failed")
606-
.permissions()
607-
.readonly());
634+
assert!(
635+
fs::metadata(path)
636+
.expect("reading metadata failed")
637+
.permissions()
638+
.readonly()
639+
);
608640
})
609641
}
610642

0 commit comments

Comments
 (0)