1use std::path::Path;
27
28use anyhow::{Context, bail};
29use serde::Deserialize;
30
31use crate::transcoder::TargetEncoding;
32
33#[derive(Debug, Clone, Default, Deserialize)]
35#[serde(deny_unknown_fields)]
36pub struct ForgeConfig {
37 #[serde(default)]
39 pub filter: FilterSection,
40 #[serde(default)]
42 pub convert: ConvertSection,
43 #[serde(default)]
45 pub options: OptionsSection,
46}
47
48#[derive(Debug, Clone, Default, Deserialize)]
50#[serde(deny_unknown_fields)]
51pub struct FilterSection {
52 #[serde(default)]
57 pub extensions: Option<Vec<String>>,
58 #[serde(default)]
59 pub exclude_dirs: Vec<String>,
60 #[serde(default)]
61 pub exclude_ext: Vec<String>,
62 #[serde(default)]
63 pub exclude_globs: Vec<String>,
64 #[serde(default)]
65 pub include_rc: bool,
66 #[serde(default)]
67 pub include_msbuild: bool,
68 #[serde(default)]
69 pub include_hidden: bool,
70}
71
72#[derive(Debug, Clone, Default, Deserialize)]
74#[serde(deny_unknown_fields)]
75pub struct ConvertSection {
76 #[serde(default)]
77 pub target: Option<String>,
78 #[serde(default)]
81 pub backup_dir: Option<String>,
82}
83
84#[derive(Debug, Clone, Default, Deserialize)]
86#[serde(deny_unknown_fields)]
87pub struct OptionsSection {
88 #[serde(default)]
89 pub max_file_size: Option<u64>,
90 #[serde(default)]
91 pub threads: Option<usize>,
92}
93
94pub fn load(path: &Path) -> anyhow::Result<ForgeConfig> {
97 if !path.is_file() {
98 bail!("config file `{}` not found", path.display());
99 }
100 let text = std::fs::read_to_string(path)
101 .with_context(|| format!("cannot read config file `{}`", path.display()))?;
102 let cfg: ForgeConfig = toml::from_str(&text)
103 .with_context(|| format!("config file `{}` is not valid", path.display()))?;
104 Ok(cfg)
105}
106
107impl ForgeConfig {
108 pub fn target(&self) -> anyhow::Result<Option<TargetEncoding>> {
111 match &self.convert.target {
112 None => Ok(None),
113 Some(raw) => TargetEncoding::parse(raw).map(Some).ok_or_else(|| {
114 anyhow::anyhow!(
115 "config [convert].target: invalid target encoding `{raw}` (expected utf-8 | utf-8-bom | gb18030 | utf-16le | utf-16be)"
116 )
117 }),
118 }
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[test]
127 fn parses_full_document() {
128 let cfg: ForgeConfig = toml::from_str(
129 r#"
130[filter]
131extensions = ["c", "cpp"]
132exclude_dirs = ["legacy"]
133include_rc = true
134
135[convert]
136target = "utf-16le"
137backup_dir = "/tmp/bk"
138
139[options]
140max_file_size = 1024
141threads = 4
142"#,
143 )
144 .unwrap();
145 assert_eq!(
146 cfg.filter.extensions,
147 Some(vec!["c".to_string(), "cpp".to_string()])
148 );
149 assert!(cfg.filter.include_rc);
150 assert_eq!(cfg.options.max_file_size, Some(1024));
151 assert_eq!(cfg.target().unwrap(), Some(TargetEncoding::Utf16Le));
152 }
153
154 #[test]
155 fn empty_document_is_valid() {
156 let cfg: ForgeConfig = toml::from_str("").unwrap();
157 assert!(cfg.filter.extensions.is_none());
158 assert_eq!(cfg.target().unwrap(), None);
159 }
160
161 #[test]
162 fn unknown_keys_are_rejected() {
163 let err = toml::from_str::<ForgeConfig>(
164 "[filter]
165typo_key = 1
166",
167 )
168 .unwrap_err();
169 assert!(err.to_string().contains("typo_key"), "{err}");
170 }
171
172 #[test]
173 fn invalid_target_value_is_rejected_with_key_name() {
174 let cfg: ForgeConfig = toml::from_str("[convert]\ntarget = \"gbk\"\n").unwrap();
175 let err = cfg.target().unwrap_err().to_string();
176 assert!(err.contains("[convert].target"), "{err}");
177 }
178}