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, serde::Serialize, serde::Deserialize)]
11        #[serde(transparent)]
12        pub struct $name(DigestBytes);
13
14        impl $name {
15            /// The raw 32 BLAKE3 output bytes.
16            #[must_use]
17            pub const fn as_bytes(&self) -> &[u8; 32] {
18                &self.0.0
19            }
20        }
21
22        impl From<[u8; 32]> for $name {
23            fn from(bytes: [u8; 32]) -> Self {
24                Self(DigestBytes(bytes))
25            }
26        }
27
28        impl fmt::Display for $name {
29            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30                self.0.fmt(f)
31            }
32        }
33
34        impl FromStr for $name {
35            type Err = DigestParseError;
36
37            fn from_str(text: &str) -> Result<Self, Self::Err> {
38                DigestBytes::parse(text).map(Self)
39            }
40        }
41    };
42}
43
44digest_newtype!(
45    /// Canonical content identity of an authored [`crate::Program`].
46    ///
47    /// Covers rules, body literals and arguments in authored order and every
48    /// term, filter, expression and aggregate kind. Displays as lowercase hex.
49    ProgramDigest
50);
51
52digest_newtype!(
53    /// Identity of a [`crate::Program`] checked under one
54    /// [`crate::ValidationContract`]: equality mode, limits and termination law.
55    ContractDigest
56);
57
58digest_newtype!(
59    /// Canonical content identity of an exact [`crate::GroundTerm`].
60    ///
61    /// Covers constant text, structural kinds, predicates, argument counts,
62    /// argument order and nesting. Displays as lowercase hex. Serde transports
63    /// the fixed 32-byte output independently of its canonical hash preimage.
64    GroundTermDigest
65);