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
//! A common error used by SQRL clients and servers

use std::{fmt, num::ParseIntError, string::FromUtf8Error};

/// An error that can occur during SQRL protocol
pub struct SqrlError {
    error_message: String,
}

impl SqrlError {
    /// Create a new SqrlError with the string as error message
    pub fn new(error: String) -> Self {
        SqrlError {
            error_message: error,
        }
    }
}

impl fmt::Display for SqrlError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error_message)
    }
}

impl fmt::Debug for SqrlError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error_message)
    }
}

impl std::error::Error for SqrlError {}

impl From<url::ParseError> for SqrlError {
    fn from(error: url::ParseError) -> Self {
        SqrlError::new(error.to_string())
    }
}

impl From<base64::DecodeError> for SqrlError {
    fn from(error: base64::DecodeError) -> Self {
        SqrlError::new(error.to_string())
    }
}

impl From<FromUtf8Error> for SqrlError {
    fn from(error: FromUtf8Error) -> Self {
        SqrlError::new(error.to_string())
    }
}

impl From<ParseIntError> for SqrlError {
    fn from(value: ParseIntError) -> Self {
        SqrlError::new(value.to_string())
    }
}