Skip to main content

flow_value/
error.rs

1//! The ways an operation in this crate can decline.
2
3use std::fmt;
4
5/// Why an arithmetic operation produced no value.
6///
7/// `data.md` makes a zero divisor the only operation on its types that faults.
8/// The evaluator turns either of these into a fault; neither is a value a
9/// program can observe.
10#[derive(Clone, Copy, PartialEq, Eq, Debug)]
11pub enum Fault {
12    /// `int.div`, `%` or `/` was given a zero divisor.
13    DivisionByZero,
14    /// The exact result exists but its scale cannot be represented: a
15    /// `Decimal` whose power of ten falls outside a signed 64-bit exponent.
16    ///
17    /// This is the exponent limit `execution.md` sets, not a rule about the
18    /// number.
19    Limit,
20}
21
22impl fmt::Display for Fault {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.write_str(match self {
25            Fault::DivisionByZero => "division by zero",
26            Fault::Limit => "the result's exponent does not fit in 64 bits",
27        })
28    }
29}
30
31impl std::error::Error for Fault {}
32
33/// Why equality, ordering or text was refused.
34///
35/// `types.md` makes each of these a compile error on a type that is not data,
36/// so a checked program never reaches either case. They exist so that a defect
37/// upstream surfaces as a typed refusal rather than as an answer invented here.
38#[derive(Clone, Copy, PartialEq, Eq, Debug)]
39pub enum DataError {
40    /// A function, a task, a host token or another evaluator-owned value was
41    /// reached: none of them has equality, an order or a text form.
42    NotData,
43    /// The two values are not of one type.
44    Mismatch,
45}
46
47impl fmt::Display for DataError {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.write_str(match self {
50            DataError::NotData => "the value is not data",
51            DataError::Mismatch => "the values are not of one type",
52        })
53    }
54}
55
56impl std::error::Error for DataError {}
57
58/// Why a value could not be built from the parts given for it.
59///
60/// A reader of history rebuilds values node by node and run by run
61/// ([`Node::leaf`](crate::Node::leaf), [`List::from_parts`](crate::List::from_parts),
62/// [`Text::of_block`](crate::Text::of_block)); each part it gives is checked
63/// here, so a value this crate holds keeps its invariants whatever it was
64/// read from.
65#[derive(Clone, Copy, PartialEq, Eq, Debug)]
66pub enum StructureError {
67    /// A leaf of no elements or of more than eight.
68    Leaf,
69    /// A list's front or back of more than eight elements.
70    End,
71    /// A list takes elements from beyond the end of its tree.
72    Window,
73    /// A run that does not lie within its block, or that begins or ends
74    /// inside a character.
75    Run,
76}
77
78impl fmt::Display for StructureError {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        f.write_str(match self {
81            StructureError::Leaf => "a leaf holds from one to eight elements",
82            StructureError::End => "a list's end holds at most eight elements",
83            StructureError::Window => "a list takes elements its tree does not hold",
84            StructureError::Run => "a run does not lie between characters of its block",
85        })
86    }
87}
88
89impl std::error::Error for StructureError {}