pub fn parse(input: &str) -> Result<DSN, ParseError>Expand description
Parse a DSN string into a structured DSN object
This function parses a Data Source Name (DSN) string and extracts all components including driver, credentials, protocol, address, database name, and parameters.
§Arguments
input- A DSN string in the format:driver://username:password@protocol(address)/database?param=value
§Returns
Returns a Result containing the parsed DSN struct on success, or a
ParseError if the DSN string is malformed.
§Errors
Returns ParseError in the following cases:
InvalidDriver- Missing or invalid driver nameInvalidProtocol- Missing or invalid protocolInvalidSocket- Unix socket path doesn’t start with/InvalidPath- File path is not absoluteInvalidPort- Port number is invalid or out of range (0-65535)MissingAddress- Address is missing after protocolMissingHost- Host is missing in TCP/UDP addressInvalidParams- Query parameters are malformedUtf8Error- Percent-encoded credentials contain invalid UTF-8
§Examples
Basic TCP connection:
use dsn::parse;
let dsn = parse(r#"mysql://user:o%3Ao@tcp(localhost:3306)/database?charset=utf8"#).unwrap();
assert_eq!(dsn.driver, "mysql");
assert_eq!(dsn.username.unwrap(), "user");
assert_eq!(dsn.password.unwrap(), "o:o");
assert_eq!(dsn.protocol, "tcp");
assert_eq!(dsn.address, "localhost:3306");
assert_eq!(dsn.host.unwrap(), "localhost");
assert_eq!(dsn.port.unwrap(), 3306);
assert_eq!(dsn.database.unwrap(), "database");
assert_eq!(dsn.socket, None);
assert!(!dsn.params.is_empty());
assert_eq!(dsn.params.get("charset").unwrap(), "utf8");Unix socket connection:
use dsn::parse;
let dsn = parse(r"mysql://user@unix(/var/run/mysql.sock)/mydb").unwrap();
assert_eq!(dsn.protocol, "unix");
assert_eq!(dsn.socket.unwrap(), "/var/run/mysql.sock");Examples found in repository?
examples/postgres_ssl.rs (line 51)
5fn main() {
6 println!("=== PostgreSQL SSL Mode Examples ===\n");
7
8 // Building DSNs with different SSL modes
9 println!("1. Building PostgreSQL DSNs:\n");
10
11 let ssl_require = DSNBuilder::postgres()
12 .username("app")
13 .password("secret")
14 .host("prod.postgres.com")
15 .database("app_db")
16 .param("sslmode", "require")
17 .build();
18 println!(" Production (SSL required):");
19 println!(" {ssl_require}\n");
20
21 let ssl_prefer = DSNBuilder::postgres()
22 .username("app")
23 .password("secret")
24 .host("staging.postgres.com")
25 .database("app_db")
26 .param("sslmode", "prefer")
27 .build();
28 println!(" Staging (SSL preferred):");
29 println!(" {ssl_prefer}\n");
30
31 let ssl_disable = DSNBuilder::postgres()
32 .username("dev")
33 .password("dev123")
34 .host("localhost")
35 .database("dev_db")
36 .param("sslmode", "disable")
37 .build();
38 println!(" Development (SSL disabled):");
39 println!(" {ssl_disable}\n");
40
41 // Parsing and checking SSL mode
42 println!("2. Parsing PostgreSQL DSN and checking SSL mode:\n");
43
44 let examples = [
45 "postgres://user:pass@tcp(localhost:5432)/mydb?sslmode=disable",
46 "postgres://user:pass@tcp(prod.db.com:5432)/mydb?sslmode=require",
47 "postgres://user:pass@tcp(stage.db.com:5432)/mydb?sslmode=prefer&connect_timeout=10",
48 ];
49
50 for dsn_str in examples {
51 match parse(dsn_str) {
52 Ok(dsn) => {
53 println!(" DSN: {dsn_str}");
54 println!(" Host: {}", dsn.host.as_ref().unwrap());
55 println!(" Database: {}", dsn.database.as_ref().unwrap());
56
57 if let Some(sslmode) = dsn.params.get("sslmode") {
58 println!(" SSL Mode: {sslmode}");
59
60 match sslmode.as_str() {
61 "disable" => {
62 println!(
63 " [WARNING] SSL is disabled - not recommended for production!"
64 );
65 }
66 "require" => println!(" [OK] SSL is required - secure connection"),
67 "prefer" => println!(" [INFO] SSL is preferred - will use if available"),
68 "verify-ca" => println!(" [SECURE] SSL with CA verification"),
69 "verify-full" => println!(" [SECURE] SSL with full verification"),
70 _ => println!(" [UNKNOWN] Unknown SSL mode: {sslmode}"),
71 }
72 } else {
73 println!(" [INFO] No SSL mode specified (will use PostgreSQL default)");
74 }
75
76 // Check for other connection parameters
77 if let Some(timeout) = dsn.params.get("connect_timeout") {
78 println!(" Connection timeout: {timeout}s");
79 }
80
81 println!();
82 }
83 Err(e) => {
84 eprintln!(" [ERROR] Failed to parse: {e}");
85 }
86 }
87 }
88
89 // Practical usage example
90 println!("3. Practical usage - connection string selection:\n");
91
92 let environment = std::env::var("ENVIRONMENT").unwrap_or_else(|_| "development".to_string());
93
94 let dsn = match environment.as_str() {
95 "production" => DSNBuilder::postgres()
96 .username("prod_user")
97 .password("prod_pass")
98 .host("prod.postgres.com")
99 .database("prod_db")
100 .param("sslmode", "require")
101 .param("connect_timeout", "30")
102 .build(),
103
104 "staging" => DSNBuilder::postgres()
105 .username("stage_user")
106 .password("stage_pass")
107 .host("staging.postgres.com")
108 .database("stage_db")
109 .param("sslmode", "prefer")
110 .param("connect_timeout", "10")
111 .build(),
112
113 _ => DSNBuilder::postgres()
114 .username("dev")
115 .password("dev")
116 .host("localhost")
117 .database("dev_db")
118 .param("sslmode", "disable")
119 .build(),
120 };
121
122 println!(" Environment: {environment}");
123 println!(" Connection string: {dsn}");
124}