nova_forms/
query_string.rs

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::{collections::HashMap, fmt::Display, str::FromStr};

use leptos::*;
use percent_encoding::{percent_decode, percent_encode, NON_ALPHANUMERIC};
use serde::Serialize;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum QueryStringPart {
    Index(usize),
    Key(String),
}

impl Display for QueryStringPart {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryStringPart::Index(i) => write!(f, "{}", i),
            QueryStringPart::Key(k) => write!(f, "{}", k),
        }
    }
}

/// Used to bind a form input element to a form data element.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct QueryString(Vec<QueryStringPart>);

impl QueryString {
    /// Checks whether the current query string extends the other query string.
    /// 
    /// ```rust
    /// assert_eq!(QueryString::from("form_data[a][b]").extends(QueryString::from("form_data[a]")), Some(QueryString::from("b")));
    /// ```
    fn extends(&self, other: &Self) -> Option<QueryString> {
        if self.0.len() < other.0.len() {
            return None;
        }

        if !self.0.iter().zip(other.0.iter()).all(|(s, o)| s == o) {
            return None;
        }

        Some(QueryString(
            self.0.iter().skip(other.0.len()).cloned().collect(),
        ))
    }

    /// Gets the `QueryString` and the serialized `FormData` for the current context.
    pub fn form_context(&self) -> (QueryString, FormDataSerialized) {
        let form_data = expect_context::<FormDataSerialized>();
        let curr_form_data = form_data.level(&self);
        let prefix_qs = expect_context::<QueryString>();
        let curr_qs = prefix_qs.join(self.clone());
        (curr_qs, curr_form_data)
    }

    /// Gets the `QueryString` and the serialized value for the current context.
    /// This is very similar to `form_context`, but it assumes that `FormData` only contains one value
    /// which is deserializable into the type `T`.
    pub fn form_value<T: FromStr>(&self) -> (QueryString, Result<T, <T as FromStr>::Err>) {
        let form_data = expect_context::<FormDataSerialized>();
        let value = T::from_str(&form_data
            .exact(&self)
            .unwrap_or_default());
        let prefix_qs = expect_context::<QueryString>();
        let curr_qs = prefix_qs.join(self.clone());
        (curr_qs, value)
    }

    /// Joins two `QueryString`s.
    pub fn join(self, mut other: Self) -> Self {
        let mut parts = self.0;
        parts.append(&mut other.0);
        QueryString(parts)
    }

    pub fn add(mut self, part: QueryStringPart) -> Self {
        self.0.push(part);
        self
    }

    pub fn add_index(mut self, index: usize) -> Self {
        self.0.push(QueryStringPart::Index(index));
        self
    }

    pub fn add_key<K: Into<String>>(mut self, key: K) -> Self {
        self.0.push(QueryStringPart::Key(key.into()));
        self
    }
}

impl IntoAttribute for QueryString {
    fn into_attribute(self) -> Attribute {
        Attribute::String(Oco::Owned(format!("{self}")))
    }

    fn into_attribute_boxed(self: Box<Self>) -> Attribute {
        Attribute::String(Oco::Owned(format!("{self}")))
    }
}

impl From<&str> for QueryString {
    fn from(value: &str) -> Self {
        let mut chars = value.chars();
        let mut parts = Vec::new();
        while let Some(c) = chars.next() {
            match c {
                '[' => parts.push(String::new()),
                ']' => {}
                _ => {
                    if let Some(last) = parts.last_mut() {
                        last.push(c);
                    } else {
                        parts.push(String::from(c));
                    }
                }
            }
        }
        QueryString(
            parts
                .into_iter()
                .map(|p| {
                    p.parse::<usize>()
                        .map(QueryStringPart::Index)
                        .unwrap_or_else(|_| QueryStringPart::Key(p))
                })
                .collect(),
        )
    }
}

impl From<String> for QueryString {
    fn from(value: String) -> Self {
        QueryString::from(value.as_str())
    }
}

impl Display for QueryString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(first) = self.0.first() {
            write!(f, "{}", first)?;
        }
        for part in self.0.iter().skip(1) {
            write!(f, "[{}]", part)?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct FormDataSerialized(HashMap<QueryString, String>);

impl<F: Serialize> From<F> for FormDataSerialized {
    fn from(form_data: F) -> Self {
        let serialized = serde_qs::to_string(&form_data).expect("must be serializable");
        FormDataSerialized::from_str(&serialized).unwrap()
    }
}

impl FromStr for FormDataSerialized {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, ()> {
        let map = s
            .split("&")
            .into_iter()
            .map(|pair| {
                pair.split_once("=")
                    .map(|(k, v)| {
                        (
                            QueryString::from(k),
                            percent_decode(v.as_bytes()).decode_utf8_lossy().to_string(),
                        )
                    })
                    .unwrap_or_else(|| (QueryString::from(pair), String::new()))
            })
            .collect();

        Ok(FormDataSerialized(map))
    }
}

impl ToString for FormDataSerialized {
    fn to_string(&self) -> String {
        self.0
            .iter()
            .map(|(k, v)| format!("{}={}", k, percent_encode(v.as_bytes(), NON_ALPHANUMERIC)))
            .collect::<Vec<_>>()
            .join("&")
    }
}

impl FormDataSerialized {
    pub fn exact(&self, key: &QueryString) -> Option<String> {
        self.0.get(&key).map(|s| s.to_owned())
    }

    pub fn level(&self, head: &QueryString) -> FormDataSerialized {
        let map = self
            .0
            .iter()
            .filter_map(|(k, v)| k.extends(head).map(|k| (k, v.to_owned())))
            .collect();
        FormDataSerialized(map)
    }

    pub fn len(&self) -> Option<usize> {
        self.0
            .keys()
            .map(|k| {
                k.0.first().and_then(|p| {
                    if let QueryStringPart::Index(i) = p {
                        Some(*i)
                    } else {
                        None
                    }
                })
            })
            .reduce(|l1, l2| {
                if let (Some(l1), Some(l2)) = (l1, l2) {
                    Some(l1.max(l2))
                } else {
                    None
                }
            })
            .flatten()
            .map(|l| l + 1)
    }
}