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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! A simple parser for database connection URIs.

use std::{
    collections::BTreeMap,
    convert::TryFrom,
    error::Error,
    fmt::{self, Display, Formatter},
    num::ParseIntError,
    str::{FromStr, Utf8Error},
};

/// A URI connection string.
///
/// # Format
///
/// ```not_rust
/// scheme://[username:password@]host[:port1][,...hostN[:portN]][/[database][?options]]
/// ```
///
/// # Warning
///
/// The parser does **not** support IPv6 or Unix pipes, manually build
/// the config instead.
#[derive(Debug, PartialEq)]
pub struct Uri {
    pub scheme: String,

    pub username: Option<String>,
    pub password: Option<String>,

    pub hosts: Vec<String>,
    pub ports: Vec<u16>,

    pub database: Option<String>,

    pub options: Option<BTreeMap<String, String>>,
}

impl Uri {
    pub fn new<S, H>(scheme: S, host: H, port: u16) -> Uri
    where
        S: Into<String>,
        H: Into<String>,
    {
        Uri {
            scheme: scheme.into(),
            username: None,
            password: None,
            hosts: vec![host.into()],
            ports: vec![port],
            database: None,
            options: None,
        }
    }

    pub fn parse<S>(text: S) -> Result<Self, UriError>
    where
        S: AsRef<str>,
    {
        let text = text.as_ref();

        let config = Parser::parse(text)?;

        Ok(config)
    }

    pub fn username<U>(mut self, username: U) -> Self
    where
        U: Into<String>,
    {
        self.username = Some(username.into());

        self
    }

    pub fn password<P>(mut self, password: P) -> Self
    where
        P: Into<String>,
    {
        self.password = Some(password.into());

        self
    }

    pub fn database<D>(mut self, database: D) -> Self
    where
        D: Into<String>,
    {
        self.database = Some(database.into());

        self
    }

    pub fn option<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        let tree = self.options.get_or_insert_with(BTreeMap::new);

        tree.insert(key.into(), value.into());

        self
    }
}

impl FromStr for Uri {
    type Err = UriError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let config = Uri::parse(s)?;

        Ok(config)
    }
}

impl<'s> TryFrom<&'s str> for Uri {
    type Error = UriError;

    fn try_from(value: &'s str) -> Result<Self, Self::Error> {
        let config = Uri::parse(value)?;

        Ok(config)
    }
}

impl TryFrom<String> for Uri {
    type Error = UriError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        let config = Uri::parse(value)?;

        Ok(config)
    }
}

// A macro version of `take_until` as the pattern api isn't stable yet
macro_rules! take_until {
    ($text:expr, $patt:expr) => {{
        match $text.find($patt) {
            Some(index) => {
                let (head, tail) = $text.split_at(index);

                $text = tail;

                Some(head)
            }
            None => None,
        }
    }};
}

// This parser is based off the one from tokio-postgres.
// Just needed one that didn't have too many dependencies.
struct Parser<'s> {
    text: &'s str,
}

impl<'s> Parser<'s> {
    fn parse(text: &'s str) -> Result<Uri, UriError> {
        let mut parser = Parser { text };

        let scheme = take_until!(parser.text, ':').ok_or(UriError::MissingScheme)?;

        parser.eat(':')?;
        parser.eat('/')?;
        parser.eat('/')?;

        let (username, password) = if parser.text.contains('@') {
            parser.parse_credentials()?
        } else {
            (None, None)
        };
        let (hosts, ports) = parser.parse_hosts()?;
        let database = parser.parse_path();
        let options = parser.parse_params()?;

        Ok(Uri {
            scheme: scheme.to_string(),

            username,
            password,

            hosts,
            ports,

            database,

            options,
        })
    }

    fn eat(&mut self, target: char) -> Result<(), UriError> {
        if self.text.starts_with(target) {
            let (_, tail) = self.text.split_at(1);

            self.text = tail;

            Ok(())
        } else {
            Err(UriError::UnexpectedCharacter {
                expected: target,
                got: self.text.chars().next().unwrap(),
            })
        }
    }

    fn parse_credentials(&mut self) -> Result<(Option<String>, Option<String>), UriError> {
        match take_until!(self.text, '@') {
            Some(taken) => {
                let mut it = taken.splitn(2, ':');

                let username = it.next().ok_or(UriError::MissingUsername)?;
                let password = percent_encoding::percent_decode(
                    it.next().ok_or(UriError::MissingPassword)?.as_bytes(),
                );

                self.eat('@')?;

                Ok((
                    Some(username.to_string()),
                    Some(password.decode_utf8()?.to_string()),
                ))
            }
            None => Ok((None, None)),
        }
    }

