gesha_core/conversions/
format_errors.rs

1use crate::{Error, Output};
2
3pub fn format_errors<A>(output: Output<A>) -> Option<String> {
4    let (_, errors) = output.into_tuple();
5    if errors.is_empty() {
6        return None;
7    }
8    let errors = errors
9        .into_iter()
10        .flat_map(format_core_error)
11        .collect::<Vec<_>>();
12
13    let lines = errors.join("\n").to_string();
14    Some(lines)
15}
16
17fn format_core_error(err: Error) -> Vec<String> {
18    let mut lines = vec![];
19    match err {
20        Error::OpenApiTypes { path, cause } => {
21            lines.push(format!("path: {}", path.to_string_lossy()));
22            lines.append(&mut oas::format_error(cause, vec![]));
23        }
24        Error::Conversion { path, cause } => {
25            lines.push(format!("path: {}", path.to_string_lossy()));
26            lines.append(&mut conv::format_error(cause, vec![]));
27        }
28        Error::Multiple(errors) => {
29            let mut formatted = errors.into_iter().flat_map(format_core_error).collect();
30            lines.append(&mut formatted);
31        }
32        e => lines.push(format!("{:#?}", e)),
33    }
34    lines.push("".to_string());
35    lines
36}
37
38macro_rules! generate {
39    () => {
40        pub fn format_error(err: Error, mut keys: Vec<String>) -> Vec<String> {
41            let mut lines = vec![];
42            match err {
43                Error::Multiple(causes) => {
44                    let mut next_lines = causes
45                        .into_iter()
46                        .flat_map(|e| format_error(e, keys.clone()))
47                        .collect();
48
49                    lines.append(&mut next_lines)
50                }
51                Error::Enclosed { key, cause } => {
52                    keys.push(key);
53                    let mut next_lines = format_error(*cause, keys);
54                    lines.append(&mut next_lines)
55                }
56                _ => {
57                    lines.push(format!("\n    @ {}", keys.join(" > ")));
58
59                    let mut next_lines = format!("{:#?}\n", err)
60                        .lines()
61                        .map(|line| format!("    {}", line))
62                        .collect::<Vec<_>>();
63
64                    lines.append(&mut next_lines);
65                }
66            }
67            lines
68        }
69    };
70}
71
72mod oas {
73    use openapi_types::Error;
74    generate!();
75}
76
77mod conv {
78    use crate::conversions::Error;
79    generate!();
80}