utopia-dsn 0.1.0

Data source names (connection strings), parsed into scheme, credentials, host, port, path and options.
Documentation
  • Coverage
  • 100%
    17 out of 17 items documented15 out of 16 items with examples
  • Size
  • Source code size: 56.2 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 215.5 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 27s Average build duration of successful builds.
  • all releases: 27s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • appwrite/appwrite
    58649 5915 288
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • loks0n

Utopia DSN

Discord

Data source names (connection strings), parsed into scheme, credentials, host, port, path and options.

This library is maintained by the Appwrite team.

Getting started

[dependencies]
utopia-dsn = { git = "https://github.com/appwrite/appwrite" }

A DSN (data source name) puts everything needed to reach a service in one string: scheme://user:password@host:port/path?query. Applications read them from configuration so a database, cache or provider can change without code changes. This guide covers the common tasks.

Read a connection string

Parse the string once, then read each part. A missing part is None (or empty for the path).

use utopia_dsn::Dsn;

let dsn = Dsn::parse("postgresql://appwrite:secret@db.internal:5432/appwrite")?;

assert_eq!(dsn.scheme(), "postgresql");
assert_eq!(dsn.host(), "db.internal");
assert_eq!(dsn.port(), Some(5432));
assert_eq!(dsn.path(), b"appwrite");
assert_eq!(dsn.user(), Some(&b"appwrite"[..]));

Use credentials with special characters

Passwords often contain @, / or :, which have a meaning in a DSN. Percent-encode them in the string; the parser decodes them. Credentials are bytes in Rust, because a decoded password can be any bytes.

use utopia_dsn::Dsn;

let dsn = Dsn::parse("redis://default:p%40ss%2Fw%3Ard@cache:6379")?;

assert_eq!(dsn.password(), Some(&b"p@ss/w:rd"[..]));
let password = String::from_utf8_lossy(dsn.password().unwrap_or_default());
assert_eq!(password, "p@ss/w:rd");

Keep secrets out of logs

Printing a DSN for debugging never shows its password, and errors never quote the string they refused.

use utopia_dsn::Dsn;

let dsn = Dsn::parse("mariadb://root:hunter2@db:3306/appwrite")?;
let logged = format!("{dsn:?}");

assert!(logged.contains("db"));
assert!(!logged.contains("hunter2"));

Read options from the query string

Settings that are not part of the address go in the query. Read one with param, or with param_or to fall back to a default.

use utopia_dsn::Dsn;

let dsn = Dsn::parse("s3://key:secret@s3.amazonaws.com/uploads?region=eu-central-1&acl=private")?;

assert_eq!(dsn.param("region")?, Some(&b"eu-central-1"[..]));
assert_eq!(dsn.param_or("acl", b"public-read")?, b"private");
assert_eq!(dsn.param_or("endpoint", b"https://s3.amazonaws.com")?, b"https://s3.amazonaws.com");

Configure a service from the environment

Read the DSN from an environment variable, with a default for local development, and pick an adapter by its scheme.

use utopia_dsn::Dsn;

let value = std::env::var("APP_CACHE").unwrap_or_else(|_| "redis://localhost:6379".to_owned());
let dsn = Dsn::parse(&value)?;

let adapter = match dsn.scheme() {
    "redis" => format!("Redis at {}:{}", dsn.host(), dsn.port().unwrap_or(6379)),
    "memcached" => format!("Memcached at {}", dsn.host()),
    other => format!("unsupported cache: {other}"),
};
assert_eq!(adapter, "Redis at localhost:6379");

Reject invalid configuration

A DSN needs a scheme and a host. Anything else fails with an error you can report without exposing the value.

use utopia_dsn::{Dsn, Error};

for value in ["localhost:6379", "redis://", "not a dsn"] {
    match Dsn::parse(value) {
        Err(Error::InvalidArgument(reason)) => println!("invalid cache configuration: {reason}"),
        other => panic!("expected an error, got {other:?}"),
    }
}

Contributing

Development happens in appwrite/appwrite, where this crate lives at crates/utopia/dsn. Run these commands from a checkout of it. The contributing guide covers each step.

Test

cargo test -p utopia-dsn
cargo clippy -p utopia-dsn --all-targets -- -D warnings
cargo fmt --all --check

Compare with PHP

The utopia PHP extension serves this crate as Utopia\DSN\DSN, which no longer has a PHP implementation.

License

BSD-3-Clause. See LICENSE.