DSN - Data Source Name Parser & Builder
A lightweight, fast, and type-safe Rust library for parsing and building Data Source Name (DSN) strings for databases like MySQL, PostgreSQL, Redis, and more.
Table of Contents
- Features
- Installation
- DSN Format
- Quick Start
- Database-Specific Builders
- Examples
- Real-World Integration
- Contributing
- License
Features
- Parse DSNs: Parse existing DSN strings into structured, type-safe data structures
- Build DSNs: Construct DSN strings programmatically with a fluent builder API
- Percent Encoding: Automatic percent-encoding for special characters in credentials
- Database Support: Pre-configured builders for MySQL, PostgreSQL, Redis, and MariaDB
- Protocol Support: TCP, Unix sockets, and file paths
- Type Safety: Comprehensive error handling with descriptive error types
- Zero Dependencies: Only depends on
percent-encoding - Edition 2024: Built with the latest Rust edition
Installation
Add this to your Cargo.toml:
[]
= "1.2"
DSN Format
The general DSN format is:
<driver>://<username>:<password>@<protocol>(<address>)/<database>?param1=value1&...¶mN=valueN
A complete DSN example:
mysql://username:password@tcp(host:port)/dbname?charset=utf8mb4
Protocol Support
The address format changes depending on the protocol:
TCP/UDP - host:port format:
postgresql://user:pass@tcp(localhost:5432)/dbname
Unix Domain Sockets - absolute path to socket:
mysql://user@unix(/var/run/mysqld/mysqld.sock)/database
File Paths (SQLite) - absolute file path:
sqlite://@file(/full/unix/path/to/file.db)
Percent Encoding
Special characters in usernames and passwords are automatically percent-encoded when using the builder API. For parsing, you can manually encode credentials:
# Using jq
|
# Using xxd
| |
Result:
mysql://root:p%40ss%3Aword%21@tcp(localhost:3306)/test
Quick Start
Parsing DSN Strings
use parse;
let dsn = parse?;
println!;
println!;
println!;
println!;
println!;
Building DSN Strings
use DSNBuilder;
// MySQL
let mysql = mysql
.username
.password
.host
.database
.param
.build;
println!;
// Output: mysql://root:secret@tcp(localhost:3306)/myapp?charset=utf8mb4
// PostgreSQL with SSL
let postgres = postgres
.username
.password
.host
.database
.param
.build;
// PostgreSQL with SSL disabled (development)
let postgres_dev = postgres
.username
.password
.host
.database
.param
.build;
// Redis
let redis = redis
.host
.password
.database
.build;
// Unix Socket
let socket_dsn = mysql
.username
.socket
.database
.build;
Converting Back to String
DSN structs implement Display, so you can convert them back to strings:
use parse;
let original = "mysql://root:pass@tcp(localhost:3306)/db";
let dsn = parse?;
// Convert back to string
let dsn_string = dsn.to_string;
// or
let dsn_string = format!;
Error Handling
The library provides descriptive error types:
use ;
match parse
Available error types:
InvalidDriver- Driver name is invalid or missingInvalidProtocol- Protocol is invalidInvalidSocket- Unix socket path is invalidInvalidPath- File path is not absoluteInvalidPort- Port number is invalid or out of rangeInvalidParams- Query parameters are malformedMissingAddress- Address is missing after protocolMissingHost- Host is missing in addressUtf8Error- UTF-8 decoding error in credentials
Database-Specific Builders
Pre-configured builders with sensible defaults:
| Builder | Driver | Default Port | Use Case |
|---|---|---|---|
DSNBuilder::mysql() |
mysql | 3306 | MySQL databases |
DSNBuilder::postgres() |
postgres | 5432 | PostgreSQL databases |
DSNBuilder::redis() |
redis | 6379 | Redis cache/store |
DSNBuilder::mariadb() |
mariadb | 3306 | MariaDB databases |
Examples
See the examples directory for comprehensive examples:
# General DSN building examples
# PostgreSQL SSL mode examples
Real-World Integration
MySQL
Using with the mysql crate:
use parse;
let dsn = parse?;
let mut opts = new;
opts.user;
opts.pass;
opts.ip_or_hostname;
if let Some = dsn.port
opts.db_name;
let pool = new?;
PostgreSQL
Using with the postgres crate:
use DSNBuilder;
let dsn = postgres
.username
.password
.host
.database
.param
.build;
let client = connect?;
Redis
Using with the redis crate:
use DSNBuilder;
let dsn = redis
.host
.password
.database
.build;
let client = open?;
Supported Rust Versions
This crate requires Rust 1.85 or later due to the use of Edition 2024.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
BSD-3-Clause - See LICENSE for details.