Skip to main content

bast3st_eval/spec/entities/
entity_action.rs

1use derive_getters::Getters;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    Features,
6    catchable::cerr,
7    evaluation::RequiredFeatures,
8    spec::{
9        CriterionEntity, EndThisTestMode, EntityId, InnerNetworkRequest, MachineConstruction,
10        MapKey, MessageSendingLevel, MessageSeverity, NetworkResponse, PrimitiveValue, SetFlagMode,
11        Text, ValueReference, machine::Machine, runtime::RuntimeCriterion,
12    },
13};
14
15#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
16#[serde(tag = "op", rename_all = "kebab-case")]
17pub enum ActionEntity {
18    SendMsg {
19        #[serde(rename = "t")]
20        text: ValueReference,
21        #[serde(rename = "s")]
22        severity: MessageSeverity,
23        #[serde(rename = "l", default)]
24        level: MessageSendingLevel,
25    },
26    EndThisTest {
27        #[serde(rename = "m")]
28        mode: EndThisTestMode,
29        #[serde(rename = "e")]
30        explaination: ValueReference,
31    },
32
33    SetFlag {
34        #[serde(rename = "m", default)]
35        mode: SetFlagMode,
36        #[serde(rename = "k")]
37        key: Vec<ValueReference>,
38        #[serde(rename = "v")]
39        value: ValueReference,
40    },
41    #[serde(rename = "aifte")]
42    IfThenElse {
43        #[serde(rename = "i")]
44        if_: EntityId<CriterionEntity>,
45        #[serde(rename = "t")]
46        then_: EntityId<ActionEntity>,
47        #[serde(rename = "e")]
48        else_: EntityId<ActionEntity>,
49    },
50}
51impl ActionEntity {
52    pub fn machine(&self) -> Machine<RuntimeAction> {
53        match self {
54            Self::SendMsg {
55                text,
56                severity,
57                level,
58            } => {
59                let severity = *severity;
60                let level = *level;
61                text.query(move |text: PrimitiveValue| {
62                    RuntimeAction::SendMsg(SendMsgAction {
63                        text: text.into_text(),
64                        severity,
65                        level,
66                    })
67                    .into()
68                })
69            }
70            Self::EndThisTest { mode, explaination } => {
71                let mode = *mode;
72                explaination.query(move |explaination: PrimitiveValue| {
73                    RuntimeAction::EndThisTest(EndThisTestAction {
74                        mode,
75                        explaination: explaination.into_text(),
76                    })
77                    .into()
78                })
79            }
80            Self::IfThenElse { if_, then_, else_ } => {
81                let (then_, else_) = (*then_, *else_);
82                if_.query(move |if_: RuntimeCriterion| {
83                    if if_.is_fulfilled() { then_ } else { else_ }
84                        .query(|branch: RuntimeAction| branch.into())
85                })
86            }
87            Self::SetFlag { mode, key, value } => set_flag_act(
88                mode.clone(),
89                key.iter().rev().cloned().collect(),
90                vec![],
91                value.clone(),
92            ),
93        }
94        .require_features(self.required_features())
95    }
96}
97
98fn set_flag_act(
99    mode: SetFlagMode,
100    mut reversed_keys: Vec<ValueReference>,
101    mut eval_keys: Vec<MapKey>,
102    value: ValueReference,
103) -> Machine<RuntimeAction> {
104    if let Some(key) = reversed_keys.pop() {
105        key.query(move |key: MapKey| {
106            eval_keys.push(key);
107            set_flag_act(mode, reversed_keys, eval_keys, value)
108        })
109    } else {
110        value.query(move |value: PrimitiveValue| {
111            RuntimeAction::SetFlag(SetFlagAction {
112                mode,
113                key: eval_keys,
114                value,
115            })
116            .into()
117        })
118    }
119}
120
121impl RequiredFeatures for ActionEntity {
122    fn required_features(&self) -> Features {
123        match self {
124            Self::SendMsg { level, .. } => level.required_features(),
125            Self::EndThisTest { .. } => Features::END_THIS_TEST,
126            Self::SetFlag { .. } | Self::IfThenElse { .. } => Features::empty(),
127        }
128    }
129}
130
131#[derive(Debug, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
132#[serde(tag = "t", rename_all = "kebab-case")]
133pub enum RuntimeAction {
134    SendMsg(SendMsgAction),
135    EndThisTest(EndThisTestAction),
136    SetFlag(SetFlagAction),
137    Notice(NoticeAction),
138}
139
140#[derive(Debug, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
141#[serde(tag = "t", rename_all = "kebab-case")]
142pub enum NoticeAction {
143    #[serde(rename_all = "kebab-case")]
144    Network {
145        url: Result<Text, (Text, Text)>,
146        request: InnerNetworkRequest,
147        // NetworkResponse or Debug of reqwest::Error
148        response: Result<Result<NetworkResponse, Text>, cerr>,
149    },
150}
151
152#[derive(Debug, PartialEq, PartialOrd, Clone, Getters, Serialize, Deserialize)]
153pub struct SetFlagAction {
154    mode: SetFlagMode,
155    key: Vec<MapKey>,
156    value: PrimitiveValue,
157}
158impl SetFlagAction {
159    pub fn into_parts(self) -> (SetFlagMode, Vec<MapKey>, PrimitiveValue) {
160        (self.mode, self.key, self.value)
161    }
162}
163
164#[derive(Debug, PartialEq, PartialOrd, Clone, Getters, Serialize, Deserialize)]
165pub struct EndThisTestAction {
166    mode: EndThisTestMode,
167    explaination: Text,
168}
169
170impl EndThisTestMode {
171    pub fn action_with(self, explaination: Text) -> EndThisTestAction {
172        EndThisTestAction {
173            mode: self,
174            explaination,
175        }
176    }
177}
178
179#[derive(Debug, PartialEq, PartialOrd, Clone, Getters, Serialize, Deserialize)]
180pub struct SendMsgAction {
181    level: MessageSendingLevel,
182    severity: MessageSeverity,
183    text: Text,
184}
185impl MessageSeverity {
186    pub fn action_with(self, level: MessageSendingLevel, text: Text) -> SendMsgAction {
187        SendMsgAction {
188            severity: self,
189            level,
190            text,
191        }
192    }
193}
194
195#[derive(Debug, PartialEq, PartialOrd, Clone)]
196pub enum ProcessedAction {}