stateknot-core 0.1.0-alpha.1

Core domain types for the StateKnot agent runtime
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Exact, checked accounting values used by budgets and provider usage.

use std::{fmt, str::FromStr};

use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use thiserror::Error;

use crate::decimal::{UnsignedDecimalError, parse_bounded_u64};

const U64_DECIMAL_PATTERN: &str = "^(0|[1-9][0-9]{0,19})$";
const CURRENCY_CODE_PATTERN: &str = "^[A-Z]{3}$";

macro_rules! define_count {
    ($name:ident, $visitor:ident, $schema_name:literal, $documentation:literal) => {
        #[doc = $documentation]
        ///
        /// The JSON wire form is a canonical unsigned decimal string. Arithmetic
        /// is available only through checked methods, so release builds cannot
        /// silently wrap accounting values.
        #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
        pub struct $name(u64);

        impl $name {
            /// Zero usage.
            pub const ZERO: Self = Self(0);

            /// Largest representable usage.
            pub const MAX: Self = Self(u64::MAX);

            /// Constructs a count from its integer representation.
            #[must_use]
            pub const fn new(value: u64) -> Self {
                Self(value)
            }

            /// Returns the underlying integer count.
            #[must_use]
            pub const fn get(self) -> u64 {
                self.0
            }

            /// Adds two counts, returning `None` on overflow.
            #[must_use]
            pub const fn checked_add(self, other: Self) -> Option<Self> {
                match self.0.checked_add(other.0) {
                    Some(value) => Some(Self(value)),
                    None => None,
                }
            }

            /// Subtracts two counts, returning `None` on underflow.
            #[must_use]
            pub const fn checked_sub(self, other: Self) -> Option<Self> {
                match self.0.checked_sub(other.0) {
                    Some(value) => Some(Self(value)),
                    None => None,
                }
            }

            /// Multiplies a count by an integer, returning `None` on overflow.
            #[must_use]
            pub const fn checked_mul(self, multiplier: u64) -> Option<Self> {
                match self.0.checked_mul(multiplier) {
                    Some(value) => Some(Self(value)),
                    None => None,
                }
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                self.0.fmt(formatter)
            }
        }

        impl FromStr for $name {
            type Err = CountParseError;

            fn from_str(value: &str) -> Result<Self, Self::Err> {
                parse_bounded_u64(value, u64::MAX)
                    .map(Self)
                    .map_err(CountParseError::from_decimal_error)
            }
        }

        impl From<u64> for $name {
            fn from(value: u64) -> Self {
                Self::new(value)
            }
        }

        impl From<$name> for u64 {
            fn from(value: $name) -> Self {
                value.0
            }
        }

        impl Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                serializer.collect_str(self)
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'de>,
            {
                deserializer.deserialize_str($visitor)
            }
        }

        struct $visitor;

        impl de::Visitor<'_> for $visitor {
            type Value = $name;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(formatter, "a canonical decimal string containing a {}", $schema_name)
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                value.parse().map_err(E::custom)
            }
        }

        impl JsonSchema for $name {
            fn schema_name() -> std::borrow::Cow<'static, str> {
                $schema_name.into()
            }

            fn schema_id() -> std::borrow::Cow<'static, str> {
                concat!(module_path!(), "::", $schema_name).into()
            }

            fn json_schema(_: &mut SchemaGenerator) -> Schema {
                json_schema!({
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20,
                    "pattern": U64_DECIMAL_PATTERN
                })
            }

            fn inline_schema() -> bool {
                true
            }
        }
    };
}

define_count!(
    TokenCount,
    TokenCountVisitor,
    "TokenCount",
    "A count of model input, cached-input, reasoning, or output tokens."
);
define_count!(
    ByteCount,
    ByteCountVisitor,
    "ByteCount",
    "A count of input, output, event, checkpoint, or artifact bytes."
);
define_count!(
    ExecutionCount,
    ExecutionCountVisitor,
    "ExecutionCount",
    "A count of graph steps, model attempts, tool calls, branches, delegations, or retries."
);

