boxology-contract 0.1.1

Core identifiers, descriptors, and call types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::error::Error;
use std::fmt;

use crate::{ContractValue, SlotValue, ValueRef};

/// A transport-neutral raw-value tree.
///
/// Objects preserve entry order and duplicate keys. Numbers preserve their
/// original RFC 8259 token without imposing a magnitude limit.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum OpaqueTree {
    Null,
    Bool(bool),
    Number(OpaqueNumber),
    String(String),
    List(Vec<OpaqueTree>),
    Object(Vec<(String, OpaqueTree)>),
}

/// A validated, exactly preserved RFC 8259 number token.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct OpaqueNumber(String);

impl OpaqueNumber {
    /// Validates and preserves an RFC 8259 number token.
    pub fn new(text: impl Into<String>) -> Result<Self, OpaqueNumberError> {
        let text = text.into();
        if valid_number(&text) {
            Ok(Self(text))
        } else {
            Err(OpaqueNumberError { text })
        }
    }

    /// Returns the original number token.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// An invalid RFC 8259 number token.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpaqueNumberError {
    text: String,
}

impl fmt::Display for OpaqueNumberError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "invalid RFC 8259 number token: {:?}", self.text)
    }
}

impl Error for OpaqueNumberError {}

/// An opaque raw-value payload whose diagnostic representation is redacted.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct OpaquePayload(OpaqueTree);

impl OpaquePayload {
    /// Wraps a raw-value tree as an opaque payload.
    pub fn new(tree: OpaqueTree) -> Self {
        Self(tree)
    }

    /// Explicitly reveals the sensitive raw-value tree.
    ///
    /// Callers must treat the returned content as sensitive data.
    pub fn reveal(&self) -> &OpaqueTree {
        &self.0
    }

    /// Returns an independently owned redacted clone for onward transmission.
    ///
    /// This operation never reveals the payload content.
    pub fn forward(&self) -> OpaquePayload {
        self.clone()
    }

    /// Captures a slot without assuming its descriptor or transport syntax.
    ///
    /// `Missing` and `Null` both become `OpaqueTree::Null`: their distinction
    /// is intentionally lost when the payload shape is unknown.
    pub(crate) fn capture(slot: &SlotValue) -> Self {
        Self(capture_slot(slot))
    }
}

impl fmt::Debug for OpaquePayload {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("OpaquePayload(<redacted>)")
    }
}

fn capture_slot(slot: &SlotValue) -> OpaqueTree {
    match slot {
        SlotValue::Missing | SlotValue::Null => OpaqueTree::Null,
        SlotValue::Value(value) => capture_value(value),
    }
}

fn capture_value(value: &ContractValue) -> OpaqueTree {
    match value.view() {
        ValueRef::Null => OpaqueTree::Null,
        ValueRef::Bool(value) => OpaqueTree::Bool(value),
        ValueRef::I64(value) => number(value.to_string()),
        ValueRef::U64(value) => number(value.to_string()),
        ValueRef::F32(value) => number(value.to_string()),
        ValueRef::F64(value) => number(value.to_string()),
        ValueRef::String(value) => OpaqueTree::String(value.into()),
        ValueRef::Bytes(value) => OpaqueTree::Object(vec![(
            "base64".into(),
            OpaqueTree::String(standard_base64(value)),
        )]),
        ValueRef::List(values) => OpaqueTree::List(values.iter().map(capture_value).collect()),
        ValueRef::Object(object) => OpaqueTree::Object(
            object
                .entries()
                .map(|(key, value)| (key.into(), capture_value(value)))
                .collect(),
        ),
        ValueRef::Enum { tag, payload } => OpaqueTree::Object(vec![
            ("tag".into(), OpaqueTree::String(tag.into())),
            ("payload".into(), capture_slot(payload)),
        ]),
        ValueRef::Opaque(payload) => payload.reveal().clone(),
        ValueRef::Sensitive(inner) => capture_value(inner),
    }
}

fn number(text: String) -> OpaqueTree {
    OpaqueTree::Number(
        OpaqueNumber::new(text).expect("finite contract numbers format as RFC 8259 tokens"),
    )
}

