Skip to main content

json_schema_rs/
error.rs

1use std::error;
2use std::fmt;
3
4/// Error type for JSON Schema code generation operations.
5#[derive(Debug)]
6pub enum JsonSchemaGenError {
7    /// Generic error with a message.
8    GenericError(String),
9
10    /// I/O error (e.g., reading schema file, writing output file).
11    IoError(std::io::Error),
12
13    /// JSON parsing error.
14    JsonError(serde_json::Error),
15}
16
17impl error::Error for JsonSchemaGenError {}
18
19impl fmt::Display for JsonSchemaGenError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::GenericError(message) => write!(f, "{message}"),
23            Self::IoError(io_error) => fmt::Display::fmt(io_error, f),
24            Self::JsonError(json_error) => fmt::Display::fmt(json_error, f),
25        }
26    }
27}
28
29impl From<&str> for JsonSchemaGenError {
30    fn from(message: &str) -> Self {
31        Self::GenericError(message.to_string())
32    }
33}
34
35impl From<String> for JsonSchemaGenError {
36    fn from(message: String) -> Self {
37        Self::GenericError(message)
38    }
39}
40
41impl From<std::io::Error> for JsonSchemaGenError {
42    fn from(io_error: std::io::Error) -> Self {
43        Self::IoError(io_error)
44    }
45}
46
47impl From<serde_json::Error> for JsonSchemaGenError {
48    fn from(json_error: serde_json::Error) -> Self {
49        Self::JsonError(json_error)
50    }
51}