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
use std::{error::Error, fmt};

#[derive(Debug, Default)]
pub struct DSN {
    driver: &'static str,
    username: &'static str,
    password: &'static str,
    host: &'static str,
    port: u16,
    database: &'static str,
    socket: &'static str,
}

#[derive(Debug)]
pub enum ParseError {
    InvalidPort,
}

impl fmt::Display for ParseError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.description().fmt(fmt)
    }
}

impl Error for ParseError {
    fn description(&self) -> &str {
        match *self {
            ParseError::InvalidPort => "invalid port number",
        }
    }
}

pub fn parse(input: &str) -> Result<DSN, ParseError> {
    for c in input.chars() {
        match c {
            ':' => println!("push to buffer"),
            _ => println!("{}", c),
        }
    }
    let dsn = DSN::default();
    Ok(dsn)
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

pub fn default_port(scheme: &str) -> Option<u16> {
    match scheme {
        "mysql" => Some(3306),
        "pgsql" => Some(5432),
        "redis" => Some(6379),
        _ => None,
    }
}