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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Error handling for the `repository` module.
//!

use std::fmt;
use std::convert::Infallible;
use bcder::decode::{DecodeError, ContentError};
use crate::crypto::keys::SignatureVerificationError;


//------------ InspectionError -----------------------------------------------

#[derive(Debug)]
pub struct InspectionError{
    inner: ContentError,
}

impl InspectionError {
    pub fn new(err: impl Into<ContentError>) -> Self {
        InspectionError { inner: err.into() }
    }
}

impl From<ContentError> for InspectionError {
    fn from(err: ContentError) -> InspectionError {
        InspectionError { inner: err }
    }
}

impl From<InspectionError> for ContentError {
    fn from(err: InspectionError) -> Self {
        err.inner
    }
}

impl fmt::Display for InspectionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}


//------------ VerificationError ---------------------------------------------

#[derive(Debug)]
pub struct VerificationError{
    inner: ContentError,
}

impl VerificationError {
    pub fn new(err: impl Into<ContentError>) -> Self {
        VerificationError { inner: err.into() }
    }
}

impl From<ContentError> for VerificationError {
    fn from(err: ContentError) -> VerificationError {
        VerificationError { inner: err }
    }
}

impl From<SignatureVerificationError> for VerificationError {
    fn from(err: SignatureVerificationError) -> Self {
        ContentError::from(err).into()
    }
}

impl fmt::Display for VerificationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}


//------------ ValidationError -----------------------------------------------

#[derive(Debug)]
pub struct ValidationError{
    inner: ValidationErrorKind,
}

#[derive(Debug)]
enum ValidationErrorKind {
    Decoding(DecodeError<Infallible>),
    Inspection(InspectionError),
    Verification(VerificationError),
}

impl From<DecodeError<Infallible>> for ValidationError {
    fn from(err: DecodeError<Infallible>) -> ValidationError {
        ValidationError {
            inner: ValidationErrorKind::Decoding(err)
        }
    }
}

impl From<InspectionError> for ValidationError {
    fn from(err: InspectionError) -> ValidationError {
        ValidationError {
            inner: ValidationErrorKind::Inspection(err)
        }
    }
}

impl From<VerificationError> for ValidationError {
    fn from(err: VerificationError) -> ValidationError {
        ValidationError {
            inner: ValidationErrorKind::Verification(err)
        }
    }
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.inner {
            ValidationErrorKind::Decoding(ref inner) => inner.fmt(f),
            ValidationErrorKind::Inspection(ref inner) => inner.fmt(f),
            ValidationErrorKind::Verification(ref inner) => inner.fmt(f),
        }
    }
}