Skip to main content

Dsn

Struct Dsn 

Source
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"; // cache

Implementations§

Source§

impl Dsn

Source

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"; // cache
Source

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-1
Source

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"; // postgresql
Source

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.com
Source

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/word
Source

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.internal
Source

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());      // NULL
Source

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/daily
Source

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=5
Source

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) ""
Source

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-1

Trait Implementations§

Source§

impl Clone for Dsn

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dsn

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats every component except the password, which shows as <hidden>.

Source§

impl Display for Dsn

Source§

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

Auto Trait Implementations§

§

impl !Freeze for Dsn

§

impl Forget for Dsn

§

impl Move for Dsn

§

impl RefUnwindSafe for Dsn

§

impl Send for Dsn

§

impl Sync for Dsn

§

impl Unpin for Dsn

§

impl UnsafeUnpin for Dsn

§

impl UnwindSafe for Dsn

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.