ssh_key/certificate/
field.rs

1//! Certificate fields.
2
3use crate::Error;
4use core::fmt;
5
6/// Certificate fields.
7///
8/// This type is primarily used by the certificate builder for reporting
9/// errors in certificates.
10#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
11pub enum Field {
12    /// Subject public key
13    PublicKey,
14
15    /// Nonce
16    Nonce,
17
18    /// Serial number
19    Serial,
20
21    /// Certificate type: user or host
22    Type,
23
24    /// Key ID
25    KeyId,
26
27    /// Valid principals
28    ValidPrincipals,
29
30    /// Valid after (Unix time)
31    ValidAfter,
32
33    /// Valid before (Unix time)
34    ValidBefore,
35
36    /// Critical options
37    CriticalOptions,
38
39    /// Extensions
40    Extensions,
41
42    /// Signature key (i.e. CA key)
43    SignatureKey,
44
45    /// Signature
46    Signature,
47
48    /// Comment
49    Comment,
50}
51
52impl Field {
53    /// Get the field name as a string
54    pub fn as_str(self) -> &'static str {
55        match self {
56            Self::PublicKey => "public key",
57            Self::Nonce => "nonce",
58            Self::Serial => "serial",
59            Self::Type => "type",
60            Self::KeyId => "key id",
61            Self::ValidPrincipals => "valid principals",
62            Self::ValidAfter => "valid after",
63            Self::ValidBefore => "valid before",
64            Self::CriticalOptions => "critical options",
65            Self::Extensions => "extensions",
66            Self::SignatureKey => "signature key",
67            Self::Signature => "signature",
68            Self::Comment => "comment",
69        }
70    }
71
72    /// Get an [`Error`] that this field is invalid.
73    pub fn invalid_error(self) -> Error {
74        Error::CertificateFieldInvalid(self)
75    }
76}
77
78impl AsRef<str> for Field {
79    fn as_ref(&self) -> &str {
80        self.as_str()
81    }
82}
83
84impl fmt::Display for Field {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        f.write_str(self.as_str())
87    }
88}