sz_configtool_lib/
error.rs1use std::fmt;
2
3#[derive(Debug)]
5pub enum SzConfigError {
6 JsonParse(String),
8 NotFound(String), AlreadyExists(String), InvalidInput(String),
14 MissingSection(String),
16 InvalidStructure(String),
18 MissingField(String),
20 InvalidConfig(String),
22 NotImplemented(String),
24}
25
26impl SzConfigError {
27 pub fn json_parse<S: Into<String>>(msg: S) -> Self {
29 Self::JsonParse(msg.into())
30 }
31
32 pub fn not_found<S: Into<String>>(msg: S) -> Self {
34 Self::NotFound(msg.into())
35 }
36
37 pub fn already_exists<S: Into<String>>(msg: S) -> Self {
39 Self::AlreadyExists(msg.into())
40 }
41
42 pub fn validation<S: Into<String>>(msg: S) -> Self {
44 Self::InvalidInput(msg.into())
45 }
46
47 pub fn not_implemented<S: Into<String>>(msg: S) -> Self {
49 Self::NotImplemented(msg.into())
50 }
51}
52
53impl fmt::Display for SzConfigError {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 match self {
56 Self::JsonParse(msg) => write!(f, "JSON parse error: {msg}"),
57 Self::NotFound(msg) => write!(f, "{msg}"),
58 Self::AlreadyExists(msg) => write!(f, "{msg}"),
59 Self::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
60 Self::MissingSection(section) => write!(f, "Missing config section: {section}"),
61 Self::InvalidStructure(msg) => write!(f, "Invalid config structure: {msg}"),
62 Self::MissingField(field) => write!(f, "Missing required field: {field}"),
63 Self::InvalidConfig(msg) => write!(f, "Invalid configuration: {msg}"),
64 Self::NotImplemented(msg) => write!(f, "Not implemented: {msg}"),
65 }
66 }
67}
68
69impl std::error::Error for SzConfigError {}
70
71impl From<serde_json::Error> for SzConfigError {
72 fn from(err: serde_json::Error) -> Self {
73 SzConfigError::JsonParse(err.to_string())
74 }
75}
76
77pub type Result<T> = std::result::Result<T, SzConfigError>;