Skip to main content

db_mover/
uri.rs

1use std::str::FromStr;
2
3#[derive(Debug, Clone)]
4pub enum URI {
5    Sqlite(String),
6    Postgres(String),
7    Mysql(String),
8}
9
10impl FromStr for URI {
11    type Err = String;
12
13    fn from_str(s: &str) -> Result<URI, Self::Err> {
14        if s.starts_with("sqlite://") {
15            return Ok(URI::Sqlite(s.to_owned()));
16        }
17        if s.starts_with("postgres://") || s.starts_with("postgresql://") {
18            return Ok(URI::Postgres(s.to_owned()));
19        }
20        if s.starts_with("mysql://") {
21            return Ok(URI::Mysql(s.to_owned()));
22        }
23        return Err("Unknown URI format".to_string());
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_uri_from_str_sqlite() {
33        let uri = URI::from_str("sqlite://test.db");
34        assert!(matches!(uri, Ok(URI::Sqlite(_))));
35    }
36
37    #[test]
38    fn test_uri_from_str_postgres() {
39        let uri = URI::from_str("postgres://user:pass@localhost/db");
40        assert!(matches!(uri, Ok(URI::Postgres(_))));
41    }
42
43    #[test]
44    fn test_uri_from_str_mysql() {
45        let uri = URI::from_str("mysql://user@localhost:3306");
46        assert!(matches!(uri, Ok(URI::Mysql(_))));
47    }
48
49    #[test]
50    fn test_uri_from_str_invalid() {
51        let uri = URI::from_str("invalid://test");
52        assert!(uri.is_err());
53    }
54}