Skip to main content

scratch_test_value/
lib.rs

1#![doc = include_str!("../../../README.md")]
2//! ## scratch-test-value
3
4mod arithmetic;
5mod comparisons;
6mod constants;
7mod conversions;
8mod lists;
9mod math;
10mod quirk_sink;
11mod rand_range;
12mod texts;
13mod utils;
14
15#[cfg(test)]
16#[cfg(all(feature = "serde", feature = "serde_json"))]
17mod serde_tests;
18
19#[allow(unused)]
20pub mod quirks {
21    pub use crate::arithmetic::{IntegerAddWouldFailQ, IntegerSubWouldFailQ};
22    pub use crate::conversions::{
23        NumberTooBigForIntQ, SNumberToFloatQ, SValueToBoolQ, SValueToFloatQ, SValueToNumberQ,
24    };
25    pub use crate::quirk_sink::QuirkSink;
26    pub use crate::texts::NthLetterOfTextQ;
27}
28
29pub(crate) use quirk_sink::QuirkSink;
30
31pub use utils::ARc;
32
33pub use lists::SList;
34
35/// This models a numeric Scratch value and is therefore returned by numeric
36/// operations or where it is known a value is neither a text nor a boolean.
37#[derive(derive_more::Debug, Clone, derive_more::Display, Copy)]
38#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
39#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
40#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41#[cfg_attr(feature = "serde", serde(untagged))]
42pub enum SNumber {
43    #[debug("{_0:?}")]
44    Int(i64),
45    #[debug("{_0:?}")]
46    Float(f64),
47}
48
49/// This should model a Scratch value.
50/// Scratch treats texts that are non-numeric as the number `0` and also stores numbers
51/// inside of arithmetic expressions as texts, at least sometimes.
52///
53/// So it is useful to have a type that mimics this implicit conversion
54/// behaviour.
55#[derive(derive_more::Debug, Clone, derive_more::Display)]
56#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
57#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
58#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
59#[cfg_attr(feature = "serde", serde(untagged))]
60pub enum SValue {
61    #[debug("{_0:?}")]
62    #[cfg_attr(feature = "utoipa", schema(value_type = String))]
63    Text(ARc<str>),
64    #[debug("{_0:?}")]
65    Int(i64),
66    #[debug("{_0:?}")]
67    Float(f64),
68    #[debug("{_0:?}")]
69    Bool(bool),
70}