/// Parse failure for a canonical token or byte count.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CountParseError {
    /// The encoded count was empty or contained a non-decimal byte.
    #[error("count must contain only unsigned ASCII decimal digits")]
    InvalidFormat,

    /// The encoded count exceeded the maximum canonical byte length.
    #[error("count is {actual} bytes; maximum is {max}")]
    TooLong {
        /// Maximum accepted byte length.
        max: usize,
        /// Observed byte length.
        actual: usize,
    },

    /// The encoded count contained a leading zero.
    #[error("count contains a leading zero")]
    NonCanonical,

    /// The encoded count exceeded the `u64` range.
    #[error("count exceeds the supported range")]
    Overflow,
}

impl CountParseError {
    fn from_decimal_error(error: UnsignedDecimalError) -> Self {
        match error {
            UnsignedDecimalError::Empty | UnsignedDecimalError::InvalidCharacter { .. } => {
                Self::InvalidFormat
            }
            UnsignedDecimalError::TooLong { max, actual } => Self::TooLong { max, actual },
            UnsignedDecimalError::LeadingZero => Self::NonCanonical,
            UnsignedDecimalError::Overflow => Self::Overflow,
        }
    }
}

/// A structurally valid ISO 4217 alphabetic currency code.
///
/// This type intentionally validates the stable three-uppercase-ASCII-letter
/// structure, not mutable current-currency membership. Provider configuration
/// validates membership against a versioned ISO 4217 catalog; durable events
/// must remain readable after a code becomes historical.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CurrencyCode([u8; Self::LEN]);

impl CurrencyCode {
    /// Encoded length in bytes.
    pub const LEN: usize = 3;

    /// Validates an alphabetic currency code.
    ///
    /// # Errors
    ///
    /// Returns [`CurrencyCodeError::InvalidCharacter`] when any byte is not an
    /// uppercase ASCII letter.
    pub fn new(bytes: [u8; Self::LEN]) -> Result<Self, CurrencyCodeError> {
        if let Some(index) = bytes.iter().position(|byte| !byte.is_ascii_uppercase()) {
            return Err(CurrencyCodeError::InvalidCharacter { index });
        }
        Ok(Self(bytes))
    }

    /// Returns the three canonical ASCII bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; Self::LEN] {
        &self.0
    }
}

impl fmt::Debug for CurrencyCode {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("CurrencyCode")
            .field(&format_args!("{self}"))
            .finish()
    }
}

impl fmt::Display for CurrencyCode {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let value = std::str::from_utf8(&self.0).map_err(|_| fmt::Error)?;
        formatter.write_str(value)
    }
}

impl FromStr for CurrencyCode {
    type Err = CurrencyCodeError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.len() != Self::LEN {
            return Err(CurrencyCodeError::InvalidLength {
                expected: Self::LEN,
                actual: value.len(),
            });
        }
        let bytes: [u8; Self::LEN] =
            value
                .as_bytes()
                .try_into()
                .map_err(|_| CurrencyCodeError::InvalidLength {
                    expected: Self::LEN,
                    actual: value.len(),
                })?;
        Self::new(bytes)
    }
}

impl Serialize for CurrencyCode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_str(self)
    }
}

impl<'de> Deserialize<'de> for CurrencyCode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(CurrencyCodeVisitor)
    }
}

struct CurrencyCodeVisitor;

impl de::Visitor<'_> for CurrencyCodeVisitor {
    type Value = CurrencyCode;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a three-letter uppercase ISO 4217 currency code")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        value.parse().map_err(E::custom)
    }
}

