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)]
9pub enum PemLabel {
10    /// PKCS#8 private key material.
11    PrivateKey,
12    /// SEC1 elliptic-curve private key material.
13    EcPrivateKey,
14    /// SubjectPublicKeyInfo public key material.
15    PublicKey,
16}
17
18impl PemLabel {
19    /// Returns the exact RFC 7468 label text used in BEGIN/END boundaries.
20    pub const fn as_str(self) -> &'static str {
21        match self {
22            Self::PrivateKey => "PRIVATE KEY",
23            Self::EcPrivateKey => "EC PRIVATE KEY",
24            Self::PublicKey => "PUBLIC KEY",
25        }
26    }
27
28    pub(crate) fn parse(input: &str) -> Result<Self, PemError> {
29        match input {
30            "PRIVATE KEY" => Ok(Self::PrivateKey),
31            "EC PRIVATE KEY" => Ok(Self::EcPrivateKey),
32            "PUBLIC KEY" => Ok(Self::PublicKey),
33            _ => Err(PemError::UnsupportedLabel),
34        }
35    }
36}