use hex_slice::AsHex;
use serde::Serializer;
use std::fmt;
fn hex_tuple(name: &str, f: &mut fmt::Formatter<'_>, val: &[u8]) -> fmt::Result {
write!(f, "{}({:02x})", name, val.plain_hex(false))
}
#[derive(serde_derive::Serialize)]
pub struct UserDefinedDeprecated(pub Vec<u8>);
impl fmt::Debug for UserDefinedDeprecated {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex_tuple("UserDefinedDeprecated", f, &self.0)
}
}
#[derive(Debug, serde_derive::Serialize)]
pub struct IsciDeprecated(pub String);
#[derive(Debug, serde_derive::Serialize)]
pub struct AdID(pub String);
#[derive(serde_derive::Serialize)]
pub struct IsanDeprecated(pub Vec<u8>);
impl fmt::Debug for IsanDeprecated {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex_tuple("IsanDeprecated", f, &self.0)
}
}
#[derive(serde_derive::Serialize)]
pub struct Umid(pub Vec<u8>);
impl fmt::Debug for Umid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Umid(")?;
for (i, c) in self.0.chunks(4).enumerate() {
if i > 0 {
f.write_str(".")?;
}
write!(f, "{:02x}", c.plain_hex(false))?;
}
f.write_str(")")
}
}
#[derive(Debug, serde_derive::Serialize)]
pub struct TID(pub String);
#[derive(PartialEq, serde_derive::Serialize)]
pub struct TI(pub Vec<u8>);
impl fmt::Debug for TI {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex_tuple("TI", f, &self.0)
}
}
#[derive(Debug, serde_derive::Serialize)]
pub struct ADI(pub String);
#[derive(serde_derive::Serialize)]
pub struct EIDR(pub [u8; 12]);
impl fmt::Debug for EIDR {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex_tuple("EIDR", f, &self.0)
}
}
#[derive(serde_derive::Serialize)]
pub struct ATSC(pub Vec<u8>);
impl fmt::Debug for ATSC {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex_tuple("ATSC", f, &self.0)
}
}
#[derive(serde_derive::Serialize)]
pub struct MPU(pub Vec<u8>);
impl fmt::Debug for MPU {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex_tuple("MPU", f, &self.0)
}
}
#[derive(Debug, serde_derive::Serialize)]
pub struct ADSInformation(pub Vec<u8>);
#[derive(Debug)]
pub struct Url(pub url::Url);
impl serde::Serialize for Url {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
serializer.serialize_str(self.0.as_str())
}
}
#[cfg(test)]
mod test {
use super::*;
use hex_literal::*;
#[test]
fn umid_fmt() {
assert_eq!(
"Umid(00000000.11111111.22222222.33333333.44444444.55555555.66666666.77889900)",
format!(
"{:?}",
Umid(
hex!("0000000011111111222222223333333344444444555555556666666677889900")
.to_vec()
)
)
)
}
}