impl JsonSchema for CurrencyCode {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "CurrencyCode".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::CurrencyCode").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "string",
            "minLength": 3,
            "maxLength": 3,
            "pattern": CURRENCY_CODE_PATTERN
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

/// Validation failure for a [`CurrencyCode`].
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CurrencyCodeError {
    /// The code was not exactly three bytes.
    #[error("currency code is {actual} bytes; expected {expected}")]
    InvalidLength {
        /// Required encoded length in bytes.
        expected: usize,
        /// Observed byte length.
        actual: usize,
    },

    /// A byte was not an uppercase ASCII letter.
    #[error("currency code contains an invalid byte at offset {index}")]
    InvalidCharacter {
        /// Zero-based byte offset of the invalid value.
        index: usize,
    },
}

/// A non-negative known monetary cost in integer micro-units.
///
/// The stable JSON form is an object with exactly `currency` and
/// `micro_units`; the latter is a canonical decimal string. Unknown cost is
/// represented by absence in an enclosing usage value, not by zero money.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Money {
    currency: CurrencyCode,
    #[serde(with = "crate::decimal::serde_u64")]
    micro_units: u64,
}

impl Money {
    /// Constructs a known monetary amount.
    #[must_use]
    pub const fn new(currency: CurrencyCode, micro_units: u64) -> Self {
        Self {
            currency,
            micro_units,
        }
    }

    /// Constructs zero cost in a specific currency.
    #[must_use]
    pub const fn zero(currency: CurrencyCode) -> Self {
        Self::new(currency, 0)
    }

    /// Returns the currency code.
    #[must_use]
    pub const fn currency(self) -> CurrencyCode {
        self.currency
    }

    /// Returns the integer count of micro-units.
    #[must_use]
    pub const fn micro_units(self) -> u64 {
        self.micro_units
    }

    /// Adds amounts of the same currency without overflow.
    ///
    /// # Errors
    ///
    /// Returns [`MoneyArithmeticError::CurrencyMismatch`] for unlike
    /// currencies or [`MoneyArithmeticError::Overflow`] when the result
    /// exceeds `u64`.
    pub fn checked_add(self, other: Self) -> Result<Self, MoneyArithmeticError> {
        self.ensure_same_currency(other)?;
        let micro_units = self
            .micro_units
            .checked_add(other.micro_units)
            .ok_or(MoneyArithmeticError::Overflow)?;
        Ok(Self::new(self.currency, micro_units))
    }

    /// Subtracts amounts of the same currency without underflow.
    ///
    /// # Errors
    ///
    /// Returns [`MoneyArithmeticError::CurrencyMismatch`] for unlike
    /// currencies or [`MoneyArithmeticError::Underflow`] when `other` is
    /// greater than `self`.
    pub fn checked_sub(self, other: Self) -> Result<Self, MoneyArithmeticError> {
        self.ensure_same_currency(other)?;
        let micro_units = self
            .micro_units
            .checked_sub(other.micro_units)
            .ok_or(MoneyArithmeticError::Underflow)?;
        Ok(Self::new(self.currency, micro_units))
    }

    /// Multiplies this amount by an integer without overflow.
    ///
    /// # Errors
    ///
    /// Returns [`MoneyArithmeticError::Overflow`] when the result exceeds
    /// `u64`.
    pub fn checked_mul(self, multiplier: u64) -> Result<Self, MoneyArithmeticError> {
        let micro_units = self
            .micro_units
            .checked_mul(multiplier)
            .ok_or(MoneyArithmeticError::Overflow)?;
        Ok(Self::new(self.currency, micro_units))
    }

    fn ensure_same_currency(self, other: Self) -> Result<(), MoneyArithmeticError> {
        if self.currency == other.currency {
            Ok(())
        } else {
            Err(MoneyArithmeticError::CurrencyMismatch {
                left: self.currency,
                right: other.currency,
            })
        }
    }
}

impl JsonSchema for Money {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "Money".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::Money").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "object",
            "additionalProperties": false,
            "required": ["currency", "micro_units"],
            "properties": {
                "currency": {
                    "type": "string",
                    "minLength": 3,
                    "maxLength": 3,
                    "pattern": CURRENCY_CODE_PATTERN
                },
                "micro_units": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20,
                    "pattern": U64_DECIMAL_PATTERN
                }
            }
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

