tide_auth/
error.rs

1use base64::DecodeError;
2use http_types::StatusCode;
3use std::{fmt::Display, str::Utf8Error};
4
5#[derive(Debug)]
6pub enum Error {
7    /// Header value is malformed
8    Invalid,
9    /// Authentication scheme is missing
10    MissingScheme,
11    /// Required authentication field is missing
12    MissingField(&'static str),
13    /// Unable to convert header into the str
14    // ToStrError(header::ToStrError),
15    /// Malformed base64 string
16    Base64DecodeError(DecodeError),
17    /// Malformed UTF-8 string
18    Utf8Error(Utf8Error),
19}
20
21impl Display for Error {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Error::Invalid => {
25                write!(f, "Invalid")
26            }
27            Error::Base64DecodeError(error) => Display::fmt(error, f),
28            Error::MissingField(msg) => {
29                write!(f, "MissingField: {}", msg)
30            }
31            Error::MissingScheme => {
32                write!(f, "MissingScheme")
33            }
34            Error::Utf8Error(error) => Display::fmt(error, f),
35        }
36    }
37}
38
39impl From<DecodeError> for Error {
40    fn from(decode_error: DecodeError) -> Self {
41        Error::Base64DecodeError(decode_error)
42    }
43}
44
45impl From<Utf8Error> for Error {
46    fn from(utf8_error: Utf8Error) -> Self {
47        Error::Utf8Error(utf8_error)
48    }
49}
50
51impl From<Error> for tide::Error {
52    fn from(auth_error: Error) -> Self {
53        tide::Error::from_str(StatusCode::Forbidden, auth_error)
54    }
55}
56
57pub type Result<T> = std::result::Result<T, Error>;