flow-lang-value 0.1.0-rc.5

The Flow runtime value model: data values, their equality, order and text, and the operations std's built-in types declare.
Documentation
//! The ways an operation in this crate can decline.

use std::fmt;

/// Why an arithmetic operation produced no value.
///
/// `data.md` makes a zero divisor the only operation on its types that faults.
/// The evaluator turns either of these into a fault; neither is a value a
/// program can observe.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Fault {
    /// `int.div`, `%` or `/` was given a zero divisor.
    DivisionByZero,
    /// The exact result exists but its scale cannot be represented: a
    /// `Decimal` whose power of ten falls outside a signed 64-bit exponent.
    ///
    /// This is the exponent limit `execution.md` sets, not a rule about the
    /// number.
    Limit,
}

impl fmt::Display for Fault {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Fault::DivisionByZero => "division by zero",
            Fault::Limit => "the result's exponent does not fit in 64 bits",
        })
    }
}

impl std::error::Error for Fault {}

/// Why equality, ordering or text was refused.
///
/// `types.md` makes each of these a compile error on a type that is not data,
/// so a checked program never reaches either case. They exist so that a defect
/// upstream surfaces as a typed refusal rather than as an answer invented here.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DataError {
    /// A function, a task, a host token or another evaluator-owned value was
    /// reached: none of them has equality, an order or a text form.
    NotData,
    /// The two values are not of one type.
    Mismatch,
}

impl fmt::Display for DataError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            DataError::NotData => "the value is not data",
            DataError::Mismatch => "the values are not of one type",
        })
    }
}

impl std::error::Error for DataError {}

/// Why a value could not be built from the parts given for it.
///
/// A reader of history rebuilds values node by node and run by run
/// ([`Node::leaf`](crate::Node::leaf), [`List::from_parts`](crate::List::from_parts),
/// [`Text::of_block`](crate::Text::of_block)); each part it gives is checked
/// here, so a value this crate holds keeps its invariants whatever it was
/// read from.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum StructureError {
    /// A leaf of no elements or of more than eight.
    Leaf,
    /// A list's front or back of more than eight elements.
    End,
    /// A list takes elements from beyond the end of its tree.
    Window,
    /// A run that does not lie within its block, or that begins or ends
    /// inside a character.
    Run,
}

impl fmt::Display for StructureError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            StructureError::Leaf => "a leaf holds from one to eight elements",
            StructureError::End => "a list's end holds at most eight elements",
            StructureError::Window => "a list takes elements its tree does not hold",
            StructureError::Run => "a run does not lie between characters of its block",
        })
    }
}

impl std::error::Error for StructureError {}