1use std::fmt;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug, PartialEq)]
5pub struct Error {
6 pub msg: &'static str,
8 pub typ: Type,
9}
10
11impl Display for Error {
12 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
13 write!(f, "{:?}: {}", self.typ, self.msg)
14 }
15}
16
17impl std::error::Error for Error {}
18
19#[derive(Debug, PartialEq)]
21pub enum Type {
22 Invalid,
25 Expired,
27 Early,
29 Certificate,
31 Key,
33 Connection,
35 Header,
37 Payload,
39 Signature,
41 Internal,
43}
44
45pub(crate) fn err(msg: &'static str, typ: Type) -> Error {
46 Error { msg, typ }
47}
48
49pub(crate) fn err_inv(msg: &'static str) -> Error {
50 err(msg, Type::Invalid)
51}
52
53pub(crate) fn err_exp(msg: &'static str) -> Error {
54 err(msg, Type::Expired)
55}
56
57pub(crate) fn err_nbf(msg: &'static str) -> Error {
58 err(msg, Type::Early)
59}
60
61pub(crate) fn err_cer(msg: &'static str) -> Error {
62 err(msg, Type::Certificate)
63}
64
65pub(crate) fn err_key(msg: &'static str) -> Error {
66 err(msg, Type::Key)
67}
68
69pub(crate) fn err_con(msg: &'static str) -> Error {
70 err(msg, Type::Connection)
71}
72
73pub(crate) fn err_hea(msg: &'static str) -> Error {
74 err(msg, Type::Header)
75}
76
77pub(crate) fn err_pay(msg: &'static str) -> Error {
78 err(msg, Type::Payload)
79}
80
81pub(crate) fn err_sig(msg: &'static str) -> Error {
82 err(msg, Type::Signature)
83}
84
85pub(crate) fn err_int(msg: &'static str) -> Error {
86 err(msg, Type::Internal)
87}
88
89#[cfg(test)]
90mod tests {}