mln_runtime/
feature_state.rs1use feature::{Identifier, ValueObject};
4use mln_util::error::Error;
5
6pub type FeatureState = ValueObject;
8
9#[derive(Clone, Debug, PartialEq)]
11pub struct FeatureStateSelector {
12 pub source_id: String,
13 pub source_layer_id: Option<String>,
16 pub feature_id: Option<Identifier>,
23 pub state_key: Option<String>,
26}
27
28impl FeatureStateSelector {
29 pub fn feature(source_id: impl Into<String>, feature_id: Identifier) -> Self {
31 FeatureStateSelector {
32 source_id: source_id.into(),
33 source_layer_id: None,
34 feature_id: Some(feature_id),
35 state_key: None,
36 }
37 }
38
39 #[must_use]
40 pub fn with_source_layer(mut self, source_layer_id: impl Into<String>) -> Self {
41 self.source_layer_id = Some(source_layer_id.into());
42 self
43 }
44
45 #[must_use]
46 pub fn with_state_key(mut self, state_key: impl Into<String>) -> Self {
47 self.state_key = Some(state_key.into());
48 self
49 }
50
51 pub(crate) fn key(&self, needs_feature: bool) -> Result<SelectorKey, Error> {
54 if self.source_id.is_empty() {
55 return Err(Error::invalid_argument("feature state source_id must not be empty"));
56 }
57 let feature_id = match &self.feature_id {
58 None => None,
59 Some(Identifier::Null) => {
60 return Err(Error::invalid_argument("feature state feature_id must not be null"));
61 }
62 Some(Identifier::Double(value)) if !value.is_finite() => {
63 return Err(Error::invalid_argument("feature_id double_value must be finite"));
64 }
65 Some(id) => mln_util::feature::feature_id_to_string(id),
66 };
67 let state_key = self.state_key.clone().filter(|key| !key.is_empty());
68 if feature_id.is_none() {
69 if needs_feature {
70 return Err(Error::invalid_argument("feature state selector requires feature_id"));
71 }
72 if state_key.is_some() {
73 return Err(Error::invalid_argument("feature state selector state_key requires feature_id"));
74 }
75 }
76 Ok(SelectorKey {
77 source_id: self.source_id.clone(),
78 source_layer_id: self.source_layer_id.clone().unwrap_or_default(),
79 feature_id,
80 state_key,
81 })
82 }
83}
84
85#[derive(Clone, Debug)]
87pub(crate) struct SelectorKey {
88 pub(crate) source_id: String,
89 pub(crate) source_layer_id: String,
90 pub(crate) feature_id: Option<String>,
91 pub(crate) state_key: Option<String>,
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 fn text(id: &str) -> Identifier {
99 Identifier::String(id.to_owned())
100 }
101
102 fn feature_text(id: Identifier) -> String {
103 FeatureStateSelector::feature("points", id).key(true).unwrap().feature_id.unwrap()
104 }
105
106 #[test]
107 fn numbers_name_the_feature_of_their_text() {
108 for id in [Identifier::Uint(7), Identifier::Int(7), Identifier::Double(7.0), text("7")] {
109 assert_eq!("7", feature_text(id));
110 }
111 assert_eq!("-3", feature_text(Identifier::Int(-3)));
112 assert_eq!("0.1", feature_text(Identifier::Double(0.1)));
113 assert_eq!("1e21", feature_text(Identifier::Double(1e21)));
114 let key =
116 FeatureStateSelector::feature("points", text("a")).with_source_layer("").with_state_key("").key(false);
117 let key = key.unwrap();
118 assert_eq!(("", None), (key.source_layer_id.as_str(), key.state_key));
119 }
120
121 #[test]
122 fn selectors_check_what_each_use_needs() {
123 let message = |selector: FeatureStateSelector, needs_feature| selector.key(needs_feature).unwrap_err().message;
124 let feature = FeatureStateSelector::feature("points", text("a"));
125 assert_eq!(
126 "feature state source_id must not be empty",
127 message(FeatureStateSelector { source_id: String::new(), ..feature.clone() }, false)
128 );
129 let source = FeatureStateSelector { feature_id: None, ..feature.clone() };
130 assert_eq!("feature state selector requires feature_id", message(source.clone(), true));
131 assert_eq!("feature state selector state_key requires feature_id", message(source.with_state_key("k"), false));
132 assert!(FeatureStateSelector { feature_id: None, ..feature }.with_state_key("").key(false).is_ok());
133 assert_eq!(
134 "feature_id double_value must be finite",
135 message(FeatureStateSelector::feature("points", Identifier::Double(f64::NAN)), true)
136 );
137 }
138}