next_rust_router/
params.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5#[serde(untagged)]
6pub enum ParamValue {
7 One(String),
9 Many(Vec<String>),
11}
12
13#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
15#[serde(
16 from = "std::collections::BTreeMap<String, ParamValue>",
17 into = "std::collections::BTreeMap<String, ParamValue>"
18)]
19pub struct Params(Vec<(String, ParamValue)>);
20
21impl Params {
22 pub fn new() -> Self {
23 Self(Vec::new())
24 }
25
26 pub fn with(mut self, name: impl Into<String>, value: impl Into<ParamValue>) -> Self {
28 self.insert(name, value);
29 self
30 }
31
32 pub fn insert(&mut self, name: impl Into<String>, value: impl Into<ParamValue>) {
33 let name = name.into();
34 let value = value.into();
35 if let Some(slot) = self.0.iter_mut().find(|(n, _)| *n == name) {
36 slot.1 = value;
37 } else {
38 self.0.push((name, value));
39 }
40 }
41
42 pub(crate) fn pop(&mut self) {
43 self.0.pop();
44 }
45
46 pub fn get(&self, name: &str) -> Option<&str> {
49 self.0.iter().find(|(n, _)| n == name).and_then(|(_, v)| match v {
50 ParamValue::One(s) => Some(s.as_str()),
51 ParamValue::Many(_) => None,
52 })
53 }
54
55 pub fn get_all(&self, name: &str) -> Option<&[String]> {
58 self.0.iter().find(|(n, _)| n == name).map(|(_, v)| match v {
59 ParamValue::One(s) => std::slice::from_ref(s),
60 ParamValue::Many(v) => v.as_slice(),
61 })
62 }
63
64 pub fn value(&self, name: &str) -> Option<&ParamValue> {
65 self.0.iter().find(|(n, _)| n == name).map(|(_, v)| v)
66 }
67
68 pub fn iter(&self) -> impl Iterator<Item = (&str, &ParamValue)> {
69 self.0.iter().map(|(n, v)| (n.as_str(), v))
70 }
71
72 pub fn len(&self) -> usize {
73 self.0.len()
74 }
75
76 pub fn is_empty(&self) -> bool {
77 self.0.is_empty()
78 }
79}
80
81impl From<&str> for ParamValue {
82 fn from(s: &str) -> Self {
83 ParamValue::One(s.to_owned())
84 }
85}
86
87impl From<String> for ParamValue {
88 fn from(s: String) -> Self {
89 ParamValue::One(s)
90 }
91}
92
93impl From<Vec<String>> for ParamValue {
94 fn from(v: Vec<String>) -> Self {
95 ParamValue::Many(v)
96 }
97}
98
99impl From<Vec<&str>> for ParamValue {
100 fn from(v: Vec<&str>) -> Self {
101 ParamValue::Many(v.into_iter().map(str::to_owned).collect())
102 }
103}
104
105impl<const N: usize> From<[&str; N]> for ParamValue {
106 fn from(v: [&str; N]) -> Self {
107 ParamValue::Many(v.iter().map(|s| (*s).to_owned()).collect())
108 }
109}
110
111impl From<std::collections::BTreeMap<String, ParamValue>> for Params {
112 fn from(map: std::collections::BTreeMap<String, ParamValue>) -> Self {
113 Params(map.into_iter().collect())
114 }
115}
116
117impl From<Params> for std::collections::BTreeMap<String, ParamValue> {
118 fn from(p: Params) -> Self {
119 p.0.into_iter().collect()
120 }
121}
122
123impl<K: Into<String>, V: Into<ParamValue>> FromIterator<(K, V)> for Params {
124 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
125 let mut p = Params::new();
126 for (k, v) in iter {
127 p.insert(k, v);
128 }
129 p
130 }
131}