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
//! Builders shared by the concern files.

#![allow(
    dead_code,
    unreachable_pub,
    missing_docs,
    reason = "each concern file uses only some of the shared builders"
)]

use std::cmp::Ordering;
use std::sync::Arc;

use flow_types::{Constructor, EnumShape, Payload, RecordShape};
use flow_value::{Carry, Decimal, Int, Native, Record, Value, Variant, compare, equal};

/// An evaluator-made value, as this crate sees one: carried, holding
/// nothing, the same as nothing else.
#[derive(Debug, PartialEq)]
pub struct Made(pub i64);

impl Carry for Made {
    fn held(&self) -> &[Value] {
        &[]
    }

    fn held_mut(&mut self) -> &mut [Value] {
        &mut []
    }
}

/// A native value carrying `Made(n)`.
pub fn native(n: i64) -> Value {
    Value::Native(Native::new(Made(n)))
}

/// The integer `digits` spells.
pub fn int(digits: &str) -> Int {
    Int::parse(digits).expect("an integer spelling")
}

/// The decimal `spelling` spells.
pub fn dec(spelling: &str) -> Decimal {
    Decimal::parse(spelling).expect("a decimal spelling")
}

/// `Value::Decimal` of `spelling`.
pub fn decimal_value(spelling: &str) -> Value {
    Value::Decimal(dec(spelling))
}

/// `pub type Option<T> { None, Some(T) }`.
pub fn option_shape() -> Arc<EnumShape> {
    EnumShape::new(
        "Option",
        [
            Constructor::new("None", Payload::None),
            Constructor::new("Some", Payload::Positional(1)),
        ],
    )
}

/// `None`.
pub fn none() -> Value {
    Value::Variant(Variant::new(option_shape(), 0, vec![]).expect("None"))
}

/// `Some(value)`.
pub fn some(value: Value) -> Value {
    Value::Variant(Variant::new(option_shape(), 1, vec![value]).expect("Some"))
}

/// A record `name { fields... }` with the values given, in declaration order.
pub fn record(name: &str, fields: &[(&str, Value)]) -> Value {
    let shape = RecordShape::new(name, fields.iter().map(|(field, _)| *field));
    Value::Record(
        Record::new(
            shape,
            fields.iter().map(|(_, value)| value.clone()).collect(),
        )
        .expect("one value per field"),
    )
}

/// The order of two values, which must be comparable.
pub fn order(left: &Value, right: &Value) -> Ordering {
    compare(left, right).expect("comparable values")
}

/// Whether two values are equal, which must be comparable.
pub fn same(left: &Value, right: &Value) -> bool {
    equal(left, right).expect("comparable values")
}

/// A small deterministic generator (SplitMix64): property tests draw from a
/// fixed seed, so a failure reproduces exactly.
pub struct Draw(u64);

impl Draw {
    pub fn new(seed: u64) -> Self {
        Draw(seed)
    }

    pub fn next(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    /// A number in `0..bound`.
    pub fn below(&mut self, bound: u64) -> u64 {
        self.next() % bound
    }

    /// An integer of up to `digits` decimal digits, of either sign.
    pub fn int(&mut self, digits: u64) -> Int {
        let length = 1 + self.below(digits);
        let mut spelling = String::new();
        if self.below(2) == 0 {
            spelling.push('-');
        }
        for _ in 0..length {
            spelling.push(char::from(
                b'0' + u8::try_from(self.below(10)).expect("a digit"),
            ));
        }
        int(&spelling)
    }

    /// A decimal with up to `digits` digits, of either sign, with up to
    /// `places` of them after the point.
    pub fn decimal(&mut self, digits: u64, places: u64) -> Decimal {
        let coefficient = self.int(digits);
        let exponent = -i64::try_from(self.below(places + 1)).expect("small");
        Decimal::new(coefficient.as_bigint().clone(), exponent).expect("a small exponent")
    }
}