1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use linear_map::LinearMap;
use std::{rc::Rc, str::FromStr};
use thiserror::Error;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ParamsMap(pub LinearMap<String, String>);
impl ParamsMap {
pub fn new() -> Self {
Self(LinearMap::new())
}
pub fn with_capacity(capacity: usize) -> Self {
Self(LinearMap::with_capacity(capacity))
}
pub fn insert(&mut self, key: String, value: String) -> Option<String> {
self.0.insert(key, value)
}
pub fn get(&self, key: &str) -> Option<&String> {
self.0.get(key)
}
pub fn remove(&mut self, key: &str) -> Option<String> {
self.0.remove(key)
}
#[cfg(any(feature = "csr", feature = "hydrate", feature = "ssr"))]
pub fn to_query_string(&self) -> String {
use crate::history::url::escape;
let mut buf = String::from("?");
for (k, v) in &self.0 {
buf.push_str(&escape(k));
buf.push('=');
buf.push_str(&escape(v));
buf.push('&');
}
buf
}
}
impl Default for ParamsMap {
fn default() -> Self {
Self::new()
}
}
#[macro_export]
macro_rules! params_map {
($($key:expr => $val:expr),* ,) => (
$crate::ParamsMap!($($key => $val),*)
);
($($key:expr => $val:expr),*) => ({
let start_capacity = common_macros::const_expr_count!($($key);*);
#[allow(unused_mut)]
let mut map = linear_map::LinearMap::with_capacity(start_capacity);
$( map.insert($key.to_string(), $val.to_string()); )*
$crate::ParamsMap(map)
});
}
pub trait Params
where
Self: Sized,
{
fn from_map(map: &ParamsMap) -> Result<Self, ParamsError>;
}
impl Params for () {
fn from_map(_map: &ParamsMap) -> Result<Self, ParamsError> {
Ok(())
}
}
pub trait IntoParam
where
Self: Sized,
{
fn into_param(value: Option<&str>, name: &str)
-> Result<Self, ParamsError>;
}
impl<T> IntoParam for Option<T>
where
T: FromStr,
<T as FromStr>::Err: std::error::Error + 'static,
{
fn into_param(
value: Option<&str>,
_name: &str,
) -> Result<Self, ParamsError> {
match value {
None => Ok(None),
Some(value) => match T::from_str(value) {
Ok(value) => Ok(Some(value)),
Err(e) => {
eprintln!("{e}");
Err(ParamsError::Params(Rc::new(e)))
}
},
}
}
}
cfg_if::cfg_if! {
if #[cfg(not(feature = "stable"))] {
auto trait NotOption {}
impl<T> !NotOption for Option<T> {}
impl<T> IntoParam for T
where
T: FromStr + NotOption,
<T as FromStr>::Err: std::error::Error + Send + Sync + 'static,
{
fn into_param(value: Option<&str>, name: &str) -> Result<Self, ParamsError> {
let value = value.ok_or_else(|| ParamsError::MissingParam(name.to_string()))?;
Self::from_str(value).map_err(|e| ParamsError::Params(Rc::new(e)))
}
}
}
}
#[derive(Error, Debug, Clone)]
pub enum ParamsError {
#[error("could not find parameter {0}")]
MissingParam(String),
#[error("failed to deserialize parameters")]
Params(Rc<dyn std::error::Error>),
}
impl PartialEq for ParamsError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::MissingParam(l0), Self::MissingParam(r0)) => l0 == r0,
(Self::Params(_), Self::Params(_)) => false,
_ => false,
}
}
}