Skip to main content

nv_redfish_csdl_compiler/
error.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use 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/// CSDL Compiler errors.
28#[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// Passing by reference would break possibility to use it as
41// `map_err(Error::compile_error)` etc.
42#[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 {}