Skip to main content

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    #[must_use]
55    pub fn as_str(self) -> &'static str {
56        match self {
57            Self::PublicKey => "public key",
58            Self::Nonce => "nonce",
59            Self::Serial => "serial",
60            Self::Type => "type",
61            Self::KeyId => "key id",
62            Self::ValidPrincipals => "valid principals",
63            Self::ValidAfter => "valid after",
64            Self::ValidBefore => "valid before",
65            Self::CriticalOptions => "critical options",
66            Self::Extensions => "extensions",
67            Self::SignatureKey => "signature key",
68            Self::Signature => "signature",
69            Self::Comment => "comment",
70        }
71    }
72
73    /// Get an [`Error`] that this field is invalid.
74    #[must_use]
75    pub fn invalid_error(self) -> Error {
76        Error::CertificateFieldInvalid(self)
77    }
78}
79
80impl AsRef<str> for Field {
81    fn as_ref(&self) -> &str {
82        self.as_str()
83    }
84}
85
86impl fmt::Display for Field {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        f.write_str(self.as_str())
89    }
90}