Skip to main content

json_web_key/
error.rs

1use std::{error::Error as StdError, fmt};
2
3#[derive(Clone, Debug)]
4pub struct Error {
5    col: Option<usize>,
6    line: Option<usize>,
7    kind: ErrorKind,
8    msg: String,
9}
10
11#[derive(Clone, Debug)]
12pub enum ErrorKind {
13    JsonParsingError,
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match &self.kind {
19            ErrorKind::JsonParsingError => write!(
20                f,
21                "Json Parsing Error: [line: {}][col: {}]{}",
22                self.line.unwrap(),
23                self.col.unwrap(),
24                self.msg
25            ),
26        }
27    }
28}
29
30impl StdError for Error {}
31
32impl From<serde_json::error::Error> for Error {
33    fn from(value: serde_json::error::Error) -> Self {
34        let col: Option<usize> = Some(value.column());
35        let kind = ErrorKind::JsonParsingError;
36        let line: Option<usize> = Some(value.line());
37        let msg: String = value.to_string();
38
39        Self {
40            col,
41            kind,
42            line,
43            msg,
44        }
45    }
46}