Skip to main content

Crate utopia_dsn

Crate utopia_dsn 

Source
Expand description

Parse data source names (scheme://user:password@host:port/path?query): the connection strings that configure databases, caches, queues and other services.

Dsn::parse splits a DSN into its scheme, credentials, host, port, path and query, and Dsn::param reads one query parameter. Error messages never quote the DSN, and the Debug output of a Dsn hides the password, so credentials do not leak into logs.

PHP: ports utopia-php/dsn.

PHPRust
new DSN($dsn)Dsn::parse
getScheme(), getHost()Dsn::scheme, Dsn::host
getUser(), getPassword()Dsn::user, Dsn::password (bytes: they are URL-decoded)
getPort() (?string)Dsn::port (Option<u16>; PHP’s string is its decimal form)
DSN::from(...)Dsn::from
getPath(), getQuery()Dsn::path (bytes: it is URL-decoded), Dsn::query
__toString()Dsn’s Display
getParam($key, $default)Dsn::param (Ok(None) where PHP returns $default), Dsn::param_or
\InvalidArgumentException, \TypeErrorError::InvalidArgument, Error::Type

PHP: the DSN is split by PHP’s parse_url and the query by PHP’s parse_str (php_std), so every input reads exactly as it does in PHP. PHP marks the argument #[\SensitiveParameter].

Read a database connection string:

use utopia_dsn::Dsn;

let dsn = Dsn::parse("mariadb://user:secret@localhost:3306/appwrite?charset=utf8mb4")?;
assert_eq!(dsn.scheme(), "mariadb");
assert_eq!(dsn.host(), "localhost");
assert_eq!(dsn.port(), Some(3306));
assert_eq!(dsn.path(), b"appwrite");
assert_eq!(dsn.param("charset")?, Some(&b"utf8mb4"[..]));
use Utopia\DSN\DSN;

$dsn = new DSN('mariadb://user:secret@localhost:3306/appwrite?charset=utf8mb4');
echo $dsn->getScheme(), "\n";          // mariadb
echo $dsn->getHost(), "\n";            // localhost
echo $dsn->getPort(), "\n";            // 3306
echo $dsn->getPath(), "\n";            // appwrite
echo $dsn->getParam('charset'), "\n";  // utf8mb4

Structs§

Dsn
A parsed DSN.

Enums§

Error
Why a DSN was refused or a parameter could not be read.