Skip to main content

bast3st_eval/spec/runtime/
selector.rs

1use derive_more::Display;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    evaluation::RequiredFeatures,
6    spec::{
7        Coremapping, MachineConstruction, MapKey,
8        machine::Machine,
9        runtime::{PossibleRuntimeValue, RuntimeValue, SpecializeFrom},
10    },
11};
12
13#[derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
14#[serde(untagged)]
15pub enum Selector {
16    #[display("{_0}")]
17    #[serde(rename = "coremap")]
18    Coremap(Coremapping),
19    #[display("{mapping}{key:?}")]
20    #[serde(rename = "coreitm")]
21    CoremapItem {
22        mapping: Coremapping,
23        key: Vec<MapKey>,
24    },
25}
26
27impl Selector {
28    pub const INPUT: Selector = Selector::Coremap(Coremapping::Input);
29    pub const OUTPUT: Selector = Selector::Coremap(Coremapping::Output);
30    pub const RANDOMS: Selector = Selector::Coremap(Coremapping::Randoms);
31    pub const VARIABLES: Selector = Selector::Coremap(Coremapping::Variables);
32    pub const LISTS: Selector = Selector::Coremap(Coremapping::Lists);
33    pub const PARAM: Selector = Selector::Coremap(Coremapping::Param);
34    pub const FLAGS: Selector = Selector::Coremap(Coremapping::Flags);
35
36    pub fn std_machine<Out>(self) -> Machine<RuntimeValue>
37    where
38        Self: PossibleRuntimeValue<Out>,
39        Out: SpecializeFrom + Send,
40        RuntimeValue: From<Out>,
41    {
42        let required = self.required_features();
43        self.query(|x: Out| Machine::from_final(RuntimeValue::from(x)))
44            .require_features(required)
45    }
46}
47
48impl RequiredFeatures for Selector {
49    fn required_features(&self) -> crate::Features {
50        match self {
51            Self::Coremap(mapping) | Self::CoremapItem { mapping, key: _ } => {
52                mapping.required_features()
53            }
54        }
55    }
56}
57
58impl From<Coremapping> for Selector {
59    fn from(value: Coremapping) -> Self {
60        Self::Coremap(value)
61    }
62}
63
64impl RequiredFeatures for Coremapping {
65    fn required_features(&self) -> crate::Features {
66        match self {
67            Self::Input | Self::Output | Self::Randoms | Self::Lists | Self::Variables => {
68                crate::Features::READ_RUNDATA
69            }
70            Self::Param | Self::Flags => crate::Features::empty(),
71        }
72    }
73}