Skip to main content

datalog_structural_model/digest/
output.rs

1//! Typed digest outputs.
2
3use std::{fmt, str::FromStr};
4
5use super::hex::{DigestBytes, DigestParseError};
6
7macro_rules! digest_newtype {
8    ($(#[$meta:meta])* $name:ident) => {
9        $(#[$meta])*
10        #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
11        pub struct $name(DigestBytes);
12
13        impl $name {
14            /// The raw 32 BLAKE3 output bytes.
15            #[must_use]
16            pub const fn as_bytes(&self) -> &[u8; 32] {
17                &self.0.0
18            }
19        }
20
21        impl From<[u8; 32]> for $name {
22            fn from(bytes: [u8; 32]) -> Self {
23                Self(DigestBytes(bytes))
24            }
25        }
26
27        impl fmt::Display for $name {
28            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29                self.0.fmt(f)
30            }
31        }
32
33        impl FromStr for $name {
34            type Err = DigestParseError;
35
36            fn from_str(text: &str) -> Result<Self, Self::Err> {
37                DigestBytes::parse(text).map(Self)
38            }
39        }
40    };
41}
42
43digest_newtype!(
44    /// Canonical content identity of an authored [`crate::Program`].
45    ///
46    /// Covers rules, body literals and arguments in authored order and every
47    /// term, filter, expression and aggregate kind. Displays as lowercase hex.
48    ProgramDigest
49);
50
51digest_newtype!(
52    /// Identity of a [`crate::Program`] checked under one
53    /// [`crate::ValidationContract`]: equality mode, limits and termination law.
54    ContractDigest
55);