/// Failure from checked monetary arithmetic.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum MoneyArithmeticError {
    /// Arithmetic was requested across different currencies.
    #[error("cannot combine {left} and {right} without an explicit conversion")]
    CurrencyMismatch {
        /// Currency of the left operand.
        left: CurrencyCode,
        /// Currency of the right operand.
        right: CurrencyCode,
    },

    /// The result exceeded the `u64` micro-unit range.
    #[error("monetary arithmetic overflowed")]
    Overflow,

    /// Subtraction would have produced a negative amount.
    #[error("monetary subtraction would produce a negative amount")]
    Underflow,
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use serde_json::{Value, from_str, from_value, json, to_string, to_value};

    fn usd() -> CurrencyCode {
        "USD".parse().unwrap()
    }

    #[test]
    fn counts_use_exact_decimal_strings_and_checked_arithmetic() {
        for value in [0, 1, (1_u64 << 53) - 1, 1_u64 << 53, u64::MAX] {
            let tokens = TokenCount::new(value);
            let bytes = ByteCount::new(value);
            let executions = ExecutionCount::new(value);
            assert_eq!(tokens.to_string().parse::<TokenCount>().unwrap(), tokens);
            assert_eq!(bytes.to_string().parse::<ByteCount>().unwrap(), bytes);
            assert_eq!(
                executions.to_string().parse::<ExecutionCount>().unwrap(),
                executions
            );
            assert_eq!(to_string(&tokens).unwrap(), format!("\"{value}\""));
            assert_eq!(to_string(&bytes).unwrap(), format!("\"{value}\""));
            assert_eq!(to_string(&executions).unwrap(), format!("\"{value}\""));
        }

        assert_eq!(TokenCount::MAX.checked_add(TokenCount::new(1)), None);
        assert_eq!(ByteCount::ZERO.checked_sub(ByteCount::new(1)), None);
        assert_eq!(TokenCount::new(4).checked_mul(3), Some(TokenCount::new(12)));
    }

    #[test]
    fn counts_reject_noncanonical_or_inexact_json() {
        for value in ["", "00", "01", "+1", "-1", "1.0", "1e3", " 1"] {
            assert!(value.parse::<TokenCount>().is_err(), "accepted {value:?}");
            assert!(value.parse::<ByteCount>().is_err(), "accepted {value:?}");
            assert!(
                value.parse::<ExecutionCount>().is_err(),
                "accepted {value:?}"
            );
        }
        assert_eq!(
            "18446744073709551616".parse::<TokenCount>(),
            Err(CountParseError::Overflow)
        );
        assert!(from_value::<TokenCount>(json!(1)).is_err());
        assert!(from_value::<ByteCount>(Value::Null).is_err());
    }

    #[test]
    fn count_schemas_match_runtime_wire_bounds() {
        for schema in [
            to_value(schemars::schema_for!(TokenCount)).unwrap(),
            to_value(schemars::schema_for!(ByteCount)).unwrap(),
            to_value(schemars::schema_for!(ExecutionCount)).unwrap(),
        ] {
            assert_eq!(schema["type"], "string");
            assert_eq!(schema["minLength"], 1);
            assert_eq!(schema["maxLength"], 20);
            assert_eq!(schema["pattern"], U64_DECIMAL_PATTERN);
        }
    }

    #[test]
    fn currency_codes_enforce_stable_iso_structure() {
        for value in ["USD", "EUR", "XAU", "BGN"] {
            let code = value.parse::<CurrencyCode>().unwrap();
            assert_eq!(code.to_string(), value);
            assert_eq!(to_string(&code).unwrap(), format!("\"{value}\""));
        }

        for value in ["", "US", "USDD", "usd", "UsD", "U1D", "Â¥EN", " USD"] {
            assert!(value.parse::<CurrencyCode>().is_err(), "accepted {value:?}");
        }
        assert!(from_value::<CurrencyCode>(json!("usd")).is_err());
        assert!(from_value::<CurrencyCode>(json!(123)).is_err());
    }

    #[test]
    fn money_round_trips_and_rejects_ambiguous_objects() {
        let money = Money::new(usd(), 1_234_567);
        let encoded = to_value(money).unwrap();
        assert_eq!(
            encoded,
            json!({"currency": "USD", "micro_units": "1234567"})
        );
        assert_eq!(from_value::<Money>(encoded).unwrap(), money);

        for invalid in [
            json!({"currency": "USD", "micro_units": 123}),
            json!({"currency": "usd", "micro_units": "123"}),
            json!({"currency": "USD", "micro_units": "01"}),
            json!({"currency": "USD", "micro_units": "18446744073709551616"}),
            json!({"currency": "USD", "micro_units": "1", "extra": true}),
            json!({"currency": "USD"}),
        ] {
            assert!(
                from_value::<Money>(invalid.clone()).is_err(),
                "accepted {invalid}"
            );
        }
    }

    #[test]
    fn money_arithmetic_requires_one_currency_and_never_wraps() {
        let eur: CurrencyCode = "EUR".parse().unwrap();
        let one = Money::new(usd(), 1);
        let two = Money::new(usd(), 2);

        assert_eq!(one.checked_add(two), Ok(Money::new(usd(), 3)));
        assert_eq!(two.checked_sub(one), Ok(one));
        assert_eq!(two.checked_mul(3), Ok(Money::new(usd(), 6)));
        assert_eq!(
            Money::new(usd(), u64::MAX).checked_add(one),
            Err(MoneyArithmeticError::Overflow)
        );
        assert_eq!(one.checked_sub(two), Err(MoneyArithmeticError::Underflow));
        assert_eq!(
            one.checked_add(Money::new(eur, 1)),
            Err(MoneyArithmeticError::CurrencyMismatch {
                left: usd(),
                right: eur,
            })
        );
    }

    #[test]
    fn money_schema_requires_exact_fields_and_string_amount() {
        let schema = to_value(schemars::schema_for!(Money)).unwrap();
        assert_eq!(schema["type"], "object");
        assert_eq!(schema["additionalProperties"], false);
        assert_eq!(schema["required"], json!(["currency", "micro_units"]));
        assert_eq!(
            schema["properties"]["currency"]["pattern"],
            CURRENCY_CODE_PATTERN
        );
        assert_eq!(
            schema["properties"]["micro_units"]["pattern"],
            U64_DECIMAL_PATTERN
        );
    }

    proptest! {
        #[test]
        fn checked_count_arithmetic_matches_u64(left in any::<u64>(), right in any::<u64>()) {
            let left_tokens = TokenCount::new(left);
            let right_tokens = TokenCount::new(right);
            prop_assert_eq!(
                left_tokens.checked_add(right_tokens).map(TokenCount::get),
                left.checked_add(right)
            );
            prop_assert_eq!(
                left_tokens.checked_sub(right_tokens).map(TokenCount::get),
                left.checked_sub(right)
            );

            let left_bytes = ByteCount::new(left);
            let right_bytes = ByteCount::new(right);
            prop_assert_eq!(
                left_bytes.checked_add(right_bytes).map(ByteCount::get),
                left.checked_add(right)
            );
            prop_assert_eq!(
                left_bytes.checked_sub(right_bytes).map(ByteCount::get),
                left.checked_sub(right)
            );
        }

        #[test]
        fn count_wire_round_trip_preserves_all_u64_values(value in any::<u64>()) {
            let count = TokenCount::new(value);
            let encoded = to_string(&count).unwrap();
            prop_assert_eq!(from_str::<TokenCount>(&encoded).unwrap(), count);
        }

        #[test]
        fn checked_money_arithmetic_matches_u64(left in any::<u64>(), right in any::<u64>()) {
            let left_money = Money::new(usd(), left);
            let right_money = Money::new(usd(), right);
            prop_assert_eq!(
                left_money.checked_add(right_money).ok().map(Money::micro_units),
                left.checked_add(right)
            );
            prop_assert_eq!(
                left_money.checked_sub(right_money).ok().map(Money::micro_units),
                left.checked_sub(right)
            );
        }
    }
}