lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

media.rs (2937B)


      1 use super::{ListingError, text};
      2 use alloc::string::String;
      3 use core::fmt;
      4 
      5 /// Bounded public object-reference claims, without network or decoder authority.
      6 /// The digest is a declared SHA-256 value, not evidence of obtained file bytes.
      7 #[derive(Clone, Eq, PartialEq)]
      8 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
      9 pub struct Media {
     10     url: String,
     11     #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_digest"))]
     12     sha256: [u8; 32],
     13     mime: String,
     14     alt: String,
     15 }
     16 
     17 impl Media {
     18     /// Bounds all claims before copying. URL/MIME claims are nonempty and kept
     19     /// exactly; transport approval and actual content validation belong to use.
     20     pub fn new(url: &str, sha256: [u8; 32], mime: &str, alt: &str) -> Result<Self, ListingError> {
     21         text(url, 2048, false)?;
     22         text(mime, 80, false)?;
     23         text(alt, 240, true)?;
     24         Ok(Self {
     25             url: url.into(),
     26             sha256,
     27             mime: mime.into(),
     28             alt: alt.into(),
     29         })
     30     }
     31 
     32     /// Admits exactly64 lowercase hexadecimal digits before validated capture.
     33     pub fn from_hex_digest(
     34         url: &str,
     35         sha256: &str,
     36         mime: &str,
     37         alt: &str,
     38     ) -> Result<Self, ListingError> {
     39         if sha256.len() != 64 {
     40             return Err(ListingError::InvalidMediaDigest);
     41         }
     42         let digit = |byte| match byte {
     43             b'0'..=b'9' => Ok(byte - b'0'),
     44             b'a'..=b'f' => Ok(byte - b'a' + 10),
     45             _ => Err(ListingError::InvalidMediaDigest),
     46         };
     47         let mut digest = [0; 32];
     48         let (pairs, _) = sha256.as_bytes().as_chunks::<2>();
     49         for (out, &[a, b]) in digest.iter_mut().zip(pairs) {
     50             *out = digit(a)? * 16 + digit(b)?;
     51         }
     52         Self::new(url, digest, mime, alt)
     53     }
     54     /// Borrows the original claim; this grants no endpoint or fetch trust.
     55     pub fn url(&self) -> &str {
     56         &self.url
     57     }
     58     /// Borrows the exact declared digest; file verification is separate.
     59     pub const fn sha256(&self) -> &[u8; 32] {
     60         &self.sha256
     61     }
     62     /// Borrows the declared MIME claim; this is not a supported-codec promise.
     63     pub fn mime(&self) -> &str {
     64         &self.mime
     65     }
     66     /// Borrows the exact alternative text without rendering it.
     67     pub fn alt(&self) -> &str {
     68         &self.alt
     69     }
     70 }
     71 impl fmt::Debug for Media {
     72     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     73         f.write_str("Media(<redacted>)")
     74     }
     75 }
     76 #[cfg(feature = "serde")]
     77 fn serialize_digest<S: serde::Serializer>(
     78     value: &[u8; 32],
     79     serializer: S,
     80 ) -> Result<S::Ok, S::Error> {
     81     struct Digest<'a>(&'a [u8; 32]);
     82     impl fmt::Display for Digest<'_> {
     83         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     84             for byte in self.0 {
     85                 write!(f, "{byte:02x}")?;
     86             }
     87             Ok(())
     88         }
     89     }
     90     serializer.collect_str(&Digest(value))
     91 }