bast3st_eval/evaluation/selectable/
source_flagdata.rs1use std::{
2 borrow::Cow,
3 collections::{BTreeMap, VecDeque},
4};
5
6use serde::{Deserialize, Serialize};
7
8use crate::{
9 catchable::cerr,
10 spec::{
11 Coremapping, FatalError, IntoRuntimeAny, MapKey, RealMapping, RuntimeAction, RuntimeAny,
12 RuntimeValue, Selector, SetFlagMode, Text,
13 },
14};
15
16use super::{Features, SelectableSource};
17
18#[derive(derive_more::Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
19#[serde(into = "RealMapping", from = "RealMapping")]
20pub struct MappingData<T> {
21 data: RuntimeAny, #[debug(skip)]
23 _phantom: std::marker::PhantomData<T>,
24}
25
26impl<T> Clone for MappingData<T> {
27 fn clone(&self) -> Self {
28 Self {
29 data: self.data.clone(),
30 _phantom: Default::default(),
31 }
32 }
33}
34
35#[derive(derive_more::Debug, PartialEq, PartialOrd, Clone)]
36pub struct SourceFlag(());
37#[derive(derive_more::Debug, PartialEq, PartialOrd, Clone)]
38pub struct SourceParam(());
39
40pub type FlagData = MappingData<SourceFlag>;
41pub type ParamData = MappingData<SourceParam>;
42
43impl<X> Default for MappingData<X> {
44 fn default() -> Self {
45 Self {
46 data: RuntimeAny::Value(RealMapping::default().into()),
47 _phantom: Default::default(),
48 }
49 }
50}
51
52impl<X> From<BTreeMap<MapKey, Result<RuntimeValue, cerr>>> for MappingData<X> {
53 fn from(value: BTreeMap<MapKey, Result<RuntimeValue, cerr>>) -> Self {
54 Self {
55 data: RuntimeAny::Value(RealMapping::from(value).into()),
56 _phantom: Default::default(),
57 }
58 }
59}
60
61impl<X> From<RealMapping> for MappingData<X> {
62 fn from(value: RealMapping) -> Self {
63 Self {
64 data: RuntimeAny::Value(value.into()),
65 _phantom: Default::default(),
66 }
67 }
68}
69
70impl<X> From<BTreeMap<Text, RuntimeValue>> for MappingData<X> {
71 fn from(value: BTreeMap<Text, RuntimeValue>) -> Self {
72 Self::default().extended(value)
73 }
74}
75
76impl<X, K: Into<MapKey>, V: Into<RuntimeValue>> Extend<(K, V)> for MappingData<X> {
77 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
78 let flags: RealMapping = self.as_ref().clone();
79 self.data = flags
80 .with_new_keys(
81 iter.into_iter()
82 .map(|(text, val)| (vec![text.into()].into_iter().collect(), Ok(val.into()))),
83 )
84 .into();
85 }
86}
87
88impl<X, K: Into<MapKey>, V: Into<RuntimeValue>> Extend<(VecDeque<K>, V)> for MappingData<X> {
89 fn extend<T: IntoIterator<Item = (VecDeque<K>, V)>>(&mut self, iter: T) {
90 let flags: RealMapping = self.as_ref().clone();
91 self.data =
92 flags
93 .with_new_keys(iter.into_iter().map(|(text, val)| {
94 (text.into_iter().map(|x| x.into()).collect(), Ok(val.into()))
95 }))
96 .into();
97 }
98}
99
100impl<X, K: Into<MapKey>, V: Into<RuntimeValue>> Extend<(Vec<K>, V)> for MappingData<X> {
101 fn extend<T: IntoIterator<Item = (Vec<K>, V)>>(&mut self, iter: T) {
102 let flags: RealMapping = self.as_ref().clone();
103 self.data = flags
104 .with_new_keys(iter.into_iter().map(|(text, val)| {
105 let text = text.into_iter().map(|x| x.into()).collect();
106 let val = val.into();
107 (text, Ok(val))
108 }))
109 .into();
110 }
111}
112
113impl<X> MappingData<X> {
114 pub fn extended<E>(mut self, other: impl IntoIterator<Item = E>) -> Self
115 where
116 Self: Extend<E>,
117 {
118 self.extend(other);
119 self
120 }
121 pub fn with_append(self, other: FlagData) -> Self {
122 let m: RealMapping = self.as_ref().clone();
123 Self {
124 data: m.with_merged(other.as_ref().as_ref()).into(),
125 _phantom: Default::default(),
126 }
127 }
128
129 pub fn iter(&self) -> impl Iterator<Item = (&MapKey, &Result<RuntimeValue, cerr>)> {
130 self.as_ref().iter()
131 }
132
133 pub fn inefficient_insert(&mut self, key: impl Into<MapKey>, value: RuntimeValue) {
134 self.extend(std::iter::once((key.into(), value)));
135 }
136}
137impl<X> AsRef<RealMapping> for MappingData<X> {
138 fn as_ref(&self) -> &RealMapping {
139 if let RuntimeAny::Value(RuntimeValue::Comp(crate::spec::MappingOrArray::Mapping(
140 mapping,
141 ))) = &self.data
142 {
143 mapping
144 } else {
145 unreachable!("the type guarantees that the kept value is a real mapping")
146 }
147 }
148}
149impl<X> From<MappingData<X>> for RealMapping {
150 fn from(value: MappingData<X>) -> Self {
151 value.as_ref().clone()
152 }
153}
154
155impl SelectableSource for ParamData {
156 async fn request<'a>(
157 &'a self,
158 selector: &crate::spec::Selector,
159 _allowed_features: Features,
160 ) -> Result<Cow<'a, RuntimeAny>, FatalError> {
161 match selector {
162 Selector::Coremap(Coremapping::Param) => Ok(Cow::Borrowed(&self.data)),
163 Selector::CoremapItem {
164 mapping: Coremapping::Param,
165 key,
166 } => Ok(Cow::Owned(
167 self.as_ref()
168 .get_by_seq(key)
169 .transpose()
170 .map(|x| x.cloned().into_runtimeany())
171 .unwrap_or(self.data.clone()),
172 )),
173
174 Selector::Coremap(_) | Selector::CoremapItem { .. } => {
175 Self::mark_fallback_need(selector.clone())
176 }
177 }
178 }
179}
180
181impl SelectableSource for FlagData {
182 async fn request<'a>(
183 &'a self,
184 selector: &crate::spec::Selector,
185 _allowed_features: Features,
186 ) -> Result<Cow<'a, RuntimeAny>, FatalError> {
187 match selector {
188 Selector::Coremap(Coremapping::Flags) => Ok(Cow::Borrowed(&self.data)),
189 Selector::CoremapItem {
190 mapping: Coremapping::Flags,
191 key,
192 } => Ok(Cow::Owned(
193 self.as_ref()
194 .get_by_seq(key)
195 .transpose()
196 .map(|x| x.cloned().into_runtimeany())
197 .unwrap_or(self.data.clone()),
198 )),
199
200 Selector::Coremap(_) | Selector::CoremapItem { .. } => {
201 Self::mark_fallback_need(selector.clone())
202 }
203 }
204 }
205}
206
207impl<'a> Extend<&'a RuntimeAction> for MappingData<SourceFlag> {
208 fn extend<T: IntoIterator<Item = &'a RuntimeAction>>(&mut self, actions: T) {
209 let actions = actions.into_iter().flat_map(|action: &'a RuntimeAction| {
210 if let RuntimeAction::SetFlag(action) = action {
211 let (mode, key, value) = action.clone().into_parts();
212 match mode {
213 SetFlagMode::Keep => Some((key, value)),
214 }
215 } else {
216 None
217 }
218 });
219 self.extend(actions);
220 }
221}