Skip to content

Commit f7524ef

Browse files
authored
docs: clarify YAML feature behavior (#26)
1 parent b118ae8 commit f7524ef

5 files changed

Lines changed: 74 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ When the `json5` feature is active, both `.json` and `.json5` files are parsed
147147
using the JSON5 format. Standard JSON is valid JSON5, so existing `.json` files
148148
continue to work. Without this feature enabled, attempting to load a `.json` or
149149
`.json5` file will result in an error.
150+
When the `yaml` feature is enabled, `.yaml` and `.yml` files are also discovered and parsed. Without this feature, those extensions are ignored during path discovery.
150151

151152
JSON5 extends JSON with conveniences such as comments, trailing commas,
152153
single-quoted strings, and unquoted keys.

ortho_config/src/subcommand.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ fn candidate_paths(prefix: &str) -> Vec<PathBuf> {
1313

1414
if let Some(home) = std::env::var_os("HOME") {
1515
let home = PathBuf::from(home);
16-
for ext in ["toml", "json5"] {
16+
paths.push(home.join(format!(".{base}.toml")));
17+
#[cfg(feature = "json5")]
18+
for ext in ["json", "json5"] {
19+
paths.push(home.join(format!(".{base}.{ext}")));
20+
}
21+
#[cfg(feature = "yaml")]
22+
for ext in ["yaml", "yml"] {
1723
paths.push(home.join(format!(".{base}.{ext}")));
1824
}
1925
}
@@ -23,13 +29,29 @@ fn candidate_paths(prefix: &str) -> Vec<PathBuf> {
2329
} else {
2430
BaseDirectories::with_prefix(&base)
2531
};
26-
for ext in ["toml", "json5"] {
32+
if let Some(p) = xdg_dirs.find_config_file("config.toml") {
33+
paths.push(p);
34+
}
35+
#[cfg(feature = "json5")]
36+
for ext in ["json", "json5"] {
37+
if let Some(p) = xdg_dirs.find_config_file(format!("config.{ext}")) {
38+
paths.push(p);
39+
}
40+
}
41+
#[cfg(feature = "yaml")]
42+
for ext in ["yaml", "yml"] {
2743
if let Some(p) = xdg_dirs.find_config_file(format!("config.{ext}")) {
2844
paths.push(p);
2945
}
3046
}
3147

32-
for ext in ["toml", "json5"] {
48+
paths.push(PathBuf::from(format!(".{base}.toml")));
49+
#[cfg(feature = "json5")]
50+
for ext in ["json", "json5"] {
51+
paths.push(PathBuf::from(format!(".{base}.{ext}")));
52+
}
53+
#[cfg(feature = "yaml")]
54+
for ext in ["yaml", "yml"] {
3355
paths.push(PathBuf::from(format!(".{base}.{ext}")));
3456
}
3557

ortho_config/tests/clap_integration.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,19 @@ fn loads_from_xdg_config() {
134134
Ok(())
135135
});
136136
}
137+
138+
#[cfg(feature = "yaml")]
139+
#[test]
140+
fn loads_from_xdg_yaml_config() {
141+
figment::Jail::expect_with(|j| {
142+
let dir = j.create_dir("xdg_yaml")?;
143+
let abs = std::fs::canonicalize(&dir).unwrap();
144+
j.create_file(dir.join("config.yaml"), "sample_value: xdg\nother: val")?;
145+
j.set_env("XDG_CONFIG_HOME", abs.to_str().unwrap());
146+
147+
let cfg = TestConfig::load_from_iter(["prog"]).expect("load");
148+
assert_eq!(cfg.sample_value, "xdg");
149+
assert_eq!(cfg.other, "val");
150+
Ok(())
151+
});
152+
}

ortho_config/tests/subcommand.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ fn loads_from_xdg_config() {
5757
Ok(())
5858
});
5959
}
60+
61+
#[cfg(feature = "yaml")]
62+
#[test]
63+
fn loads_yaml_file() {
64+
figment::Jail::expect_with(|j| {
65+
j.create_file(".app.yml", "cmds:\n test:\n foo: yaml")?;
66+
let cfg: CmdCfg = load_subcommand_config("APP_", "test").expect("load");
67+
assert_eq!(cfg.foo.as_deref(), Some("yaml"));
68+
Ok(())
69+
});
70+
}

ortho_config_macros/src/derive/build.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,27 @@ pub(crate) fn build_xdg_snippet(struct_attrs: &StructAttrs) -> proc_macro2::Toke
114114
} else {
115115
xdg::BaseDirectories::with_prefix(&xdg_base)
116116
};
117-
for ext in &["toml", "json5"] {
118-
let filename = format!("config.{}", ext);
119-
if let Some(p) = xdg_dirs.find_config_file(&filename) {
120-
file_fig = ortho_config::load_config_file(&p)?;
121-
break;
117+
if let Some(p) = xdg_dirs.find_config_file("config.toml") {
118+
file_fig = ortho_config::load_config_file(&p)?;
119+
}
120+
#[cfg(feature = "json5")]
121+
if file_fig.is_none() {
122+
for ext in &["json", "json5"] {
123+
let filename = format!("config.{}", ext);
124+
if let Some(p) = xdg_dirs.find_config_file(&filename) {
125+
file_fig = ortho_config::load_config_file(&p)?;
126+
break;
127+
}
128+
}
129+
}
130+
#[cfg(feature = "yaml")]
131+
if file_fig.is_none() {
132+
for ext in &["yaml", "yml"] {
133+
let filename = format!("config.{}", ext);
134+
if let Some(p) = xdg_dirs.find_config_file(&filename) {
135+
file_fig = ortho_config::load_config_file(&p)?;
136+
break;
137+
}
122138
}
123139
}
124140
}

0 commit comments

Comments
 (0)