fn standard_base64(bytes: &[u8]) -> String {
    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut output = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for chunk in bytes.chunks(3) {
        let first = chunk[0];
        let second = chunk.get(1).copied().unwrap_or(0);
        let third = chunk.get(2).copied().unwrap_or(0);
        output.push(ALPHABET[(first >> 2) as usize] as char);
        output.push(ALPHABET[(((first & 0x03) << 4) | (second >> 4)) as usize] as char);
        if chunk.len() > 1 {
            output.push(ALPHABET[(((second & 0x0f) << 2) | (third >> 6)) as usize] as char);
        } else {
            output.push('=');
        }
        if chunk.len() > 2 {
            output.push(ALPHABET[(third & 0x3f) as usize] as char);
        } else {
            output.push('=');
        }
    }
    output
}

fn valid_number(text: &str) -> bool {
    let bytes = text.as_bytes();
    let mut index = usize::from(bytes.first() == Some(&b'-'));
    match bytes.get(index) {
        Some(b'0') => index += 1,
        Some(b'1'..=b'9') => {
            index += 1;
            while matches!(bytes.get(index), Some(b'0'..=b'9')) {
                index += 1;
            }
        }
        _ => return false,
    }
    if bytes.get(index) == Some(&b'.') {
        index += 1;
        let start = index;
        while matches!(bytes.get(index), Some(b'0'..=b'9')) {
            index += 1;
        }
        if index == start {
            return false;
        }
    }
    if matches!(bytes.get(index), Some(b'e' | b'E')) {
        index += 1;
        if matches!(bytes.get(index), Some(b'+' | b'-')) {
            index += 1;
        }
        let start = index;
        while matches!(bytes.get(index), Some(b'0'..=b'9')) {
            index += 1;
        }
        if index == start {
            return false;
        }
    }
    index == bytes.len()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ContractValue, SlotValue};

    fn value(value: ContractValue) -> SlotValue {
        SlotValue::Value(value)
    }

    fn captured(slot: &SlotValue) -> OpaqueTree {
        OpaquePayload::capture(slot).reveal().clone()
    }

    #[test]
    fn numbers_accept_exact_rfc_grammar_and_preserve_tokens() {
        let accepted = [
            "0",
            "-0",
            "10",
            "-42",
            "0.01",
            "1.0",
            "1e0",
            "1E+9",
            "-1.25e-999999",
            "123456789012345678901234567890",
        ];
        for token in accepted {
            assert_eq!(OpaqueNumber::new(token).unwrap().as_str(), token);
        }

        let rejected = [
            "", "-", "00", "01", "-01", ".1", "1.", "1e", "1e+", "+1", " 1", "1 ", "1x", "NaN",
        ];
        for token in rejected {
            let error = OpaqueNumber::new(token).unwrap_err();
            assert!(error.to_string().contains(token));
        }
    }

    #[test]
    fn object_order_and_duplicate_keys_are_preserved() {
        let tree = OpaqueTree::Object(vec![
            ("x".into(), OpaqueTree::Null),
            ("x".into(), OpaqueTree::Bool(true)),
            (
                "y".into(),
                OpaqueTree::Number(OpaqueNumber::new("1e2").unwrap()),
            ),
        ]);
        let OpaqueTree::Object(entries) = &tree else {
            panic!()
        };
        assert_eq!(entries[0].0, "x");
        assert_eq!(entries[1].0, "x");
        assert_eq!(entries[2].0, "y");
    }

    #[test]
    fn reveal_is_exact_and_forward_is_an_independent_redacted_clone() {
        let tree = OpaqueTree::String("explicitly-revealed".into());
        let payload = OpaquePayload::new(tree.clone());
        assert_eq!(payload.reveal(), &tree);
        assert!(format!("{:?}", payload.reveal()).contains("explicitly-revealed"));
        assert_eq!(format!("{payload:?}"), "OpaquePayload(<redacted>)");

        let forwarded = payload.forward();
        assert_eq!(forwarded, payload);
        let (OpaqueTree::String(original), OpaqueTree::String(clone)) =
            (payload.reveal(), forwarded.reveal())
        else {
            panic!()
        };
        assert_ne!(original.as_ptr(), clone.as_ptr());
        assert_eq!(format!("{forwarded:?}"), "OpaquePayload(<redacted>)");
    }

    #[test]
    fn public_opaque_types_are_send_sync_and_static() {
        fn assert_bounds<T: Send + Sync + 'static>() {}
        assert_bounds::<OpaqueTree>();
        assert_bounds::<OpaqueNumber>();
        assert_bounds::<OpaqueNumberError>();
        assert_bounds::<OpaquePayload>();
    }

    #[test]
    fn capture_covers_slot_states_and_every_scalar_kind() {
        assert_eq!(captured(&SlotValue::Missing), OpaqueTree::Null);
        assert_eq!(captured(&SlotValue::Null), OpaqueTree::Null);
        assert_eq!(captured(&value(ContractValue::null())), OpaqueTree::Null);
        assert_eq!(
            captured(&value(ContractValue::bool(true))),
            OpaqueTree::Bool(true)
        );
        assert_eq!(
            captured(&value(ContractValue::string("text"))),
            OpaqueTree::String("text".into())
        );

        let cases = [
            (ContractValue::i64(i64::MIN), i64::MIN.to_string()),
            (ContractValue::u64(u64::MAX), u64::MAX.to_string()),
            (ContractValue::f32(-0.0).unwrap(), "-0".into()),
            (ContractValue::f64(1.5).unwrap(), "1.5".into()),
        ];
        for (input, token) in cases {
            let OpaqueTree::Number(number) = captured(&value(input)) else {
                panic!()
            };
            assert_eq!(number.as_str(), token);
            assert!(OpaqueNumber::new(token).is_ok());
        }
    }

    #[test]
    fn capture_uses_standard_padded_base64_for_bytes() {
        let cases: &[(&[u8], &str)] = &[
            (b"", ""),
            (b"f", "Zg=="),
            (b"fo", "Zm8="),
            (b"foo", "Zm9v"),
            (b"foob", "Zm9vYg=="),
            (b"foobar", "Zm9vYmFy"),
            (&[0xfb, 0xff], "+/8="),
        ];
        for (bytes, encoded) in cases {
            assert_eq!(
                captured(&value(ContractValue::bytes(bytes.to_vec()))),
                OpaqueTree::Object(vec![(
                    "base64".into(),
                    OpaqueTree::String((*encoded).into()),
                )])
            );
        }
    }

    #[test]
    fn capture_recurses_through_lists_objects_and_enum_payloads_in_order() {
        let input = ContractValue::object([
            (
                "list".into(),
                ContractValue::list([ContractValue::bool(false), ContractValue::string("item")]),
            ),
            (
                "enum".into(),
                ContractValue::enum_value(
                    "known",
                    value(
                        ContractValue::object([("inner".into(), ContractValue::i64(3))]).unwrap(),
                    ),
                ),
            ),
            (
                "unit".into(),
                ContractValue::enum_value("unit", SlotValue::Missing),
            ),
        ])
        .unwrap();
        let expected = OpaqueTree::Object(vec![
            (
                "list".into(),
                OpaqueTree::List(vec![
                    OpaqueTree::Bool(false),
                    OpaqueTree::String("item".into()),
                ]),
            ),
            (
                "enum".into(),
                OpaqueTree::Object(vec![
                    ("tag".into(), OpaqueTree::String("known".into())),
                    (
                        "payload".into(),
                        OpaqueTree::Object(vec![(
                            "inner".into(),
                            OpaqueTree::Number(OpaqueNumber::new("3").unwrap()),
                        )]),
                    ),
                ]),
            ),
            (
                "unit".into(),
                OpaqueTree::Object(vec![
                    ("tag".into(), OpaqueTree::String("unit".into())),
                    ("payload".into(), OpaqueTree::Null),
                ]),
            ),
        ]);
        assert_eq!(captured(&value(input)), expected);
    }

    #[test]
    fn capture_splices_opaque_and_redacts_sensitive_values() {
        const SENTINEL: &str = "captured-sensitive-runtime-value";
        let raw = OpaqueTree::Object(vec![
            ("duplicate".into(), OpaqueTree::String(SENTINEL.into())),
            ("duplicate".into(), OpaqueTree::Bool(true)),
        ]);
        let captured_opaque = OpaquePayload::capture(&value(ContractValue::opaque(
            OpaquePayload::new(raw.clone()),
        )));
        assert_eq!(captured_opaque.reveal(), &raw);

        let captured_sensitive = OpaquePayload::capture(&value(ContractValue::sensitive(
            ContractValue::string(SENTINEL),
        )));
        assert_eq!(
            captured_sensitive.reveal(),
            &OpaqueTree::String(SENTINEL.into())
        );
        let diagnostics = [
            format!("{captured_opaque:?}"),
            format!("{captured_sensitive:?}"),
            format!(
                "{:?}",
                SlotValue::Value(ContractValue::opaque(captured_sensitive.forward()))
            ),
        ];
        for diagnostic in diagnostics {
            assert!(!diagnostic.contains(SENTINEL));
            assert!(diagnostic.contains("<redacted>"));
        }
    }
}