ssh_key/certificate/
cert_type.rs

1/// OpenSSH certificate types.
2use crate::{decode::Decode, encode::Encode, reader::Reader, writer::Writer, Error, Result};
3
4/// Types of OpenSSH certificates: user or host.
5#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
6#[repr(u32)]
7pub enum CertType {
8    /// User certificate
9    User = 1,
10
11    /// Host certificate
12    Host = 2,
13}
14
15impl CertType {
16    /// Is this a host certificate?
17    pub fn is_host(self) -> bool {
18        self == CertType::Host
19    }
20
21    /// Is this a user certificate?
22    pub fn is_user(self) -> bool {
23        self == CertType::User
24    }
25}
26
27impl Decode for CertType {
28    fn decode(reader: &mut impl Reader) -> Result<Self> {
29        u32::decode(reader)?.try_into()
30    }
31}
32
33impl Default for CertType {
34    fn default() -> Self {
35        Self::User
36    }
37}
38
39impl Encode for CertType {
40    fn encoded_len(&self) -> Result<usize> {
41        Ok(4)
42    }
43
44    fn encode(&self, writer: &mut impl Writer) -> Result<()> {
45        u32::from(*self).encode(writer)
46    }
47}
48
49impl From<CertType> for u32 {
50    fn from(cert_type: CertType) -> u32 {
51        cert_type as u32
52    }
53}
54
55impl TryFrom<u32> for CertType {
56    type Error = Error;
57
58    fn try_from(n: u32) -> Result<CertType> {
59        match n {
60            1 => Ok(CertType::User),
61            2 => Ok(CertType::Host),
62            _ => Err(Error::FormatEncoding),
63        }
64    }
65}