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
use std::borrow::Cow;
use std::collections::HashMap;

use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC};

use crate::error::Error;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
/// Parsed representation of database connection URI
pub struct Options<'a> {
    /// The URI schema
    pub scheme: Cow<'a, str>,
    /// The authenticating user name
    pub user: Cow<'a, str>,
    /// The authenticating user password
    pub password: Cow<'a, str>,
    /// The host name
    pub host: Cow<'a, str>,
    /// The path component
    pub path: Cow<'a, str>,
    /// The query component
    pub query: HashMap<String, String>,
    /// The fragment component
    pub fragment: Cow<'a, str>,
}

impl<'a> Options<'a> {
    /// Parse a URI string into an Options structure
    pub fn parse_uri(uri: &str) -> Result<Options<'_>, Error> {
        let mut fragment_and_remain = uri.splitn(2, '#');
        let uri = fragment_and_remain.next().unwrap_or_default();
        let fragment = percent_decode(fragment_and_remain.next().unwrap_or_default());
        let mut scheme_and_remain = uri.splitn(2, ':');
        let scheme = scheme_and_remain.next().unwrap_or_default();

        let (scheme, host_and_query) = if let Some(remain) = scheme_and_remain.next() {
            if scheme.is_empty() {
                ("", uri)
            } else {
                (scheme, remain.trim_start_matches("//"))
            }
        } else {
            ("", uri)
        };
        let scheme = percent_decode(scheme);

        let mut host_and_query = host_and_query.splitn(2, '?');
        let (user, password, host) = {
            let mut user_and_host = host_and_query.next().unwrap_or_default().splitn(2, '@');
            let user_pass = user_and_host.next().unwrap_or_default();
            if let Some(host) = user_and_host.next() {
                let mut user_pass = user_pass.splitn(2, ':');
                let user = percent_decode(user_pass.next().unwrap_or_default());
                let pass = percent_decode(user_pass.next().unwrap_or_default());
                (user, pass, host)
            } else {
                (Cow::Borrowed(""), Cow::Borrowed(""), user_pass)
            }
        };
        let (host, path) = if let Some(path_pos) = host.find('/') {
            (
                percent_decode(&host[..path_pos]),
                percent_decode(&host[path_pos..]),
            )
        } else {
            (percent_decode(host), Cow::Borrowed(""))
        };

        let query = if let Some(query) = host_and_query.next() {
            url::form_urlencoded::parse(query.as_bytes())
                .into_owned()
                .fold(HashMap::new(), |mut map, (k, v)| {
                    map.insert(k, v);
                    map
                })
        } else {
            HashMap::new()
        };

        Ok(Options {
            user,
            password,
            host,
            path,
            scheme,
            query,
            fragment,
        })
    }

    /// Convert an options structure back into a string
    pub fn into_uri(self) -> String {
        let mut uri = String::new();
        if !self.scheme.is_empty() {
            percent_encode_into(&mut uri, &self.scheme);
            uri.push_str("://");
        }
        if !self.user.is_empty() || !self.password.is_empty() {
            percent_encode_into(&mut uri, &self.user);
            uri.push(':');
            percent_encode_into(&mut uri, &self.password);
            uri.push('@');
        }
        uri.push_str(&self.host);
        uri.push_str(&self.path);
        if !self.query.is_empty() {
            uri.push('?');
            for (k, v) in self.query {
                push_iter_str(&mut uri, url::form_urlencoded::byte_serialize(k.as_bytes()));
                uri.push('=');
                push_iter_str(&mut uri, url::form_urlencoded::byte_serialize(v.as_bytes()));
            }
        }
        if !self.fragment.is_empty() {
            uri.push('#');
            percent_encode_into(&mut uri, &self.fragment);
        }
        uri
    }
}

#[inline]
fn push_iter_str<'a, I: Iterator<Item = &'a str>>(s: &mut String, iter: I) {
    for item in iter {
        s.push_str(item);
    }
}

#[inline]
fn percent_decode(s: &str) -> Cow<'_, str> {
    percent_decode_str(s).decode_utf8_lossy()
}

#[inline]
fn percent_encode_into(result: &mut String, s: &str) {
    push_iter_str(result, utf8_percent_encode(s, NON_ALPHANUMERIC))
}

/// A trait implemented by types that can be converted into Options
pub trait IntoOptions<'a> {
    /// Try to convert self into an Options structure
    fn into_options(self) -> Result<Options<'a>, Error>;
}

impl<'a> IntoOptions<'a> for Options<'a> {
    fn into_options(self) -> Result<Options<'a>, Error> {
        Ok(self)
    }
}

impl<'a> IntoOptions<'a> for &'a str {
    fn into_options(self) -> Result<Options<'a>, Error> {
        Options::parse_uri(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn options_basic() {
        let opts = Options::parse_uri("scheme://user%2E:pass@host/dbname?a+1=b#frag").unwrap();
        let bs = Cow::Borrowed;
        assert_eq!(
            opts,
            Options {
                user: bs("user."),
                password: bs("pass"),
                scheme: bs("scheme"),
                host: bs("host"),
                path: bs("/dbname"),
                query: HashMap::from_iter(vec![("a 1".to_owned(), "b".to_owned())]),
                fragment: bs("frag"),
            }
        );
    }

    #[test]
    fn options_no_schema() {
        let opts = Options::parse_uri("dbname/path?a#frag").unwrap();
        assert_eq!(
            opts,
            Options {
                user: Default::default(),
                password: Default::default(),
                scheme: Default::default(),
                host: Cow::Borrowed("dbname"),
                path: Cow::Borrowed("/path"),
                query: HashMap::from_iter(vec![("a".to_owned(), "".to_owned())]),
                fragment: Cow::Borrowed("frag")
            }
        );
    }

    #[test]
    fn options_round_trip() {
        let opts_str = "schema://user%2F:pass@dbname?a+1=b#frag%2E";
        let opts = Options::parse_uri(opts_str).unwrap();
        assert_eq!(opts.into_uri(), opts_str);
    }
}