twist-jwt 0.3.1

An implementation of RFC7519 JSON Web Token (JWT)
//! Possible `twist-jwt` errors.
use Algorithm;
use base64;
use ring;
use serde_json;
use std::{self, fmt};

/// Errors generated by this library.
pub enum TwistJwt {
    /// Error decoding a base64 string.
    Base64(base64::Base64Error),
    /// Thrown if an invalid hmac algorithm was specified.
    InvalidAlgorithm(Algorithm),
    /// Thown if decode receives an invalid token.
    InvalidToken,
    /// Thown on IO Errors.
    Io(std::io::Error),
    /// Error decoding/encoding JSON.
    Json(serde_json::Error),
    /// Error from ring.
    Ring(ring::error::Unspecified),
}

impl fmt::Display for TwistJwt {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            TwistJwt::Base64(ref e) => write!(f, "{}", e),
            TwistJwt::InvalidAlgorithm(ref a) => write!(f, "{}", a),
            TwistJwt::InvalidToken => write!(f, "Invalid Token"),
            TwistJwt::Io(ref e) => write!(f, "{}", e),
            TwistJwt::Json(ref e) => write!(f, "{}", e),
            TwistJwt::Ring(ref e) => write!(f, "{}", e),
        }
    }
}

impl From<base64::Base64Error> for TwistJwt {
    fn from(err: base64::Base64Error) -> TwistJwt {
        TwistJwt::Base64(err)
    }
}

impl From<std::io::Error> for TwistJwt {
    fn from(err: std::io::Error) -> TwistJwt {
        TwistJwt::Io(err)
    }
}

impl From<ring::error::Unspecified> for TwistJwt {
    fn from(err: ring::error::Unspecified) -> TwistJwt {
        TwistJwt::Ring(err)
    }
}

impl From<serde_json::Error> for TwistJwt {
    fn from(err: serde_json::Error) -> TwistJwt {
        TwistJwt::Json(err)
    }
}