1use std::fs;
27use std::io::Read;
28use std::path::{Path, PathBuf};
29
30use serde::de::DeserializeOwned;
31use serde::Serialize;
32
33use crate::error::Error;
34use crate::Result;
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum ConfigFormat {
39 Yaml,
41 Json,
43 Toml,
45}
46
47impl ConfigFormat {
48 pub fn from_extension(ext: &str) -> Option<Self> {
50 match ext.to_lowercase().as_str() {
51 "yaml" | "yml" => Some(Self::Yaml),
52 "json" => Some(Self::Json),
53 "toml" => Some(Self::Toml),
54 _ => None,
55 }
56 }
57
58 pub fn from_path(path: impl AsRef<Path>) -> Option<Self> {
60 path.as_ref()
61 .extension()
62 .and_then(|e| e.to_str())
63 .and_then(Self::from_extension)
64 }
65
66 pub fn extension(&self) -> &'static str {
68 match self {
69 Self::Yaml => "yaml",
70 Self::Json => "json",
71 Self::Toml => "toml",
72 }
73 }
74
75 pub fn extensions(&self) -> &'static [&'static str] {
77 match self {
78 Self::Yaml => &["yaml", "yml"],
79 Self::Json => &["json"],
80 Self::Toml => &["toml"],
81 }
82 }
83
84 pub fn mime_type(&self) -> &'static str {
86 match self {
87 Self::Yaml => "application/x-yaml",
88 Self::Json => "application/json",
89 Self::Toml => "application/toml",
90 }
91 }
92}
93
94impl std::fmt::Display for ConfigFormat {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 match self {
97 Self::Yaml => write!(f, "YAML"),
98 Self::Json => write!(f, "JSON"),
99 Self::Toml => write!(f, "TOML"),
100 }
101 }
102}
103
104pub struct ConfigLoader;
106
107impl ConfigLoader {
108 pub fn load<T: DeserializeOwned>(path: impl AsRef<Path>) -> Result<T> {
122 let path = path.as_ref();
123 let format = ConfigFormat::from_path(path).ok_or_else(|| {
124 Error::Config(format!(
125 "Cannot determine format from file extension: {}",
126 path.display()
127 ))
128 })?;
129
130 Self::load_with_format(path, format)
131 }
132
133 pub fn load_with_format<T: DeserializeOwned>(
135 path: impl AsRef<Path>,
136 format: ConfigFormat,
137 ) -> Result<T> {
138 let path = path.as_ref();
139 let content = fs::read_to_string(path)?;
140 Self::parse(&content, format)
141 }
142
143 pub fn load_from_reader<T: DeserializeOwned, R: Read>(
145 reader: &mut R,
146 format: ConfigFormat,
147 ) -> Result<T> {
148 let mut content = String::new();
149 reader.read_to_string(&mut content)?;
150 Self::parse(&content, format)
151 }
152
153 pub fn parse<T: DeserializeOwned>(content: &str, format: ConfigFormat) -> Result<T> {
155 match format {
156 ConfigFormat::Yaml => {
157 serde_yaml::from_str(content).map_err(|e| Error::Serialization(e.to_string()))
158 }
159 ConfigFormat::Json => {
160 serde_json::from_str(content).map_err(|e| Error::Serialization(e.to_string()))
161 }
162 ConfigFormat::Toml => {
163 toml::from_str(content).map_err(|e| Error::Serialization(e.to_string()))
164 }
165 }
166 }
167
168 pub fn serialize<T: Serialize>(config: &T, format: ConfigFormat) -> Result<String> {
170 match format {
171 ConfigFormat::Yaml => {
172 serde_yaml::to_string(config).map_err(|e| Error::Serialization(e.to_string()))
173 }
174 ConfigFormat::Json => {
175 serde_json::to_string_pretty(config).map_err(|e| Error::Serialization(e.to_string()))
176 }
177 ConfigFormat::Toml => {
178 toml::to_string_pretty(config).map_err(|e| Error::Serialization(e.to_string()))
179 }
180 }
181 }
182
183 pub fn save<T: Serialize>(config: &T, path: impl AsRef<Path>) -> Result<()> {
185 let path = path.as_ref();
186 let format = ConfigFormat::from_path(path).ok_or_else(|| {
187 Error::Config(format!(
188 "Cannot determine format from file extension: {}",
189 path.display()
190 ))
191 })?;
192
193 Self::save_with_format(config, path, format)
194 }
195
196 pub fn save_with_format<T: Serialize>(
198 config: &T,
199 path: impl AsRef<Path>,
200 format: ConfigFormat,
201 ) -> Result<()> {
202 let content = Self::serialize(config, format)?;
203 fs::write(path, content)?;
204 Ok(())
205 }
206
207 pub fn load_first<T: DeserializeOwned>(paths: &[impl AsRef<Path>]) -> Result<(T, PathBuf)> {
218 let mut last_error = None;
219
220 for path in paths {
221 let path = path.as_ref();
222 if path.exists() {
223 match Self::load(path) {
224 Ok(config) => return Ok((config, path.to_path_buf())),
225 Err(e) => last_error = Some(e),
226 }
227 }
228 }
229
230 Err(last_error.unwrap_or_else(|| {
231 Error::Config("No configuration file found in any of the specified paths".to_string())
232 }))
233 }
234
235 pub fn is_supported(path: impl AsRef<Path>) -> bool {
237 ConfigFormat::from_path(path).is_some()
238 }
239}
240
241pub struct ConfigDiscovery {
243 base_name: String,
245 search_dirs: Vec<PathBuf>,
247 formats: Vec<ConfigFormat>,
249}
250
251impl ConfigDiscovery {
252 pub fn new(base_name: impl Into<String>) -> Self {
254 Self {
255 base_name: base_name.into(),
256 search_dirs: Vec::new(),
257 formats: vec![ConfigFormat::Yaml, ConfigFormat::Toml, ConfigFormat::Json],
258 }
259 }
260
261 pub fn search_dir(mut self, dir: impl Into<PathBuf>) -> Self {
263 self.search_dirs.push(dir.into());
264 self
265 }
266
267 pub fn search_dirs(mut self, dirs: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
269 self.search_dirs.extend(dirs.into_iter().map(Into::into));
270 self
271 }
272
273 pub fn formats(mut self, formats: Vec<ConfigFormat>) -> Self {
275 self.formats = formats;
276 self
277 }
278
279 pub fn with_standard_dirs(mut self, app_name: &str) -> Self {
287 self.search_dirs.push(PathBuf::from("."));
289
290 self.search_dirs.push(PathBuf::from("./config"));
292
293 if let Some(home) = dirs_home() {
295 self.search_dirs.push(home.join(".config").join(app_name));
296 }
297
298 #[cfg(unix)]
300 {
301 self.search_dirs.push(PathBuf::from("/etc").join(app_name));
302 }
303
304 self
305 }
306
307 pub fn candidates(&self) -> Vec<PathBuf> {
309 let mut candidates = Vec::new();
310
311 for dir in &self.search_dirs {
312 for format in &self.formats {
313 for ext in format.extensions() {
314 let path = dir.join(format!("{}.{}", self.base_name, ext));
315 candidates.push(path);
316 }
317 }
318 }
319
320 candidates
321 }
322
323 pub fn find(&self) -> Option<PathBuf> {
325 self.candidates().into_iter().find(|p| p.exists())
326 }
327
328 pub fn load<T: DeserializeOwned>(&self) -> Result<(T, PathBuf)> {
330 let candidates = self.candidates();
331 ConfigLoader::load_first(&candidates)
332 }
333}
334
335fn dirs_home() -> Option<PathBuf> {
337 #[cfg(unix)]
338 {
339 std::env::var("HOME").ok().map(PathBuf::from)
340 }
341 #[cfg(windows)]
342 {
343 std::env::var("USERPROFILE").ok().map(PathBuf::from)
344 }
345 #[cfg(not(any(unix, windows)))]
346 {
347 None
348 }
349}
350
351pub struct LayeredConfigBuilder<T> {
356 base: T,
357 loaded_from: Vec<PathBuf>,
358}
359
360impl<T: DeserializeOwned + Default + Clone> LayeredConfigBuilder<T> {
361 pub fn new() -> Self {
363 Self {
364 base: T::default(),
365 loaded_from: Vec::new(),
366 }
367 }
368
369 pub fn with_base(base: T) -> Self {
371 Self {
372 base,
373 loaded_from: Vec::new(),
374 }
375 }
376}
377
378impl<T: DeserializeOwned + Clone> LayeredConfigBuilder<T> {
379 pub fn load_optional(mut self, path: impl AsRef<Path>) -> Self {
381 let path = path.as_ref();
382 if path.exists() {
383 if let Ok(config) = ConfigLoader::load::<T>(path) {
384 self.base = config;
385 self.loaded_from.push(path.to_path_buf());
386 }
387 }
388 self
389 }
390
391 pub fn load(mut self, path: impl AsRef<Path>) -> Result<Self> {
393 let path = path.as_ref();
394 self.base = ConfigLoader::load(path)?;
395 self.loaded_from.push(path.to_path_buf());
396 Ok(self)
397 }
398
399 pub fn loaded_from(&self) -> &[PathBuf] {
401 &self.loaded_from
402 }
403
404 pub fn build(self) -> T {
406 self.base
407 }
408}
409
410impl<T: DeserializeOwned + Default + Clone> Default for LayeredConfigBuilder<T> {
411 fn default() -> Self {
412 Self::new()
413 }
414}
415
416#[cfg(test)]
417mod tests {
418 use super::*;
419 use serde::{Deserialize, Serialize};
420 use std::io::Cursor;
421
422 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
423 struct TestConfig {
424 name: String,
425 #[serde(default)]
426 count: u32,
427 }
428
429 #[test]
430 fn test_format_from_extension() {
431 assert_eq!(ConfigFormat::from_extension("yaml"), Some(ConfigFormat::Yaml));
432 assert_eq!(ConfigFormat::from_extension("yml"), Some(ConfigFormat::Yaml));
433 assert_eq!(ConfigFormat::from_extension("json"), Some(ConfigFormat::Json));
434 assert_eq!(ConfigFormat::from_extension("toml"), Some(ConfigFormat::Toml));
435 assert_eq!(ConfigFormat::from_extension("txt"), None);
436 }
437
438 #[test]
439 fn test_format_from_path() {
440 assert_eq!(
441 ConfigFormat::from_path("config.yaml"),
442 Some(ConfigFormat::Yaml)
443 );
444 assert_eq!(
445 ConfigFormat::from_path("/etc/app/config.toml"),
446 Some(ConfigFormat::Toml)
447 );
448 assert_eq!(
449 ConfigFormat::from_path("data.json"),
450 Some(ConfigFormat::Json)
451 );
452 assert_eq!(ConfigFormat::from_path("noext"), None);
453 }
454
455 #[test]
456 fn test_parse_yaml() {
457 let yaml = r#"
458name: test
459count: 42
460"#;
461 let config: TestConfig = ConfigLoader::parse(yaml, ConfigFormat::Yaml).unwrap();
462 assert_eq!(config.name, "test");
463 assert_eq!(config.count, 42);
464 }
465
466 #[test]
467 fn test_parse_json() {
468 let json = r#"{"name": "test", "count": 42}"#;
469 let config: TestConfig = ConfigLoader::parse(json, ConfigFormat::Json).unwrap();
470 assert_eq!(config.name, "test");
471 assert_eq!(config.count, 42);
472 }
473
474 #[test]
475 fn test_parse_toml() {
476 let toml = r#"
477name = "test"
478count = 42
479"#;
480 let config: TestConfig = ConfigLoader::parse(toml, ConfigFormat::Toml).unwrap();
481 assert_eq!(config.name, "test");
482 assert_eq!(config.count, 42);
483 }
484
485 #[test]
486 fn test_serialize_yaml() {
487 let config = TestConfig {
488 name: "test".to_string(),
489 count: 42,
490 };
491 let yaml = ConfigLoader::serialize(&config, ConfigFormat::Yaml).unwrap();
492 assert!(yaml.contains("name: test"));
493 }
494
495 #[test]
496 fn test_serialize_json() {
497 let config = TestConfig {
498 name: "test".to_string(),
499 count: 42,
500 };
501 let json = ConfigLoader::serialize(&config, ConfigFormat::Json).unwrap();
502 assert!(json.contains("\"name\": \"test\""));
503 }
504
505 #[test]
506 fn test_serialize_toml() {
507 let config = TestConfig {
508 name: "test".to_string(),
509 count: 42,
510 };
511 let toml = ConfigLoader::serialize(&config, ConfigFormat::Toml).unwrap();
512 assert!(toml.contains("name = \"test\""));
513 }
514
515 #[test]
516 fn test_load_from_reader() {
517 let yaml = "name: reader_test\ncount: 100\n";
518 let mut reader = Cursor::new(yaml);
519 let config: TestConfig =
520 ConfigLoader::load_from_reader(&mut reader, ConfigFormat::Yaml).unwrap();
521 assert_eq!(config.name, "reader_test");
522 assert_eq!(config.count, 100);
523 }
524
525 #[test]
526 fn test_is_supported() {
527 assert!(ConfigLoader::is_supported("config.yaml"));
528 assert!(ConfigLoader::is_supported("config.yml"));
529 assert!(ConfigLoader::is_supported("config.json"));
530 assert!(ConfigLoader::is_supported("config.toml"));
531 assert!(!ConfigLoader::is_supported("config.txt"));
532 assert!(!ConfigLoader::is_supported("config"));
533 }
534
535 #[test]
536 fn test_config_discovery_candidates() {
537 let discovery = ConfigDiscovery::new("config")
538 .search_dir("/etc/app")
539 .search_dir(".");
540
541 let candidates = discovery.candidates();
542
543 assert!(candidates.iter().any(|p| p.to_string_lossy().contains("config.yaml")));
545 assert!(candidates.iter().any(|p| p.to_string_lossy().contains("config.toml")));
546 assert!(candidates.iter().any(|p| p.to_string_lossy().contains("config.json")));
547 }
548
549 #[test]
550 fn test_layered_config_builder() {
551 let config = LayeredConfigBuilder::<TestConfig>::new().build();
552 assert_eq!(config.name, "");
553 assert_eq!(config.count, 0);
554 }
555
556 #[test]
557 fn test_format_display() {
558 assert_eq!(format!("{}", ConfigFormat::Yaml), "YAML");
559 assert_eq!(format!("{}", ConfigFormat::Json), "JSON");
560 assert_eq!(format!("{}", ConfigFormat::Toml), "TOML");
561 }
562
563 #[test]
564 fn test_format_mime_type() {
565 assert_eq!(ConfigFormat::Yaml.mime_type(), "application/x-yaml");
566 assert_eq!(ConfigFormat::Json.mime_type(), "application/json");
567 assert_eq!(ConfigFormat::Toml.mime_type(), "application/toml");
568 }
569}