Skip to main content

capsule_lib/
encoding.rs

1// SPDX-FileCopyrightText: 2026 Alexander R. Croft
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4use crate::error::{CapsuleError, CapsuleResult};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Encoding {
8    Ascii,
9    Base64,
10    Cbor,
11}
12
13impl Encoding {
14    pub fn from_byte(b: u8) -> CapsuleResult<Self> {
15        match b {
16            b'A' => Ok(Self::Ascii),
17            b'B' => Ok(Self::Base64),
18            b'C' => Ok(Self::Cbor),
19            _ => Err(CapsuleError::InvalidEncodingField),
20        }
21    }
22
23    pub fn to_byte(self) -> u8 {
24        match self {
25            Self::Ascii => b'A',
26            Self::Base64 => b'B',
27            Self::Cbor => b'C',
28        }
29    }
30}