use alloc::borrow::Cow;
use alloc::string::String;
use alloc::string::ToString;
use der::{
Choice, ValueOrd,
asn1::{Any, BmpString, PrintableString, TeletexString},
};
#[derive(Clone, Debug, Eq, PartialEq, Choice, ValueOrd)]
#[allow(missing_docs)]
pub enum DirectoryString {
#[asn1(type = "PrintableString")]
PrintableString(PrintableString),
#[asn1(type = "TeletexString")]
TeletexString(TeletexString),
#[asn1(type = "UTF8String")]
Utf8String(String),
#[asn1(type = "BMPString")]
BmpString(BmpString),
}
impl<'a> TryFrom<&'a Any> for DirectoryString {
type Error = der::Error;
fn try_from(any: &'a Any) -> der::Result<Self> {
any.decode_as()
}
}
impl DirectoryString {
pub fn value(&self) -> Cow<'_, str> {
match self {
Self::PrintableString(s) => Cow::Borrowed(s.as_ref()),
Self::TeletexString(s) => Cow::Borrowed(s.as_ref()),
Self::Utf8String(s) => Cow::Borrowed(s.as_ref()),
Self::BmpString(s) => Cow::Owned(s.to_string()),
}
}
#[deprecated(since = "0.3.0-pre.0", note = "use `DirectoryString::value` instead")]
#[allow(clippy::should_implement_trait)]
pub fn as_ref(&self) -> &str {
match self {
Self::PrintableString(s) => s.as_ref(),
Self::TeletexString(s) => s.as_ref(),
Self::Utf8String(s) => s.as_ref(),
Self::BmpString(_s) => "",
}
}
}
impl From<DirectoryString> for String {
fn from(value: DirectoryString) -> Self {
value.value().into_owned()
}
}