Skip to main content

kernel/
budget.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4// Copyright (c) 2026 2youg1 and the sprawling contributors
5
6//! Money and quantities as integers (15.3-6), and what a piece of work
7//! turned out to cost.
8//! The part `crates/kernel/spec/Budget.lean` specifies this module.
9//!
10//! There is no ceiling here and no decision that reads one. Nobody can
11//! price a piece of work before it runs, so the only brake this city has
12//! is `Halt`: it shuts a scope to new work and stops the background
13//! members inside it. What survives is the reporting half — `BudgetUse`
14//! is a payload field of `Progress::Unplanned` and of the evidence a
15//! finished run carries, which is how a person is told what was spent
16//! rather than told in advance what may be.
17
18use serde::{Deserialize, Serialize};
19
20/// One micro-USD. Decimal price lists convert at the single accounting
21/// entry point (gateway::cost); decisions never touch floats.
22#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
23#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24pub struct UsdMicros(u64);
25
26/// Whole tokens.
27#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
28#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
29pub struct Tokens(u64);
30
31/// Whole bytes.
32#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
33pub struct ByteLen(u64);
34
35macro_rules! quantity {
36    ($name:ident) => {
37        impl $name {
38            pub const fn new(value: u64) -> $name {
39                $name(value)
40            }
41
42            pub const fn get(self) -> u64 {
43                self.0
44            }
45
46            /// Overflow is `None`; the caller owns the verdict, which is
47            /// `E_INVALID_ARGS` at every site that has one. Choosing an
48            /// error story here would force it on every caller.
49            pub fn checked_add(self, other: $name) -> Option<$name> {
50                self.0.checked_add(other.0).map($name)
51            }
52        }
53    };
54}
55
56quantity!(UsdMicros);
57quantity!(Tokens);
58quantity!(ByteLen);
59
60#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
61#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
62pub struct BudgetUse {
63    pub usd: UsdMicros,
64    pub tokens: Tokens,
65}