#![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};
#[derive(Debug, PartialEq)]
pub struct Made(pub i64);
impl Carry for Made {
fn held(&self) -> &[Value] {
&[]
}
fn held_mut(&mut self) -> &mut [Value] {
&mut []
}
}
pub fn native(n: i64) -> Value {
Value::Native(Native::new(Made(n)))
}
pub fn int(digits: &str) -> Int {
Int::parse(digits).expect("an integer spelling")
}
pub fn dec(spelling: &str) -> Decimal {
Decimal::parse(spelling).expect("a decimal spelling")
}
pub fn decimal_value(spelling: &str) -> Value {
Value::Decimal(dec(spelling))
}
pub fn option_shape() -> Arc<EnumShape> {
EnumShape::new(
"Option",
[
Constructor::new("None", Payload::None),
Constructor::new("Some", Payload::Positional(1)),
],
)
}
pub fn none() -> Value {
Value::Variant(Variant::new(option_shape(), 0, vec![]).expect("None"))
}
pub fn some(value: Value) -> Value {
Value::Variant(Variant::new(option_shape(), 1, vec![value]).expect("Some"))
}
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"),
)
}
pub fn order(left: &Value, right: &Value) -> Ordering {
compare(left, right).expect("comparable values")
}
pub fn same(left: &Value, right: &Value) -> bool {
equal(left, right).expect("comparable values")
}
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)
}
pub fn below(&mut self, bound: u64) -> u64 {
self.next() % bound
}
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)
}
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")
}
}