Skip to main content

bast3st_eval/spec/
mod.rs

1mod entities;
2mod hooks;
3mod machine;
4mod random_generation;
5mod runtime;
6mod structure;
7
8use std::{cmp::Ordering, fmt::Debug, sync::Arc};
9
10use derive_more::{Deref, From, Into};
11use scratch_test_value::{SNumber, SValue};
12use serde::{Deserialize, Serialize};
13
14pub use entities::*;
15pub use hooks::*;
16pub use machine::{
17    FatalError, Machine, MachineConstruction, MachineConstructionN, MissingValue, UnfinishedMachine,
18};
19pub use random_generation::RandomGeneration;
20pub use runtime::*;
21pub use structure::*;
22
23#[derive(derive_more::Debug, Serialize, Deserialize, Clone, From)]
24#[serde(untagged)]
25pub enum PrimitiveValue {
26    // Bool(bool),
27    #[debug("{_0:?}")]
28    Str(Text),
29    #[debug("{_0}")]
30    Number(Numeric),
31}
32
33impl PrimitiveValue {
34    pub fn into_svalue(self) -> scratch_test_value::SValue {
35        match self {
36            Self::Str(t) => scratch_test_value::SValue::Text(t.into()),
37            Self::Number(x) => scratch_test_value::SValue::from(x),
38        }
39    }
40}
41
42impl From<SValue> for PrimitiveValue {
43    fn from(value: SValue) -> Self {
44        match value {
45            SValue::Int(x) => Self::Number(SNumber::Int(x)),
46            SValue::Float(x) => Self::Number(SNumber::Float(x)),
47            SValue::Text(x) => Self::Str(x.into()),
48            SValue::Bool(true) => Self::Number(SNumber::Int(1)),
49            SValue::Bool(false) => Self::Number(SNumber::Int(0)),
50        }
51    }
52}
53
54impl PartialEq for PrimitiveValue {
55    fn eq(&self, other: &Self) -> bool {
56        match (self, other) {
57            (Self::Str(a), Self::Str(b)) => a == b,
58            (Self::Number(a), Self::Number(b)) => a == b,
59            (Self::Str(s), Self::Number(n)) | (Self::Number(n), Self::Str(s)) => {
60                if let Ok(i) = s.parse() {
61                    Numeric::Int(i) == *n
62                } else if let Ok(f) = s.parse() {
63                    Numeric::Float(f) == *n
64                } else {
65                    false
66                }
67            }
68        }
69    }
70}
71impl PartialOrd for PrimitiveValue {
72    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
73        match (self, other) {
74            (Self::Str(a), Self::Str(b)) => a.partial_cmp(b),
75            (Self::Number(a), Self::Number(b)) => a.partial_cmp(b),
76            (Self::Str(s), Self::Number(n)) => s.as_ref().partial_cmp(n.to_string().as_str()),
77            (Self::Number(n), Self::Str(s)) => n.to_string().as_str().partial_cmp(s.as_ref()),
78        }
79    }
80}
81
82impl PrimitiveValue {
83    pub fn into_text(self) -> Text {
84        match self {
85            Self::Str(t) => t,
86            Self::Number(n) => n.to_string().into(),
87            // Self::Bool(b) => b.to_string().into(),
88        }
89    }
90}
91
92pub type Numeric = scratch_test_value::SNumber;
93
94#[derive(
95    derive_more::Debug,
96    derive_more::Display,
97    PartialEq,
98    PartialOrd,
99    Deref,
100    Clone,
101    Serialize,
102    Deserialize,
103    Eq,
104    Ord,
105    From,
106    Into,
107    Hash,
108)]
109#[debug("{_0:?}")]
110pub struct Text(Arc<str>);
111
112#[derive(Debug, Deref, Into, Clone, PartialEq, Eq, PartialOrd, Ord, From, Hash)]
113/// Special type that informs the query system that its also ok to take another primitive and convert it to
114/// text
115pub struct PrimitiveIntoText(Text);
116
117impl Text {
118    pub fn from_debug(x: impl Debug) -> Text {
119        Text(format!("{x:?}").into())
120    }
121}
122
123impl From<String> for Text {
124    fn from(value: String) -> Self {
125        Self(value.into())
126    }
127}
128
129impl From<String> for PrimitiveIntoText {
130    fn from(value: String) -> Self {
131        Self(value.into())
132    }
133}
134
135impl<'a> From<&'a str> for Text {
136    fn from(value: &'a str) -> Self {
137        Self(value.into())
138    }
139}
140
141impl<'a> From<&'a String> for Text {
142    fn from(value: &'a String) -> Self {
143        Self(value.to_owned().into())
144    }
145}
146impl<'a> From<&'a str> for PrimitiveValue {
147    fn from(value: &'a str) -> Self {
148        Self::Str(value.into())
149    }
150}
151impl From<String> for PrimitiveValue {
152    fn from(value: String) -> Self {
153        Self::Str(value.into())
154    }
155}
156impl<'a> From<&'a str> for MapKey {
157    fn from(value: &'a str) -> Self {
158        Self::Str(value.into())
159    }
160}
161
162#[derive(
163    derive_more::Debug, PartialEq, PartialOrd, Clone, From, Eq, Ord, Hash, Serialize, Deserialize,
164)]
165#[serde(untagged)]
166pub enum MapKey {
167    #[debug("{_0:?}")]
168    Str(Text),
169    #[debug("{_0:?}")]
170    Int(i64),
171    // Bool(bool),
172}
173impl MapKey {
174    pub fn into_text(self) -> Text {
175        match self {
176            Self::Str(t) => t,
177            Self::Int(i) => i.to_string().into(),
178            // Self::Bool(b) => b.to_string().into(),
179        }
180    }
181    pub fn to_prim(&self) -> PrimitiveValue {
182        self.clone().into()
183    }
184}
185
186impl From<MapKey> for PrimitiveValue {
187    fn from(value: MapKey) -> Self {
188        match value {
189            MapKey::Str(t) => Self::Str(t),
190            MapKey::Int(i) => Self::Number(Numeric::Int(i)),
191            // MapKey::Bool(b) => Self::Bool(b),
192        }
193    }
194}