flow_abi/limits.rs
1//! The limits a decoded value is held to (`execution.md`, *Limits*).
2
3/// How large a decoded value may be.
4///
5/// Every host decodes within [`Limits::LANGUAGE`]; other values exist only
6/// so that tests can reach a limit with a small value.
7///
8/// A value past any of these does not decode, like any other value that does
9/// not match its type: in a Tool reply that is no answer, so the call stays
10/// open and the run stops, never a fault (`abi.md`, *Decoding*).
11#[derive(Clone, Copy, PartialEq, Eq, Debug)]
12pub struct Limits {
13 /// How deeply the decoded value may nest: a scalar is 0 deep, and a
14 /// list, set, dictionary, tuple, record or enum value one deeper than its
15 /// deepest part, as the evaluator counts.
16 pub depth: usize,
17 /// How many bits an `Int` may take, sign excluded, and so the integer
18 /// any number's significant digits make, which bounds a `Decimal`'s
19 /// coefficient.
20 pub int_bits: u64,
21 /// How many bytes one `Text` or `Bytes` may hold, how many digits a
22 /// number may take written in plain decimal notation, and how many
23 /// elements written in no bytes, such as `Unit`s, one binary value may
24 /// hold in all its collections together.
25 pub text_bytes: usize,
26}
27
28impl Limits {
29 /// The language's limits, which every conforming implementation holds a
30 /// program to exactly: no host raises or lowers them.
31 pub const LANGUAGE: Limits = Limits {
32 depth: 256,
33 int_bits: 4096,
34 text_bytes: 16 * 1024 * 1024,
35 };
36
37 /// No limit at all: for bytes this implementation wrote itself, such as
38 /// a value already held, or type metadata, whose nesting no program
39 /// value's limit bounds.
40 pub const UNBOUNDED: Limits = Limits {
41 depth: usize::MAX,
42 int_bits: u64::MAX,
43 text_bytes: usize::MAX,
44 };
45
46 /// The reply limit: the most bytes one Tool reply may take.
47 pub const REPLY_BYTES: usize = 16 * 1024 * 1024;
48
49 /// How deeply JSON arrays and objects may nest before decoding refuses
50 /// the text unread.
51 ///
52 /// A JSON level makes at least half a level of value — an enum with a
53 /// named payload is two objects around one value — so text nested deeper
54 /// than this could not decode within [`Limits::depth`] in any case.
55 pub(crate) fn json_depth(&self) -> usize {
56 self.depth.saturating_mul(2).saturating_add(1)
57 }
58}
59
60impl Default for Limits {
61 fn default() -> Self {
62 Limits::LANGUAGE
63 }
64}