Skip to main content

rpki/repository/
error.rs

1//! Error handling for the `repository` module.
2//!
3
4use std::fmt;
5use std::convert::Infallible;
6use bcder::decode::{DecodeError, ContentError};
7use crate::crypto::keys::SignatureVerificationError;
8
9
10//------------ InspectionError -----------------------------------------------
11
12#[derive(Debug)]
13pub struct InspectionError{
14    inner: ContentError,
15}
16
17impl InspectionError {
18    pub fn new(err: impl Into<ContentError>) -> Self {
19        InspectionError { inner: err.into() }
20    }
21}
22
23impl From<ContentError> for InspectionError {
24    fn from(err: ContentError) -> InspectionError {
25        InspectionError { inner: err }
26    }
27}
28
29impl From<InspectionError> for ContentError {
30    fn from(err: InspectionError) -> Self {
31        err.inner
32    }
33}
34
35impl fmt::Display for InspectionError {
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        self.inner.fmt(f)
38    }
39}
40
41
42//------------ VerificationError ---------------------------------------------
43
44#[derive(Debug)]
45pub struct VerificationError{
46    inner: ContentError,
47}
48
49impl VerificationError {
50    pub fn new(err: impl Into<ContentError>) -> Self {
51        VerificationError { inner: err.into() }
52    }
53}
54
55impl From<ContentError> for VerificationError {
56    fn from(err: ContentError) -> VerificationError {
57        VerificationError { inner: err }
58    }
59}
60
61impl From<SignatureVerificationError> for VerificationError {
62    fn from(err: SignatureVerificationError) -> Self {
63        ContentError::from(err).into()
64    }
65}
66
67impl fmt::Display for VerificationError {
68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69        self.inner.fmt(f)
70    }
71}
72
73
74//------------ ValidationError -----------------------------------------------
75
76#[derive(Debug)]
77pub struct ValidationError{
78    inner: ValidationErrorKind,
79}
80
81#[derive(Debug)]
82enum ValidationErrorKind {
83    Decoding(DecodeError<Infallible>),
84    Inspection(InspectionError),
85    Verification(VerificationError),
86}
87
88impl From<DecodeError<Infallible>> for ValidationError {
89    fn from(err: DecodeError<Infallible>) -> ValidationError {
90        ValidationError {
91            inner: ValidationErrorKind::Decoding(err)
92        }
93    }
94}
95
96impl From<InspectionError> for ValidationError {
97    fn from(err: InspectionError) -> ValidationError {
98        ValidationError {
99            inner: ValidationErrorKind::Inspection(err)
100        }
101    }
102}
103
104impl From<VerificationError> for ValidationError {
105    fn from(err: VerificationError) -> ValidationError {
106        ValidationError {
107            inner: ValidationErrorKind::Verification(err)
108        }
109    }
110}
111
112impl fmt::Display for ValidationError {
113    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114        match self.inner {
115            ValidationErrorKind::Decoding(ref inner) => inner.fmt(f),
116            ValidationErrorKind::Inspection(ref inner) => inner.fmt(f),
117            ValidationErrorKind::Verification(ref inner) => inner.fmt(f),
118        }
119    }
120}
121