1use super::*;
2
3#[derive(Debug, Default, Deserialize)]
4pub struct MergeConfig {
5 pub inputs: Vec<Input>,
6 pub output: PathBuf,
7 #[serde(skip)]
8 pub source: PathBuf,
9 #[serde(skip)]
10 pub load_time: time::Duration,
11 #[serde(skip)]
12 pub merged: OpenAPI,
13 #[serde(skip)]
14 pub merge_time: time::Duration,
15 #[serde(skip)]
16 pub save_time: time::Duration,
17}
18
19impl MergeConfig {
20 pub fn from_path(path: impl AsRef<Path>) -> io::Result<Self> {
21 let now = time::Instant::now();
22 let source = path.as_ref().to_path_buf();
23 let config = load_json_file(path)?;
24 let load_time = now.elapsed();
25
26 Ok(Self {
27 source,
28 load_time,
29 ..config
30 })
31 }
32
33 pub fn into_merge(self) -> Merge {
34 self.into()
35 }
36
37 pub fn load_inputs(self) -> io::Result<Self> {
38 let base = self.source.parent().unwrap_or(Path::new("."));
39
40 self.inputs
41 .into_iter()
42 .map(|input| input.load(base))
43 .collect::<io::Result<Vec<_>>>()
44 .map(|inputs| Self { inputs, ..self })
45 }
46
47 pub fn output(&self) -> PathBuf {
48 self.source
49 .parent()
50 .unwrap_or(Path::new("."))
51 .join(&self.output)
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 use pretty_assertions::assert_eq;
60
61 const CONFIG: &str = r#"
62{
63 "inputs": [
64 {
65 "inputFile": "./base/openapi.json",
66 "operationSelection": {
67 "excludeTags": [
68 "deprecated"
69 ]
70 }
71 },
72 {
73 "inputFile": "./mod1/openapi.json"
74 },
75 {
76 "inputFile": "./mod2/openapi.json"
77 }
78 ],
79 "output": "./rest-api/user-facing-openapi.json"
80}
81"#;
82 #[test]
83 fn merge_config_deserialize() {
84 let config: MergeConfig = json::from_str(CONFIG).unwrap();
85 assert_eq!(config.inputs.len(), 3);
86 }
87}