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
//! The certificate file formats.

use std::str::FromStr;

/// Format of the certificate/key file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
    /// PEM format.
    PEM,
    /// DER format
    /// PKCS8 specifically, other formats are not supported via this DER.
    DER,
}

/// Error for the format parsing.
#[derive(Debug, thiserror::Error)]
#[error("unknown format: {0}")]
pub struct FormatParseError(pub String);

impl FromStr for Format {
    type Err = FormatParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "pem" => Self::PEM,
            "der" => Self::DER,
            other => return Err(FormatParseError(other.to_owned())),
        })
    }
}