    fn parse_hosts(&mut self) -> Result<(Vec<String>, Vec<u16>), UriError> {
        match take_until!(self.text, &['/', '?'] as &[char]) {
            Some(taken) => {
                let pairs = taken.split(',');

                let mut hosts = Vec::new();
                let mut ports = Vec::new();

                for pair in pairs {
                    if let Some(index) = pair.find(':') {
                        let (head, tail) = pair.split_at(index);

                        hosts.push(head.to_string());
                        ports.push(
                            (tail[1..])
                                .parse()
                                .map_err(|err| (tail[1..].to_string(), err))?,
                        );
                    }
                }

                Ok((hosts, ports))
            }
            None => {
                if self.text.is_empty() {
                    Err(UriError::MissingHostPort)
                } else {
                    let mut hosts = Vec::new();
                    let mut ports = Vec::new();

                    if let Some(index) = self.text.find(':') {
                        let (head, tail) = self.text.split_at(index);

                        hosts.push(head.to_string());
                        ports.push(
                            (tail[1..])
                                .parse()
                                .map_err(|err| (tail[1..].to_string(), err))?,
                        );
                    }

                    Ok((hosts, ports))
                }
            }
        }
    }

    fn parse_path(&mut self) -> Option<String> {
        if self.text.starts_with('/') {
            self.text = &self.text[1..];

            if self.text.is_empty() {
                None
            } else if let Some(index) = self.text.find('?') {
                let (head, tail) = self.text.split_at(index);

                self.text = tail;

                Some(String::from(head))
            } else {
                Some(String::from(self.text))
            }
        } else {
            None
        }
    }

    fn parse_params(&mut self) -> Result<Option<BTreeMap<String, String>>, UriError> {
        if self.text.starts_with('?') {
            self.text = &self.text[1..];

            let mut tree = BTreeMap::new();

            for pair in self.text.split('&') {
                let mut splitter = pair.split('=');

                if let (Some(key), Some(value)) = (splitter.next(), splitter.next()) {
                    let key = percent_encoding::percent_decode(key.as_bytes()).decode_utf8()?;
                    let value = percent_encoding::percent_decode(value.as_bytes()).decode_utf8()?;

                    tree.insert(key.to_string(), value.to_string());
                }
            }

            Ok(if tree.is_empty() { None } else { Some(tree) })
        } else {
            Ok(None)
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum UriError {
    InvalidHostPort { port: String, err: ParseIntError },
    InvalidEncoding { err: Utf8Error },
    MissingScheme,
    MissingUsername,
    MissingPassword,
    MissingHostPort,
    UnexpectedEof,
    UnexpectedCharacter { expected: char, got: char },
}

impl Display for UriError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            UriError::InvalidHostPort { port, .. } => write!(f, "invalid host port: `{}`", port)?,
            UriError::InvalidEncoding { .. } => write!(f, "invalid param encoding")?,
            UriError::MissingScheme => write!(f, "missing scheme")?,
            UriError::MissingUsername => write!(f, "missing username from credentials")?,
            UriError::MissingPassword => write!(f, "missing password from credentials")?,
            UriError::MissingHostPort => write!(f, "missing host and or port")?,
            UriError::UnexpectedEof => write!(f, "unexpected EOF")?,
            UriError::UnexpectedCharacter { expected, got } => write!(
                f,
                "unexpected character: expected `{}` but got `{}`",
                expected, got,
            )?,
        }

        Ok(())
    }
}

impl Error for UriError {}

impl From<(String, ParseIntError)> for UriError {
    fn from((port, err): (String, ParseIntError)) -> Self {
        Self::InvalidHostPort { port, err }
    }
}

impl From<Utf8Error> for UriError {
    fn from(err: Utf8Error) -> Self {
        Self::InvalidEncoding { err }
    }
}

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

    #[test]
    fn test_minimal_no_encoding() {
        let expected = Uri::new("postgres", "localhost", 54123);
        let actual = Uri::parse("postgres://localhost:54123");

        assert_eq!(Ok(expected), actual);
    }

    #[test]
    fn test_options_encoding() {
        let expected = Uri::new("postgres", "localhost", 54123).option("with a space", "for sure");
        let actual = Uri::parse("postgres://localhost:54123?with%20a%20space=for%20sure");

        assert_eq!(Ok(expected), actual);
    }

    #[test]
    fn test_all_no_encoding() {
        let expected = Uri::new("postgres", "localhost", 54123)
            .username("username")
            .password("password")
            .database("database")
            .option("tls", "true");
        let actual = Uri::parse("postgres://username:password@localhost:54123/database?tls=true");

        assert_eq!(Ok(expected), actual);
    }
}