Skip to main content

bast3st_eval/evaluation/selectable/
source_effects.rs

1use std::borrow::Cow;
2
3use derive_getters::Getters;
4use either::Either;
5use itertools::Itertools;
6use serde::{Deserialize, Serialize};
7
8use crate::{
9    Features, Messages,
10    evaluation::{FlagData, SelectableSource},
11    messages::{AnyMessages, MsgType},
12    spec::{FatalError, NoticeAction, RuntimeAction, RuntimeAny, SendMsgAction},
13};
14
15#[derive(Debug, Default, Getters, PartialEq, PartialOrd, Clone)]
16pub struct Effects {
17    flags: FlagData,
18    messages: AnyMessages,
19    notice: Vec<NoticeAction>,
20}
21
22impl Effects {
23    pub fn append(&mut self, other: Effects) {
24        self.messages.extend(other.messages);
25        let flags = std::mem::take(&mut self.flags);
26        self.flags = flags.with_append(other.flags);
27        self.notice.extend(other.notice);
28    }
29    pub fn take_messages<L: MsgType>(&mut self) -> Messages<L> {
30        self.messages.drain_msg_of()
31    }
32    pub fn take_notice(self) -> Vec<NoticeAction> {
33        self.notice
34    }
35    pub fn take_rest(self) -> (FlagData, Vec<NoticeAction>) {
36        (self.flags, self.notice)
37    }
38}
39
40impl SelectableSource for Effects {
41    async fn request<'a>(
42        &'a self,
43        selector: &crate::spec::Selector,
44        allowed_features: Features,
45    ) -> Result<Cow<'a, RuntimeAny>, FatalError> {
46        self.flags.request(selector, allowed_features).await
47    }
48}
49
50impl Extend<RuntimeAction> for Effects {
51    fn extend<T: IntoIterator<Item = RuntimeAction>>(&mut self, iter: T) {
52        let (other, messages): (Vec<Either<_, _>>, Vec<SendMsgAction>) = iter
53            .into_iter()
54            .flat_map(|item| match item {
55                RuntimeAction::SendMsg(send) => Some(Either::Right(send)),
56                RuntimeAction::EndThisTest(_) => None,
57                act @ RuntimeAction::SetFlag { .. } => Some(Either::Left(Either::Left(act))),
58                RuntimeAction::Notice(notice) => Some(Either::Left(Either::Right(notice))),
59            })
60            .partition_map(|x| x);
61        let (other, notice): (Vec<RuntimeAction>, Vec<NoticeAction>) =
62            other.into_iter().partition_map(|x| x);
63        self.flags.extend(&other);
64        self.messages.extend(messages);
65        self.notice.extend(notice);
66    }
67}
68
69impl Extend<NoticeAction> for Effects {
70    fn extend<T: IntoIterator<Item = NoticeAction>>(&mut self, iter: T) {
71        self.notice.extend(iter);
72    }
73}
74
75#[derive(Debug, PartialEq, PartialOrd, Clone)]
76pub struct WithEffects<T> {
77    data: T,
78    effects: Effects,
79}
80impl<T> WithEffects<T> {
81    pub fn new(data: T, effects: Effects) -> Self {
82        Self { data, effects }
83    }
84    pub fn with_flags(self, effects: Effects) -> Self {
85        Self {
86            data: self.data,
87            effects,
88        }
89    }
90    pub fn into_parts(self) -> (T, Effects) {
91        (self.data, self.effects)
92    }
93}
94
95#[derive(Debug, PartialEq, PartialOrd, Clone, thiserror::Error, Serialize, Deserialize)]
96#[error("notice: {notice:?}, error: {data}")]
97pub struct WithNotice<T> {
98    data: T,
99    notice: Vec<NoticeAction>,
100}
101impl<T> WithNotice<T> {
102    pub fn new(data: T, notice: Vec<NoticeAction>) -> Self {
103        Self { data, notice }
104    }
105    pub fn into_parts(self) -> (T, Vec<NoticeAction>) {
106        (self.data, self.notice)
107    }
108    pub fn take_notice(&mut self) -> Vec<NoticeAction> {
109        std::mem::take(&mut self.notice)
110    }
111}