1#[cfg(feature = "cmd")]
5pub mod cmd;
6#[cfg(feature = "env")]
7pub mod env;
8#[cfg(feature = "json")]
9pub mod json;
10#[cfg(feature = "json5-parser")]
11pub mod json5;
12#[cfg(test)]
13mod tests;
14#[cfg(feature = "toml-parser")]
15pub mod toml;
16#[cfg(feature = "yaml")]
17pub mod yaml;
18
19use crate::{AnyResult, Case, Parse, Value, DEFAULT_KEYS_SEPARATOR};
20use derive_builder::Builder;
21use std::{
22 borrow::Cow,
23 fs::File,
24 io::{BufReader, Error as IoError, Read},
25 path::{Path, PathBuf},
26 result::Result as StdResult,
27};
28
29pub type Result<T> = StdResult<T, Error>;
31
32type CowPath<'a> = Cow<'a, Path>;
33
34#[non_exhaustive]
36#[derive(thiserror::Error, Debug)]
37pub enum Error {
38 #[error("Failed to get file path by option: '{1}'")]
39 PathOption(#[source] crate::Error, String),
40 #[error("Failed to open file: '{1}'")]
41 Open(#[source] IoError, PathBuf),
42 #[error("Failed to get meta data for file: '{1}'")]
43 Meta(#[source] IoError, PathBuf),
44 #[error("Is not a file: '{0}'")]
45 NotAFile(PathBuf),
46}
47
48pub trait Load: Case {
50 fn load(&mut self, reader: impl Read) -> AnyResult<Value>;
56}
57
58#[derive(Builder)]
60#[builder(setter(into, strip_option))]
61pub struct FileParser<L: Load + Default> {
62 default_path: PathBuf,
64 #[builder(default = "None")]
66 path_option: Option<String>,
67 #[builder(default = "DEFAULT_KEYS_SEPARATOR.to_string()")]
69 keys_delimiter: String,
70 #[builder(default = "false")]
72 ignore_missing_file: bool,
73 #[builder(default)]
75 loader: L,
76}
77
78impl<L: Load + Default> Case for FileParser<L> {
79 #[inline]
80 fn is_case_sensitive(&self) -> bool {
81 self.loader.is_case_sensitive()
82 }
83}
84
85impl<L: Load + Default> Parse for FileParser<L> {
86 fn parse(&mut self, value: &Value) -> AnyResult<Value> {
87 let path = get_path(
88 value,
89 &self.path_option,
90 &self.default_path,
91 &self.keys_delimiter,
92 )?;
93
94 let file = match try_open_file(path.as_ref()) {
95 Ok(f) => f,
96 Err(_) if self.ignore_missing_file => return Ok(Value::default()),
97 Err(e) => return Err(e.into()),
98 };
99
100 self.loader.load(BufReader::new(file))
101 }
102}
103
104fn get_path<'a>(
105 value: &Value,
106 path_option: &Option<String>,
107 default: &'a Path,
108 delim: &str,
109) -> Result<CowPath<'a>> {
110 let default = default.into();
111 let Some(option) = path_option else {
112 return Ok(default);
113 };
114
115 let path: Option<String> = value
116 .get_by_key_path_with_delim(option, delim)
117 .map_err(|e| Error::PathOption(e, option.into()))?;
118 Ok(path.map_or(default, |p| PathBuf::from(p).into()))
119}
120
121fn try_open_file(path: &Path) -> Result<File> {
122 let file = File::open(path).map_err(|e| Error::Open(e, path.into()))?;
123 if file
124 .metadata()
125 .map_err(|e| Error::Meta(e, path.into()))?
126 .is_file()
127 {
128 return Ok(file);
129 }
130
131 Err(Error::NotAFile(path.into()))
132}