use bstr::ByteSlice;
use winnow::{error::StrContext, prelude::*};
use crate::{signature::decode, Identity, IdentityRef};
impl<'a> IdentityRef<'a> {
pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<Self, winnow::error::ErrMode<E>>
where
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8], StrContext>,
{
decode::identity.parse_next(&mut data)
}
pub fn to_owned(&self) -> Identity {
Identity {
name: self.name.to_owned(),
email: self.email.to_owned(),
}
}
pub fn trim(&self) -> IdentityRef<'a> {
IdentityRef {
name: self.name.trim().as_bstr(),
email: self.email.trim().as_bstr(),
}
}
}
mod write {
use crate::{signature::write::validated_token, Identity, IdentityRef};
impl Identity {
pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
self.to_ref().write_to(out)
}
}
impl IdentityRef<'_> {
pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
out.write_all(validated_token(self.name)?)?;
out.write_all(b" ")?;
out.write_all(b"<")?;
out.write_all(validated_token(self.email)?)?;
out.write_all(b">")
}
}
}
mod impls {
use crate::{Identity, IdentityRef};
impl Identity {
pub fn to_ref(&self) -> IdentityRef<'_> {
IdentityRef {
name: self.name.as_ref(),
email: self.email.as_ref(),
}
}
}
impl From<IdentityRef<'_>> for Identity {
fn from(other: IdentityRef<'_>) -> Identity {
let IdentityRef { name, email } = other;
Identity {
name: name.to_owned(),
email: email.to_owned(),
}
}
}
impl<'a> From<&'a Identity> for IdentityRef<'a> {
fn from(other: &'a Identity) -> IdentityRef<'a> {
other.to_ref()
}
}
}