Skip to main content

bast3st_eval/evaluation/selectable/
mod.rs

1mod sel_fal;
2mod sel_merge;
3mod source_effects;
4mod source_flagdata;
5mod source_rundata;
6
7use std::borrow::Cow;
8
9#[allow(unused)]
10pub use crate::evaluation::features::RequiredFeatures;
11
12pub use sel_fal::*;
13pub use source_effects::*;
14pub use source_flagdata::*;
15pub use source_rundata::*;
16
17use crate::{
18    evaluation::features::Features,
19    spec::{FatalError, RuntimeAny, Selector},
20};
21
22pub trait SelectableSource: Send + Sync {
23    fn enforce_required(required: Features, provided: Features) -> Result<(), FatalError> {
24        if provided.contains(required) {
25            Ok(())
26        } else {
27            Err(FatalError::FeatureMissmatch { required, provided })
28        }
29    }
30    fn mark_fallback_need<O>(selector: Selector) -> Result<O, FatalError> {
31        Err(FatalError::RequestedSelectorValueNotAvailable(selector))
32    }
33
34    /// This is a way to request the value for a selector
35    /// and explicitly state which [`Features`] are allowed for the
36    /// specific evaluation at hand
37    fn request<'a>(
38        &'a self,
39        selector: &Selector,
40        allowed_features: Features,
41    ) -> impl Future<Output = Result<Cow<'a, RuntimeAny>, FatalError>> + Send;
42}
43
44/// `()` can be used as a fallback [`SelectableSource`] that always returns
45/// `Err(`[`FatalError::RequestedSelectorValueNotAvailable`]`(selector.clone()))`
46impl SelectableSource for () {
47    async fn request<'a>(
48        &'a self,
49        selector: &Selector,
50        _allowed_features: Features,
51    ) -> Result<Cow<'a, RuntimeAny>, FatalError> {
52        Err(FatalError::RequestedSelectorValueNotAvailable(
53            selector.clone(),
54        ))
55    }
56}
57
58impl<A: SelectableSource> SelectableSource for &mut A {
59    async fn request<'z>(
60        &'z self,
61        selector: &crate::spec::Selector,
62        allowed_features: crate::Features,
63    ) -> Result<Cow<'z, crate::spec::RuntimeAny>, FatalError> {
64        <A as SelectableSource>::request(self, selector, allowed_features).await
65    }
66}
67
68impl<A: SelectableSource> SelectableSource for &A {
69    async fn request<'z>(
70        &'z self,
71        selector: &crate::spec::Selector,
72        allowed_features: crate::Features,
73    ) -> Result<Cow<'z, crate::spec::RuntimeAny>, FatalError> {
74        <A as SelectableSource>::request(self, selector, allowed_features).await
75    }
76}
77
78impl<A: SelectableSource> SelectableSource for Option<A> {
79    async fn request<'a>(
80        &'a self,
81        selector: &Selector,
82        allowed_features: Features,
83    ) -> Result<Cow<'a, RuntimeAny>, FatalError> {
84        if let Some(x) = self {
85            x.request(selector, allowed_features).await
86        } else {
87            ().request(selector, allowed_features).await
88        }
89    }
90}
91
92impl<A: SelectableSource> SelectableSource for std::sync::Arc<A> {
93    async fn request<'a>(
94        &'a self,
95        selector: &Selector,
96        allowed_features: Features,
97    ) -> Result<Cow<'a, RuntimeAny>, FatalError> {
98        use std::ops::Deref;
99        self.deref().request(selector, allowed_features).await
100    }
101}