use std::{
fmt::{Debug, Display},
str::FromStr,
};
use crate::error::ParseError;
mod cmm;
pub use cmm::Cmm;
mod device_class;
pub use device_class::DeviceClass;
mod colorspace;
pub use colorspace::ColorSpace;
pub mod technology;
mod pcs;
pub use pcs::Pcs;
mod platform;
pub use platform::Platform;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Signature(pub u32);
impl Display for Signature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format_u32_as_string(f, self.0)
}
}
impl Debug for Signature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format_u32_as_string(f, self.0)
}
}
fn format_u32_as_string(f: &mut std::fmt::Formatter<'_>, value: u32) -> std::fmt::Result {
use crate::is_printable_ascii_bytes;
let bytes = value.to_be_bytes();
if is_printable_ascii_bytes(&bytes) {
let s = String::from_utf8_lossy(&bytes);
write!(f, "{s}")
} else {
write!(f, "{value:08X}")
}
}
impl FromStr for Signature {
type Err = crate::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > 4 || s.is_empty() {
return Err(ParseError::new(format!(
"Signature must be between 1 and 4 characters - got: {s}"
))
.into());
}
let padded = format!("{s: <4}"); let bytes = padded.as_bytes();
let value = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
Ok(Signature(value))
}
}
impl From<Signature> for u32 {
fn from(sig: Signature) -> Self {
sig.0
}
}
impl From<u32> for Signature {
fn from(value: u32) -> Self {
Signature(value)
}
}
impl From<Signature> for [u8; 4] {
fn from(sig: Signature) -> Self {
sig.0.to_be_bytes()
}
}