nv_redfish_csdl_compiler/
error.rs1use crate::compiler::Error as CompileError;
17use crate::edmx::attribute_values::Error as AttributeValuesError;
18use crate::edmx::ValidateError;
19use crate::generator::rust::Error as GenerateError;
20use std::error::Error as StdError;
21use std::fmt::Display;
22use std::fmt::Formatter;
23use std::fmt::Result as FmtResult;
24use std::io::Error as IoError;
25use std::path::PathBuf;
26
27#[derive(Debug)]
29pub enum Error {
30 AtLeastOneCSDLFileNeeded,
31 Io(String, IoError),
32 Edmx(String, ValidateError),
33 Compile(Vec<String>),
34 WrongRootService(AttributeValuesError),
35 Generate(Vec<String>),
36 ParseGenerated(syn::Error),
37 WriteOutput(PathBuf, IoError),
38}
39
40#[allow(clippy::needless_pass_by_value)]
43impl Error {
44 pub fn compile_error(e: CompileError<'_>) -> Self {
45 Self::Compile(
46 format!("{e}")
47 .split('\n')
48 .map(ToString::to_string)
49 .collect(),
50 )
51 }
52 pub fn generate_error(e: GenerateError<'_>) -> Self {
53 Self::Generate(
54 format!("{e}")
55 .split('\n')
56 .map(ToString::to_string)
57 .collect(),
58 )
59 }
60}
61
62impl Display for Error {
63 fn fmt(&self, f: &mut Formatter) -> FmtResult {
64 match self {
65 Self::AtLeastOneCSDLFileNeeded => {
66 "at least one CSDL file is needed for compilation".fmt(f)
67 }
68 Self::Io(fname, error) => write!(f, "input/output error: file: {fname}: {error}"),
69 Self::Edmx(fname, error) => {
70 write!(f, "EDMX format validation error: file: {fname}: {error}")
71 }
72 Self::Compile(lines) => {
73 write!(f, "compilation error:")?;
74 lines
75 .iter()
76 .enumerate()
77 .try_for_each(|(no, line)| write!(f, "\n #{no}: {line}"))
78 }
79 Self::WrongRootService(error) => write!(f, "root service format error: {error}"),
80 Self::Generate(lines) => {
81 write!(f, "generation error:")?;
82 lines
83 .iter()
84 .enumerate()
85 .try_for_each(|(no, line)| write!(f, "\n #{no}: {line}"))
86 }
87 Self::ParseGenerated(error) => {
88 write!(f, "failed to parse generated file: {error}")
89 }
90 Self::WriteOutput(fname, error) => {
91 write!(f, "failed write output file: {}: {error}", fname.display())
92 }
93 }
94 }
95}
96
97impl StdError for Error {}