bast3st_eval/spec/runtime/
mod.rs1mod collections;
2mod criterion;
3mod marker;
4mod network;
5mod selector;
6mod tasks;
7
8use crate::helpers::NoError;
9pub use crate::spec::RuntimeAction;
10pub use crate::spec::runtime::criterion::{InnerRuntimeCriterion, RuntimeCriterion};
11pub use collections::{Array, MappingOrArray, RealMapping};
12use derive_more::From;
13pub(super) use marker::{CheapBorrowFromAny, ClarifiedCerrMerging, PossibleRuntimeValue};
14pub(crate) use marker::{MachineReturnVal, SpecializeFrom};
15pub use network::{InnerNetworkRequest, NetworkRequest, NetworkResponse};
16pub use selector::Selector;
17use serde::{Deserialize, Serialize};
18pub use tasks::{CompiledRegex, SpecificTaskRequest};
19
20use crate::spec::{MapKey, Numeric};
21use crate::{catchable::cerr, spec::PrimitiveValue};
22
23#[derive(Debug, PartialEq, PartialOrd, Clone, From, Serialize, Deserialize)]
24pub enum RuntimeAny<E = cerr> {
25 #[serde(rename = "crit")]
26 #[from]
27 Criterion(RuntimeCriterion),
28 #[serde(rename = "act")]
29 #[from]
30 Action(RuntimeAction),
31 #[serde(rename = "val")]
32 #[from]
33 Value(RuntimeValue),
34 #[serde(rename = "err")]
35 Catchable(E),
36}
37pub type RuntimeAnyWithoutCerr = RuntimeAny<NoError>;
38
39impl From<cerr> for RuntimeAny {
40 fn from(value: cerr) -> Self {
41 Self::Catchable(value)
42 }
43}
44
45impl From<Result<RealMapping, cerr>> for RuntimeAny {
46 fn from(value: Result<RealMapping, cerr>) -> Self {
47 match value {
48 Ok(x) => Self::from(x),
49 Err(e) => Self::Catchable(e),
50 }
51 }
52}
53
54#[derive(
55 Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, From, Default, Serialize, Deserialize,
56)]
57#[serde(tag = "t")]
58pub enum MaybeEval<T> {
59 #[serde(rename = "unev")]
60 #[default]
61 Unevaluated,
62 #[serde(rename = "eval")]
63 Evaluated(T),
64}
65impl<T> MaybeEval<T> {
66 pub fn as_eval(&self) -> Option<&T> {
67 if let Self::Evaluated(t) = self {
68 Some(t)
69 } else {
70 None
71 }
72 }
73 pub fn into_eval(self) -> Option<T> {
74 if let Self::Evaluated(t) = self {
75 Some(t)
76 } else {
77 None
78 }
79 }
80}
81
82#[derive(derive_more::Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum RuntimeValue {
85 #[debug("Prim({_0:?})")]
86 Prim(PrimitiveValue),
87 Comp(MappingOrArray),
88}
89
90impl<T> From<T> for RuntimeValue
91where
92 PrimitiveValue: From<T>,
93{
94 fn from(value: T) -> Self {
95 Self::Prim(value.into())
96 }
97}
98
99impl From<MappingOrArray> for RuntimeValue {
100 fn from(value: MappingOrArray) -> Self {
101 Self::Comp(value)
102 }
103}
104impl From<Array> for RuntimeValue {
105 fn from(value: Array) -> Self {
106 Self::Comp(value.into())
107 }
108}
109impl From<RealMapping> for RuntimeValue {
110 fn from(value: RealMapping) -> Self {
111 Self::Comp(value.into())
112 }
113}
114
115impl From<u16> for PrimitiveValue {
116 fn from(value: u16) -> Self {
117 Self::Number(Numeric::Int(value.into()))
118 }
119}
120
121impl From<RealMapping> for RuntimeAny {
122 fn from(value: RealMapping) -> Self {
123 Self::Value(value.into())
124 }
125}
126impl From<Array> for RuntimeAny {
127 fn from(value: Array) -> Self {
128 Self::Value(value.into())
129 }
130}
131
132pub trait IntoRuntimeAny {
133 fn into_runtimeany(self) -> RuntimeAny;
134}
135impl<T: Into<RuntimeAny>> IntoRuntimeAny for Result<T, cerr> {
136 fn into_runtimeany(self) -> RuntimeAny {
137 self.map_or_else(RuntimeAny::Catchable, |x| x.into())
138 }
139}
140
141impl From<serde_json::Value> for RuntimeValue {
142 fn from(value: serde_json::Value) -> Self {
143 match value {
144 serde_json::Value::Null | serde_json::Value::Bool(false) => {
145 RuntimeValue::Prim(0.into())
146 }
147 serde_json::Value::Bool(true) => RuntimeValue::Prim(1.into()),
148 serde_json::Value::Array(array) => RuntimeValue::Comp(MappingOrArray::Array(
149 array.into_iter().map(RuntimeValue::from).collect(),
150 )),
151 serde_json::Value::Number(number) => {
152 RuntimeValue::Prim(PrimitiveValue::Number(number.into()))
153 }
154 serde_json::Value::String(txt) => RuntimeValue::Prim(PrimitiveValue::Str(txt.into())),
155 serde_json::Value::Object(mapping) => {
156 RuntimeValue::Comp(MappingOrArray::Mapping(RealMapping::from_iter(
157 mapping
158 .into_iter()
159 .map(|(key, value)| (MapKey::Str(key.into()), RuntimeValue::from(value))),
160 )))
161 }
162 }
163 }
164}