Skip to main content

datalog_structural_model/digest/
ground_preimage.rs

1//! Canonical identities of grounded structural values.
2
3use super::{
4    domain::{CanonicalPreimage, DomainDigest},
5    output::GroundTermDigest,
6    sink::PreimageSink,
7};
8use crate::{GroundAtom, GroundTerm};
9
10impl GroundTerm {
11    /// Computes this value's canonical BLAKE3 content identity.
12    ///
13    /// Preserves exact constant text, term kinds, predicates, argument counts,
14    /// argument order and nesting. The encoding is independent of Serde and
15    /// uses the `datalog-structural-model/ground-term-digest/v1` domain.
16    /// Structural validation remains separate; this method does not select
17    /// or enforce a nesting limit.
18    #[must_use]
19    pub fn digest(&self) -> GroundTermDigest {
20        DomainDigest::digest(self)
21    }
22}
23
24impl DomainDigest for GroundTerm {
25    const DOMAIN: &'static str = "datalog-structural-model/ground-term-digest/v1";
26    type Output = GroundTermDigest;
27}
28
29impl CanonicalPreimage for GroundTerm {
30    fn write_preimage(&self, sink: &mut PreimageSink) {
31        match self {
32            Self::Constant(constant) => {
33                sink.tag(0);
34                constant.write_preimage(sink);
35            }
36            Self::TripleTerm(atom) => {
37                sink.tag(1);
38                atom.write_preimage(sink);
39            }
40        }
41    }
42}
43
44impl CanonicalPreimage for GroundAtom {
45    fn write_preimage(&self, sink: &mut PreimageSink) {
46        self.predicate.write_preimage(sink);
47        GroundTerm::write_sequence(&self.args, sink);
48    }
49}