use alloc::string::String;
use alloc::vec::Vec;
use crate::error::ProfileError;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct KeyId(Vec<u8>);
impl KeyId {
pub fn from_text(id: &str) -> Result<Self, ProfileError> {
Self::from_bytes(id.as_bytes().to_vec())
}
pub fn from_bytes(id: Vec<u8>) -> Result<Self, ProfileError> {
if id.is_empty() || id.len() > 128 {
return Err(ProfileError::KeyIdLength { actual: id.len() });
}
Ok(Self(id))
}
#[must_use]
pub fn as_catalog_name(&self) -> Option<&str> {
core::str::from_utf8(&self.0).ok()
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MessageId(Vec<u8>);
impl MessageId {
pub fn from_bytes(id: Vec<u8>) -> Result<Self, ProfileError> {
if id.is_empty() || id.len() > 64 {
return Err(ProfileError::MessageIdLength { actual: id.len() });
}
Ok(Self(id))
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Subject(String);
impl Subject {
pub fn new(subject: String) -> Result<Self, ProfileError> {
if subject.is_empty() {
return Err(ProfileError::EmptySubject);
}
Ok(Self(subject))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ResponseSubject(String);
impl ResponseSubject {
pub fn new(subject: String) -> Result<Self, ProfileError> {
if subject.is_empty() {
return Err(ProfileError::EmptySubject);
}
Ok(Self(subject))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ContentType(String);
impl ContentType {
pub fn new(content_type: String) -> Result<Self, ProfileError> {
if content_type.is_empty()
|| content_type.trim() != content_type
|| content_type.matches('/').count() != 1
{
return Err(ProfileError::ContentTypeForm);
}
Ok(Self(content_type))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnixTime(pub i64);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExternalAad(Vec<u8>);
impl ExternalAad {
#[must_use]
pub const fn empty() -> Self {
Self(Vec::new())
}
#[must_use]
pub const fn from_bytes(bytes: Vec<u8>) -> Self {
Self(bytes)
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SealedAad {
pub signature: ExternalAad,
pub encryption: ExternalAad,
}
impl SealedAad {
#[must_use]
pub const fn empty() -> Self {
Self {
signature: ExternalAad::empty(),
encryption: ExternalAad::empty(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature(Vec<u8>);
impl Signature {
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, ProfileError> {
if bytes.is_empty() {
return Err(ProfileError::EmptySignature);
}
Ok(Self(bytes))
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CoseBytes(Vec<u8>);
impl CoseBytes {
pub(crate) const fn new(bytes: Vec<u8>) -> Self {
Self(bytes)
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
#[must_use]
pub fn into_vec(self) -> Vec<u8> {
self.0
}
}
impl AsRef<[u8]> for CoseBytes {
fn as_ref(&self) -> &[u8] {
&self.0
}
}