use ruma_macros::IdZst;
use super::{IdParseError, KeyName};
use crate::serde::{base64::Standard, Base64, Base64DecodeError};
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, IdZst)]
#[ruma_id(validate = ruma_identifiers_validation::base64_public_key::validate)]
pub struct Base64PublicKey(str);
impl OwnedBase64PublicKey {
pub fn with_bytes<B: AsRef<[u8]>>(bytes: B) -> OwnedBase64PublicKey {
Base64::<Standard, B>::new(bytes).into()
}
}
impl KeyName for Base64PublicKey {
fn validate(s: &str) -> Result<(), IdParseError> {
ruma_identifiers_validation::base64_public_key::validate(s)
}
}
impl KeyName for OwnedBase64PublicKey {
fn validate(s: &str) -> Result<(), IdParseError> {
ruma_identifiers_validation::base64_public_key::validate(s)
}
}
impl<B: AsRef<[u8]>> From<Base64<Standard, B>> for OwnedBase64PublicKey {
fn from(value: Base64<Standard, B>) -> Self {
value.to_string().try_into().unwrap_or_else(|_| unreachable!())
}
}
impl TryFrom<&Base64PublicKey> for Base64<Standard, Vec<u8>> {
type Error = Base64DecodeError;
fn try_from(value: &Base64PublicKey) -> Result<Self, Self::Error> {
Base64::parse(value)
}
}
impl TryFrom<&OwnedBase64PublicKey> for Base64<Standard, Vec<u8>> {
type Error = Base64DecodeError;
fn try_from(value: &OwnedBase64PublicKey) -> Result<Self, Self::Error> {
Base64::parse(value)
}
}
impl TryFrom<OwnedBase64PublicKey> for Base64<Standard, Vec<u8>> {
type Error = Base64DecodeError;
fn try_from(value: OwnedBase64PublicKey) -> Result<Self, Self::Error> {
Base64::parse(value)
}
}
#[cfg(test)]
mod tests {
use super::{Base64PublicKey, OwnedBase64PublicKey};
#[test]
fn valid_string() {
<&Base64PublicKey>::try_from("base64+master+public+key").unwrap();
}
#[test]
fn invalid_string() {
<&Base64PublicKey>::try_from("not@base@64").unwrap_err();
}
#[test]
fn constructor() {
_ = OwnedBase64PublicKey::with_bytes(b"self-signing master public key");
}
}