use std::fmt;
use std::ops::Deref;
use ref_cast::RefCast;
use crate::mtls::oid;
use crate::mtls::x509::X509Name;
#[repr(transparent)]
#[derive(Debug, PartialEq, RefCast)]
pub struct Name<'a>(X509Name<'a>);
impl<'a> Name<'a> {
pub fn common_name(&self) -> Option<&'a str> {
self.common_names().next()
}
pub fn common_names(&self) -> impl Iterator<Item = &'a str> + '_ {
self.iter_by_oid(&oid::OID_X509_COMMON_NAME)
.filter_map(|n| n.as_str().ok())
}
pub fn email(&self) -> Option<&'a str> {
self.emails().next()
}
pub fn emails(&self) -> impl Iterator<Item = &'a str> + '_ {
self.iter_by_oid(&oid::OID_PKCS9_EMAIL_ADDRESS)
.filter_map(|n| n.as_str().ok())
}
pub fn is_empty(&self) -> bool {
self.0.as_raw().is_empty()
}
}
impl<'a> Deref for Name<'a> {
type Target = X509Name<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for Name<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}