Skip to main content

uor_addr/json/
pipeline.rs

1//! `json::address*` — the JSON realization's public entry points, one per
2//! admissible σ-axis ([`crate::hash`]).
3//!
4//! 1. [`canonicalize`](crate::json::value::canonicalize) parses + emits
5//!    the JCS-RFC8785 §3 + Unicode NFC canonical form into an `alloc`
6//!    buffer (no width / depth / count caps).
7//! 2. The selected axis's `AddressModel*::forward` runs the shared ψ-tower:
8//!    the canonical bytes flow in as an ADR-060 `Borrowed` carrier and ψ₉
9//!    folds them through the bound `H` to mint the κ-label.
10//! 3. [`AddressOutcome::from_grounded`] extracts the owned κ-label +
11//!    replayable TC-05 witness.
12//!
13//! [`address`] selects `H = Sha256Hasher` (the default); [`address_blake3`],
14//! [`address_sha3_256`], and [`address_keccak256`] select the other 32-byte
15//! axes. JSON canonicalization requires heap storage, so the entry points
16//! are gated behind the `alloc` feature.
17
18pub use crate::outcome::{AddressOutcome, AddressWitness, VerifyError};
19
20/// Failure modes from the JSON entry points.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum AddressFailure {
23    /// The input bytes were not valid UTF-8 JSON.
24    InvalidJson,
25    /// Defensive: foundation's catamorphism or a resolver returned a
26    /// shape violation. Unreachable for well-formed inputs.
27    PipelineFailure,
28}
29
30/// **uor-addr's JSON entry point** (σ-axis `Sha256Hasher`) — one
31/// ψ-pipeline content-address inference per JSON input, yielding a
32/// `sha256:<64hex>` κ-label.
33///
34/// # Errors
35///
36/// - [`AddressFailure::InvalidJson`] — `input_bytes` is not valid UTF-8 JSON.
37/// - [`AddressFailure::PipelineFailure`] — defensive; unreachable in normal flow.
38#[cfg(feature = "alloc")]
39pub fn address(input_bytes: &[u8]) -> Result<AddressOutcome<71>, AddressFailure> {
40    use prism::pipeline::PrismModel;
41
42    use crate::json::model::AddressModel;
43    use crate::json::value::{canonicalize, JsonCarrier};
44
45    let canonical = canonicalize(input_bytes).map_err(|_| AddressFailure::InvalidJson)?;
46    let grounded = AddressModel::forward(JsonCarrier::new(&canonical))
47        .map_err(|_| AddressFailure::PipelineFailure)?;
48    AddressOutcome::<71>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
49}
50
51/// The JSON entry point under σ-axis `Blake3Hasher` — yields a
52/// `blake3:<64hex>` κ-label. See [`address`] for the error contract.
53///
54/// # Errors
55///
56/// As [`address`].
57#[cfg(feature = "alloc")]
58pub fn address_blake3(input_bytes: &[u8]) -> Result<AddressOutcome<71>, AddressFailure> {
59    use prism::pipeline::PrismModel;
60
61    use crate::json::model::AddressModelBlake3;
62    use crate::json::value::{canonicalize, JsonCarrier};
63
64    let canonical = canonicalize(input_bytes).map_err(|_| AddressFailure::InvalidJson)?;
65    let grounded = AddressModelBlake3::forward(JsonCarrier::new(&canonical))
66        .map_err(|_| AddressFailure::PipelineFailure)?;
67    AddressOutcome::<71>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
68}
69
70/// The JSON entry point under σ-axis `Sha3_256Hasher` — yields a
71/// `sha3-256:<64hex>` κ-label. See [`address`] for the error contract.
72///
73/// # Errors
74///
75/// As [`address`].
76#[cfg(feature = "alloc")]
77pub fn address_sha3_256(input_bytes: &[u8]) -> Result<AddressOutcome<73>, AddressFailure> {
78    use prism::pipeline::PrismModel;
79
80    use crate::json::model::AddressModelSha3_256;
81    use crate::json::value::{canonicalize, JsonCarrier};
82
83    let canonical = canonicalize(input_bytes).map_err(|_| AddressFailure::InvalidJson)?;
84    let grounded = AddressModelSha3_256::forward(JsonCarrier::new(&canonical))
85        .map_err(|_| AddressFailure::PipelineFailure)?;
86    AddressOutcome::<73>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
87}
88
89/// The JSON entry point under σ-axis `Keccak256Hasher` — yields a
90/// `keccak256:<64hex>` κ-label. See [`address`] for the error contract.
91///
92/// # Errors
93///
94/// As [`address`].
95#[cfg(feature = "alloc")]
96pub fn address_keccak256(input_bytes: &[u8]) -> Result<AddressOutcome<74>, AddressFailure> {
97    use prism::pipeline::PrismModel;
98
99    use crate::json::model::AddressModelKeccak256;
100    use crate::json::value::{canonicalize, JsonCarrier};
101
102    let canonical = canonicalize(input_bytes).map_err(|_| AddressFailure::InvalidJson)?;
103    let grounded = AddressModelKeccak256::forward(JsonCarrier::new(&canonical))
104        .map_err(|_| AddressFailure::PipelineFailure)?;
105    AddressOutcome::<74>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
106}
107
108/// The JSON entry point under σ-axis `Sha512Hasher` — yields a
109/// `sha512:<128hex>` κ-label (135 bytes, 64-byte fingerprint). See
110/// [`address`] for the error contract.
111///
112/// # Errors
113///
114/// As [`address`].
115#[cfg(feature = "alloc")]
116pub fn address_sha512(input_bytes: &[u8]) -> Result<AddressOutcome<135, 64>, AddressFailure> {
117    use prism::pipeline::PrismModel;
118
119    use crate::json::model::AddressModelSha512;
120    use crate::json::value::{canonicalize, JsonCarrier};
121
122    let canonical = canonicalize(input_bytes).map_err(|_| AddressFailure::InvalidJson)?;
123    let grounded = AddressModelSha512::forward(JsonCarrier::new(&canonical))
124        .map_err(|_| AddressFailure::PipelineFailure)?;
125    AddressOutcome::<135, 64>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
126}