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
#![forbid(unsafe_code, future_incompatible)]
#![deny(
    missing_debug_implementations,
    nonstandard_style,
    missing_copy_implementations,
    unused_qualifications
)]

//! # QueryStrong: A flexible interface for querystrings
//!
//! Example:
//!
//! ```rust
//! # fn main() -> querystrong::Result<()> {
//! use querystrong::{QueryStrong, Value};
//! let mut qs = QueryStrong::parse("user[name][first]=jacob&user[language]=rust")?;
//! assert_eq!(qs["user[name][first]"], "jacob");
//! assert_eq!(qs["user"].get_str("language"), Some("rust"));
//! assert_eq!(qs.get_str("user[language]"), Some("rust"));
//! assert!(qs["user"].is_map());
//! assert!(qs["user[name]"].is_map());
//!
//! qs.append("user[name][last]", "rothstein")?;
//! qs.append("user[language]", "english")?;
//! assert_eq!(
//!   qs.to_string(),
//!   "user[language][]=rust&user[language][]=english&\
//!   user[name][first]=jacob&user[name][last]=rothstein"
//! );
//! # Ok(())  }
//! ```

use std::{
    fmt::{self, Debug, Display, Formatter, Write},
    ops::{Deref, DerefMut, Index},
    str::FromStr,
};

mod indexer;
pub use indexer::Indexer;

mod index_path;
pub use index_path::IndexPath;

mod value;
pub use value::Value;

mod error;
pub use error::{Error, Result};

#[derive(Clone, PartialEq, Eq)]
pub struct QueryStrong(Value);

impl QueryStrong {
    /// Creates a new (empty) querystrong that contains a map as the
    /// top level value
    pub fn new() -> Self {
        Self(Value::new_map())
    }

    /// Attempts to create a build a querystrong from the supplied querystring
    pub fn parse(s: &str) -> Result<Self> {
        s.parse()
    }
}

impl Default for QueryStrong {
    fn default() -> Self {
        Self::new()
    }
}

impl FromStr for QueryStrong {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let mut q = QueryStrong::new();
        let parts = s.split('&').filter(|kv| !kv.is_empty()).map(|kv| {
            if let Some(eq) = kv.find('=') {
                let (k, v) = kv.split_at(eq);
                (k, Some(&v[1..]))
            } else {
                (kv, None)
            }
        });

        for (key, value) in parts {
            q.append(key, value)?;
        }

        Ok(q)
    }
}

impl Deref for QueryStrong {
    type Target = Value;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for QueryStrong {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a> IntoIterator for &'a QueryStrong {
    type Item = (IndexPath, Option<String>);

    type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        Box::new(self.0.into_iter().filter_map(|(k, v)| match (k, v) {
            (Some(k), Some(v)) => {
                if k.is_empty() || k.get(0) == Some(&Indexer::Empty) {
                    Some((IndexPath::from(v), None))
                } else {
                    Some((k, Some(v)))
                }
            }
            (Some(k), None) => Some((k, None)),
            (None, Some(k)) => Some((Indexer::from(k).into(), None)),
            (None, None) => None,
        }))
    }
}

impl<K> Index<K> for QueryStrong
where
    K: Into<IndexPath>,
{
    type Output = Value;

    fn index(&self, key: K) -> &Self::Output {
        self.get(key).unwrap()
    }
}

impl Display for QueryStrong {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut first = true;

        for (key, value) in self {
            if first {
                first = false;
            } else {
                f.write_char('&')?;
            }

            f.write_str(&key.to_string())?;

            if let Some(value) = value {
                f.write_char('=')?;
                f.write_str(&value)?;
            }
        }
        Ok(())
    }
}

impl Debug for QueryStrong {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Debug::fmt(&self.0, f)
    }
}

impl<V: Into<Value>> From<V> for QueryStrong {
    fn from(value: V) -> Self {
        Self(value.into())
    }
}

pub(crate) fn decode(s: &str) -> String {
    percent_encoding::percent_decode_str(&s.replace('+', " "))
        .decode_utf8_lossy()
        .into()
}

pub(crate) fn encode(s: &str) -> String {
    static ASCII_SET: percent_encoding::AsciiSet =
        percent_encoding::NON_ALPHANUMERIC.remove(b'_').remove(b'-');

    percent_encoding::utf8_percent_encode(s, &ASCII_SET).to_string()
}

#[cfg(feature = "serde")]
impl serde::Serialize for QueryStrong {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        self.0.serialize(serializer)
    }
}