pub struct Dsn { /* private fields */ }Expand description
A parsed DSN.
Its Debug output hides the password:
use utopia_dsn::Dsn;
let dsn = Dsn::parse("redis://default:secret@cache:6379")?;
let printed = format!("{dsn:?}");
assert!(printed.contains("cache"));
assert!(!printed.contains("secret"));use Utopia\DSN\DSN;
$dsn = new DSN('redis://default:secret@cache:6379');
echo $dsn->getHost(), "\n"; // cacheImplementations§
Source§impl Dsn
impl Dsn
Sourcepub fn parse(dsn: &str) -> Result<Self, Error>
pub fn parse(dsn: &str) -> Result<Self, Error>
Parses a DSN.
Fails with Error::InvalidArgument when the input is malformed, or
the scheme or host is missing or empty. A scheme or host of "0"
counts as missing.
PHP: new DSN($dsn). It fails where PHP’s parse_url rejects the
input, and "0" is missing because PHP’s empty() says so.
use utopia_dsn::Dsn;
let dsn = Dsn::parse("redis://cache:6379")?;
assert_eq!(dsn.host(), "cache");
assert!(Dsn::parse("redis://").is_err());use Utopia\DSN\DSN;
$dsn = new DSN('redis://cache:6379');
echo $dsn->getHost(), "\n"; // cacheSourcepub fn from(
scheme: &str,
host: &str,
user: Option<&[u8]>,
password: Option<&[u8]>,
port: Option<i64>,
path: &[u8],
params: &[(&str, &str)],
) -> Result<Self, Error>
pub fn from( scheme: &str, host: &str, user: Option<&[u8]>, password: Option<&[u8]>, port: Option<i64>, path: &[u8], params: &[(&str, &str)], ) -> Result<Self, Error>
Builds a DSN from its parts, encoding each one, so a value cannot
reshape it (a ? in the path starting the query, an @ in the user
moving the host).
Fails with Error::InvalidArgument when password is given without
user, or when a part would not read back exactly as given (a host
holding /, @ or :, a parameter key getParam rewrites, like
a.b or x[y]).
PHP: DSN::from($scheme, $host, $user, $password, $port, $path, $params). The port is PHP’s ?int; a key of params that is a
canonical integer is an integer key, as in a PHP array literal, and a
repeated key keeps its last value.
use utopia_dsn::Dsn;
let dsn = Dsn::from(
"s3",
"minio.example.com",
Some(b"admin@example.com"),
Some(b"p@ss/word"),
Some(9000),
b"bucket",
&[("region", "us-east-1")],
)?;
assert_eq!(dsn.to_string(), "s3://admin%40example.com:p%40ss%2Fword@minio.example.com:9000/bucket?region=us-east-1");
assert!(Dsn::from("s3", "user@minio.example.com", None, None, None, b"", &[]).is_err());use Utopia\DSN\DSN;
$dsn = DSN::from(
scheme: 's3',
host: 'minio.example.com',
user: 'admin@example.com',
password: 'p@ss/word',
port: 9000,
path: 'bucket',
params: ['region' => 'us-east-1'],
);
echo $dsn, "\n"; // s3://admin%40example.com:p%40ss%2Fword@minio.example.com:9000/bucket?region=us-east-1Sourcepub fn scheme(&self) -> &str
pub fn scheme(&self) -> &str
The scheme: the part before ://.
PHP: getScheme().
use utopia_dsn::Dsn;
let dsn = Dsn::parse("postgresql://db:5432/appwrite")?;
assert_eq!(dsn.scheme(), "postgresql");use Utopia\DSN\DSN;
$dsn = new DSN('postgresql://db:5432/appwrite');
echo $dsn->getScheme(), "\n"; // postgresqlSourcepub fn user(&self) -> Option<&[u8]>
pub fn user(&self) -> Option<&[u8]>
The URL-decoded user, which can be any bytes (%FF).
PHP: getUser().
use utopia_dsn::Dsn;
let dsn = Dsn::parse("smtp://mail%40example.com:secret@smtp.example.com:587")?;
assert_eq!(dsn.user(), Some(&b"mail@example.com"[..]));use Utopia\DSN\DSN;
$dsn = new DSN('smtp://mail%40example.com:secret@smtp.example.com:587');
echo $dsn->getUser(), "\n"; // mail@example.comSourcepub fn password(&self) -> Option<&[u8]>
pub fn password(&self) -> Option<&[u8]>
The URL-decoded password, which can be any bytes (%FF).
PHP: getPassword().
use utopia_dsn::Dsn;
let dsn = Dsn::parse("redis://default:p%40ss%2Fword@cache")?;
assert_eq!(dsn.password(), Some(&b"p@ss/word"[..]));use Utopia\DSN\DSN;
$dsn = new DSN('redis://default:p%40ss%2Fword@cache');
echo $dsn->getPassword(), "\n"; // p@ss/wordSourcepub fn host(&self) -> &str
pub fn host(&self) -> &str
The host name or address.
PHP: getHost().
use utopia_dsn::Dsn;
let dsn = Dsn::parse("mongodb://user:secret@mongo.internal:27017/appwrite")?;
assert_eq!(dsn.host(), "mongo.internal");use Utopia\DSN\DSN;
$dsn = new DSN('mongodb://user:secret@mongo.internal:27017/appwrite');
echo $dsn->getHost(), "\n"; // mongo.internalSourcepub fn port(&self) -> Option<u16>
pub fn port(&self) -> Option<u16>
The port, or None when the DSN has none.
PHP: getPort() returns the port as a string (or null); Rust as a
number.
use utopia_dsn::Dsn;
assert_eq!(Dsn::parse("redis://cache:6379")?.port(), Some(6379));
assert_eq!(Dsn::parse("redis://cache")?.port(), None);use Utopia\DSN\DSN;
var_dump((new DSN('redis://cache:6379'))->getPort()); // string(4) "6379"
var_dump((new DSN('redis://cache'))->getPort()); // NULLSourcepub fn path(&self) -> &[u8] ⓘ
pub fn path(&self) -> &[u8] ⓘ
The URL-decoded path without its leading slashes, which can be any
bytes (%FF); empty when there is none.
PHP: getPath().
use utopia_dsn::Dsn;
assert_eq!(Dsn::parse("s3://key:secret@s3.amazonaws.com/backups/daily")?.path(), b"backups/daily");
assert_eq!(Dsn::parse("s3://localhost/b%3Furl%3Dx")?.path(), b"b?url=x");
assert_eq!(Dsn::parse("redis://cache")?.path(), b"");use Utopia\DSN\DSN;
echo (new DSN('s3://key:secret@s3.amazonaws.com/backups/daily'))->getPath(), "\n"; // backups/dailySourcepub fn query(&self) -> Option<&str>
pub fn query(&self) -> Option<&str>
The raw query string, or None when the DSN has none.
PHP: getQuery().
use utopia_dsn::Dsn;
let dsn = Dsn::parse("mariadb://db/appwrite?charset=utf8mb4&timeout=5")?;
assert_eq!(dsn.query(), Some("charset=utf8mb4&timeout=5"));use Utopia\DSN\DSN;
echo (new DSN('mariadb://db/appwrite?charset=utf8mb4&timeout=5'))->getQuery(), "\n"; // charset=utf8mb4&timeout=5Sourcepub fn param(&self, key: impl AsRef<[u8]>) -> Result<Option<&[u8]>, Error>
pub fn param(&self, key: impl AsRef<[u8]>) -> Result<Option<&[u8]>, Error>
The URL-decoded value of a query parameter, or Ok(None) when it is
absent.
A query that is empty or "0" has no parameters. Dots and spaces in
keys read as underscores (a.b is a_b), and a key written with
brackets (a[b]=c) holds a list or map, so reading it fails with
Error::Type.
PHP: getParam($key), where Ok(None) stands for PHP returning the
default. Keys follow PHP’s parse_str.
use utopia_dsn::Dsn;
let dsn = Dsn::parse("sms://key:secret@api.twilio.com?from=%2B15550100")?;
assert_eq!(dsn.param("from")?, Some(&b"+15550100"[..]));
assert_eq!(dsn.param("region")?, None);use Utopia\DSN\DSN;
$dsn = new DSN('sms://key:secret@api.twilio.com?from=%2B15550100');
echo $dsn->getParam('from'), "\n"; // +15550100
var_dump($dsn->getParam('region')); // string(0) ""Sourcepub fn param_or<'a>(
&'a self,
key: impl AsRef<[u8]>,
default: &'a [u8],
) -> Result<&'a [u8], Error>
pub fn param_or<'a>( &'a self, key: impl AsRef<[u8]>, default: &'a [u8], ) -> Result<&'a [u8], Error>
The parameter, or default when it is absent.
PHP: getParam($key, $default).
use utopia_dsn::Dsn;
let dsn = Dsn::parse("s3://key:secret@s3.amazonaws.com/backups")?;
assert_eq!(dsn.param_or("region", b"us-east-1")?, b"us-east-1");use Utopia\DSN\DSN;
$dsn = new DSN('s3://key:secret@s3.amazonaws.com/backups');
echo $dsn->getParam('region', 'us-east-1'), "\n"; // us-east-1Trait Implementations§
Source§impl Display for Dsn
impl Display for Dsn
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
The DSN, with each part encoded as Dsn::from encodes it, and the
query as it was given.
PHP: __toString().
use utopia_dsn::Dsn;
let dsn = Dsn::parse("mariadb://user:p%40ss@localhost:3306/data%20base?charset=utf8")?;
assert_eq!(dsn.to_string(), "mariadb://user:p%40ss@localhost:3306/data%20base?charset=utf8");use Utopia\DSN\DSN;
echo new DSN('mariadb://user:p%40ss@localhost:3306/data%20base?charset=utf8'), "\n";
// mariadb://user:p%40ss@localhost:3306/data%20base?charset=utf8