connparse
Parse database, cache, file, and storage connection strings into one predictable
Rust value.
Connparse is useful when your app accepts connection strings from different
systems and you want to pull out the host, port, database, bucket, path,
credentials, query options, and a safe redacted string.
Install
[dependencies]
connparse = "0.3"
Basic Usage
use connparse::parse;
fn main() {
let result = parse("postgres://user:pass@localhost:5432/app?sslmode=require", None);
if !result.ok {
eprintln!("{:?}", result.errors);
return;
}
let address = result.value.unwrap();
println!("{}", address.safe);
}
parse() returns the same JSON-shaped result contract as the JavaScript, Go,
and Python packages:
{
"ok": true,
"value": {
"scheme": "postgres",
"type": "database",
"authority": {
"host": "localhost",
"port": 5432
},
"resource": {
"type": "database",
"name": "app"
},
"path": "",
"query": {
"sslmode": "require"
},
"fragment": null,
"credentials": {
"username": "user",
"password": "pass"
},
"options": {},
"raw": "postgres://user:pass@localhost:5432/app?sslmode=require",
"safe": "postgres://user:***@localhost:5432/app?sslmode=require"
},
"errors": [],
"warnings": []
}
Use safe for logs and UI labels. Do not log raw or credentials unless the
user explicitly asks to reveal secrets.
For stable comparison keys, use parse_normalize():
use connparse::parse_normalize;
let result = parse_normalize("postgresql://LOCALHOST:5432/app?sslmode=require", None);
assert_eq!(
result.value.unwrap().canonical,
"postgres://localhost/app?sslmode=require"
);