pem/
errors.rs

1// Copyright 2017 Jonathan Creekmore
2//
3// Licensed under the MIT license <LICENSE.md or
4// http://opensource.org/licenses/MIT>. This file may not be
5// copied, modified, or distributed except according to those terms.
6use std::error::Error;
7use std::fmt;
8
9/// The `pem` error type.
10#[derive(Debug, Eq, PartialEq)]
11#[allow(missing_docs)]
12pub enum PemError {
13    MismatchedTags(String, String),
14    MalformedFraming,
15    MissingBeginTag,
16    MissingEndTag,
17    MissingData,
18    InvalidData(::base64::DecodeError),
19    NotUtf8(::std::str::Utf8Error),
20}
21
22impl fmt::Display for PemError {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self {
25            PemError::MismatchedTags(b, e) => {
26                write!(f, "mismatching BEGIN (\"{}\") and END (\"{}\") tags", b, e)
27            }
28            PemError::MalformedFraming => write!(f, "malformedframing"),
29            PemError::MissingBeginTag => write!(f, "missing BEGIN tag"),
30            PemError::MissingEndTag => write!(f, "missing END tag"),
31            PemError::MissingData => write!(f, "missing data"),
32            PemError::InvalidData(e) => write!(f, "invalid data: {}", e),
33            PemError::NotUtf8(e) => write!(f, "invalid utf-8 value: {}", e),
34        }
35    }
36}
37
38impl Error for PemError {
39    fn source(&self) -> Option<&(dyn Error + 'static)> {
40        match self {
41            // Errors originating from other libraries.
42            PemError::InvalidData(e) => Some(e),
43            PemError::NotUtf8(e) => Some(e),
44            // Errors directly originating from `pem-rs`.
45            _ => None,
46        }
47    }
48}
49
50/// The `pem` result type.
51pub type Result<T> = ::std::result::Result<T, PemError>;