Skip to main content

codec_pem/
label.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::PemError;
6
7/// Supported PEM labels.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum PemLabel {
11    /// PKCS#8 private key material.
12    PrivateKey,
13    /// SEC1 elliptic-curve private key material.
14    EcPrivateKey,
15    /// SubjectPublicKeyInfo public key material.
16    PublicKey,
17}
18
19impl PemLabel {
20    /// Returns the exact RFC 7468 label text used in BEGIN/END boundaries.
21    pub const fn as_str(self) -> &'static str {
22        match self {
23            Self::PrivateKey => "PRIVATE KEY",
24            Self::EcPrivateKey => "EC PRIVATE KEY",
25            Self::PublicKey => "PUBLIC KEY",
26        }
27    }
28
29    pub(crate) fn parse(input: &str) -> Result<Self, PemError> {
30        match input {
31            "PRIVATE KEY" => Ok(Self::PrivateKey),
32            "EC PRIVATE KEY" => Ok(Self::EcPrivateKey),
33            "PUBLIC KEY" => Ok(Self::PublicKey),
34            _ => Err(PemError::UnsupportedLabel),
35        }
36    }
37}