1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Low level library that could be returned by the parser.
//! Based on https://github.com/draft6/libucl-rs

use std::error::Error;
use std::fmt;

use libucl_bind::{ucl_error_t, ucl_schema_error_code};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum UclErrorType {
    Ok,
    Syntax,
    Io,
    State,
    Nested,
    Macro,
    Internal,
    SSL,
    Other,
}

impl UclErrorType {
    pub fn from_code(num: i32, desc: String) -> UclError {
        match num {
            _ if num == ucl_error_t::UCL_EOK as i32 => UclError {
                code: UclErrorType::Ok,
                desc,
            },
            _ if num == ucl_error_t::UCL_ESYNTAX as i32 => UclError {
                code: UclErrorType::Syntax,
                desc,
            },
            _ if num == ucl_error_t::UCL_EIO as i32 => UclError {
                code: UclErrorType::Io,
                desc,
            },
            _ if num == ucl_error_t::UCL_ESTATE as i32 => UclError {
                code: UclErrorType::State,
                desc,
            },
            _ if num == ucl_error_t::UCL_ENESTED as i32 => UclError {
                code: UclErrorType::Nested,
                desc,
            },
            _ if num == ucl_error_t::UCL_EMACRO as i32 => UclError {
                code: UclErrorType::Macro,
                desc,
            },
            _ if num == ucl_error_t::UCL_EINTERNAL as i32 => UclError {
                code: UclErrorType::Internal,
                desc,
            },
            _ if num == ucl_error_t::UCL_ESSL as i32 => UclError {
                code: UclErrorType::SSL,
                desc,
            },
            _ => UclError {
                code: UclErrorType::Other,
                desc,
            },
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UclError {
    code: UclErrorType,
    desc: String,
}

impl fmt::Display for UclError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.desc)
    }
}

impl Error for UclError {
    fn description(&self) -> &str {
        self.desc.as_ref()
    }

    fn cause(&self) -> Option<&dyn Error> {
        None
    }
}

impl UclError {
    pub fn boxed(self) -> Box<UclError> {
        Box::new(self)
    }

    pub fn kind(&self) -> UclErrorType {
        self.code
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UclSchemaErrorType {
    Ok,
    TypeMismatch,
    InvalidSchema,
    MissingProperty,
    Constraint,
    MissingDependency,
    Other,
}

impl UclSchemaErrorType {
    pub fn from_code(num: i32, desc: String) -> UclSchemaError {
        match num {
            _ if num == ucl_schema_error_code::UCL_SCHEMA_OK as i32 => UclSchemaError {
                code: UclSchemaErrorType::Ok,
                desc,
            },
            _ if num == ucl_schema_error_code::UCL_SCHEMA_TYPE_MISMATCH as i32 => UclSchemaError {
                code: UclSchemaErrorType::TypeMismatch,
                desc,
            },
            _ if num == ucl_schema_error_code::UCL_SCHEMA_INVALID_SCHEMA as i32 => UclSchemaError {
                code: UclSchemaErrorType::InvalidSchema,
                desc,
            },
            _ if num == ucl_schema_error_code::UCL_SCHEMA_MISSING_PROPERTY as i32 => {
                UclSchemaError {
                    code: UclSchemaErrorType::MissingProperty,
                    desc,
                }
            }
            _ if num == ucl_schema_error_code::UCL_SCHEMA_CONSTRAINT as i32 => UclSchemaError {
                code: UclSchemaErrorType::Constraint,
                desc,
            },
            _ if num == ucl_schema_error_code::UCL_SCHEMA_MISSING_DEPENDENCY as i32 => {
                UclSchemaError {
                    code: UclSchemaErrorType::MissingDependency,
                    desc,
                }
            }
            _ => UclSchemaError {
                code: UclSchemaErrorType::Other,
                desc,
            },
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UclSchemaError {
    pub code: UclSchemaErrorType,
    pub desc: String,
}

impl fmt::Display for UclSchemaError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.desc)
    }
}

impl Error for UclSchemaError {
    fn description(&self) -> &str {
        self.desc.as_ref()
    }

    fn cause(&self) -> Option<&dyn Error> {
        None
    }
}