utopia-dsn 0.1.0

Data source names (connection strings), parsed into scheme, credentials, host, port, path and options.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! 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`.
//!
//! | PHP | Rust |
//! |---|---|
//! | `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`, `\TypeError` | [`Error::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"[..]));
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ```php
//! 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
//! ```

#![warn(missing_docs)]

use std::fmt;

/// The getting started guide (`guide.md`), compiled and run with the doctests.
#[cfg(doctest)]
#[doc = include_str!("../guide.md")]
pub struct GettingStarted;
use std::ops::Range;
use std::sync::OnceLock;

use php_std::encoding::{parse_str, rawurldecode_into, rawurlencode, urldecode_into};
use php_std::{Array, ArrayKey, Value};

/// Why a DSN was refused or a parameter could not be read.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
    /// The DSN is malformed or lacks a scheme or host.
    ///
    /// PHP: `\InvalidArgumentException`.
    ///
    /// ```
    /// use utopia_dsn::{Dsn, Error};
    ///
    /// let error = Dsn::parse("localhost:3306").unwrap_err();
    /// assert!(matches!(error, Error::InvalidArgument(_)));
    /// assert_eq!(error.to_string(), "Unable to parse DSN: scheme is required");
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// try {
    ///     new DSN('localhost:3306');
    /// } catch (\InvalidArgumentException $e) {
    ///     echo $e->getMessage(), "\n"; // Unable to parse DSN: scheme is required
    /// }
    /// ```
    #[error("{0}")]
    InvalidArgument(&'static str),
    /// [`Dsn::param`] was called on a key the query holds as a list or map
    /// (`a[b]=c`), which has no single value to return.
    ///
    /// PHP: `\TypeError`, as PHP cannot return an array as a string.
    #[error("{0}")]
    Type(&'static str),
}

impl Error {
    /// The name of the exception class this error matches, such as
    /// `InvalidArgumentException`.
    ///
    /// PHP: the class of the exception `utopia-php/dsn` throws for the same
    /// input.
    ///
    /// ```
    /// use utopia_dsn::Dsn;
    ///
    /// let error = Dsn::parse("redis://").unwrap_err();
    /// assert_eq!(error.php_class(), "InvalidArgumentException");
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// try {
    ///     new DSN('redis://');
    /// } catch (\Throwable $e) {
    ///     echo get_class($e), "\n"; // InvalidArgumentException
    /// }
    /// ```
    pub fn php_class(&self) -> &'static str {
        match self {
            Error::InvalidArgument(_) => "InvalidArgumentException",
            Error::Type(_) => "TypeError",
        }
    }
}

/// 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"));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ```php
/// use Utopia\DSN\DSN;
///
/// $dsn = new DSN('redis://default:secret@cache:6379');
/// echo $dsn->getHost(), "\n"; // cache
/// ```
#[derive(Clone)]
pub struct Dsn {
    /// Every component, decoded, in one allocation; the ranges index it.
    /// The scheme, host and query are copied from the `&str` input whole, so
    /// they are UTF-8.
    buffer: Box<[u8]>,
    scheme: Range<usize>,
    user: Option<Range<usize>>,
    password: Option<Range<usize>>,
    host: Range<usize>,
    port: Option<u16>,
    path: Range<usize>,
    query: Option<Range<usize>>,
    /// The query parsed by `parse_str`, on first use.
    params: OnceLock<Array>,
}

impl Dsn {
    /// 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());
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// $dsn = new DSN('redis://cache:6379');
    /// echo $dsn->getHost(), "\n"; // cache
    /// ```
    pub fn parse(dsn: &str) -> Result<Self, Error> {
        let url =
            php_std::url::parse_url(dsn.as_bytes()).ok_or(Error::InvalidArgument("Unable to parse DSN: malformed"))?;
        let scheme = url.scheme().filter(|s| !blank(s));
        let host = url.host().filter(|h| !blank(h));
        let (Some(scheme), Some(host)) = (scheme, host) else {
            return Err(Error::InvalidArgument(if url.scheme().is_none_or(|s| blank(&s)) {
                "Unable to parse DSN: scheme is required"
            } else {
                "Unable to parse DSN: host is required"
            }));
        };
        // Decoding never lengthens a component, so one allocation holds them all.
        let mut buffer = Vec::with_capacity(dsn.len());
        let scheme = append(&mut buffer, |b| b.extend_from_slice(&scheme));
        let user = url.user().map(|u| append(&mut buffer, |b| urldecode_into(b, &u)));
        let password = url.pass().map(|p| append(&mut buffer, |b| urldecode_into(b, &p)));
        let host = append(&mut buffer, |b| b.extend_from_slice(&host));
        let path = append(&mut buffer, |b| {
            if let Some(p) = url.path() {
                rawurldecode_into(b, php_std::string::ltrim(&p, b"/"));
            }
        });
        let query = url.query().map(|q| append(&mut buffer, |b| b.extend_from_slice(&q)));
        Ok(Self {
            buffer: buffer.into_boxed_slice(),
            scheme,
            user,
            password,
            host,
            port: url.port(),
            path,
            query,
            params: OnceLock::new(),
        })
    }

    /// 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());
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// 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
    /// ```
    pub fn from(
        scheme: &str,
        host: &str,
        user: Option<&[u8]>,
        password: Option<&[u8]>,
        port: Option<i64>,
        path: &[u8],
        params: &[(&str, &str)],
    ) -> Result<Self, Error> {
        if user.is_none() && password.is_some() {
            return Err(Error::InvalidArgument("Unable to build DSN: a password needs a user"));
        }
        let mut wanted = Array::new();
        for (key, value) in params {
            wanted.set(ArrayKey::from(*key), Value::from(*value));
        }

        let mut dsn = format!("{scheme}://");
        write_credentials(&mut dsn, user, password);
        dsn.push_str(host);
        if let Some(port) = port {
            dsn.push(':');
            dsn.push_str(&port.to_string());
        }
        write_path(&mut dsn, path);
        if !wanted.is_empty() {
            // http_build_query($params, '', '&', PHP_QUERY_RFC3986) of string values.
            dsn.push('?');
            for (index, (key, value)) in wanted.iter().enumerate() {
                if index > 0 {
                    dsn.push('&');
                }
                let key = match key {
                    php_std::types::KeyRef::Int(i) => i.to_string(),
                    php_std::types::KeyRef::Str(s) => rawurlencode(s),
                };
                dsn.push_str(&key);
                dsn.push('=');
                if let Value::Str(value) = value {
                    dsn.push_str(&rawurlencode(value));
                }
            }
        }

        let parsed = Self::parse(&dsn)?;
        drop(dsn);

        // getParam() reads the query with parse_str, which nests `a[b]` and
        // rewrites `.` and spaces in keys.
        let query = parse_str(parsed.query().unwrap_or_default().as_bytes());
        if parsed.scheme() != scheme
            || parsed.host() != host
            || parsed.user() != user
            || parsed.password() != password
            || parsed.port.map(i64::from) != port
            || parsed.path() != path
            || !Value::Array(query).strict_eq(&Value::Array(wanted))
        {
            return Err(Error::InvalidArgument("Unable to build DSN: a part does not survive encoding"));
        }
        Ok(parsed)
    }

    /// The scheme: the part before `://`.
    ///
    /// PHP: `getScheme()`.
    ///
    /// ```
    /// use utopia_dsn::Dsn;
    ///
    /// let dsn = Dsn::parse("postgresql://db:5432/appwrite")?;
    /// assert_eq!(dsn.scheme(), "postgresql");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// $dsn = new DSN('postgresql://db:5432/appwrite');
    /// echo $dsn->getScheme(), "\n"; // postgresql
    /// ```
    pub fn scheme(&self) -> &str {
        self.text(&self.scheme)
    }

    /// 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"[..]));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// $dsn = new DSN('smtp://mail%40example.com:secret@smtp.example.com:587');
    /// echo $dsn->getUser(), "\n"; // mail@example.com
    /// ```
    pub fn user(&self) -> Option<&[u8]> {
        self.user.clone().map(|range| &self.buffer[range])
    }

    /// 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"[..]));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// $dsn = new DSN('redis://default:p%40ss%2Fword@cache');
    /// echo $dsn->getPassword(), "\n"; // p@ss/word
    /// ```
    pub fn password(&self) -> Option<&[u8]> {
        self.password.clone().map(|range| &self.buffer[range])
    }

    /// 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");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// $dsn = new DSN('mongodb://user:secret@mongo.internal:27017/appwrite');
    /// echo $dsn->getHost(), "\n"; // mongo.internal
    /// ```
    pub fn host(&self) -> &str {
        self.text(&self.host)
    }

    /// 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);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// var_dump((new DSN('redis://cache:6379'))->getPort()); // string(4) "6379"
    /// var_dump((new DSN('redis://cache'))->getPort());      // NULL
    /// ```
    pub fn port(&self) -> Option<u16> {
        self.port
    }

    /// 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"");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// echo (new DSN('s3://key:secret@s3.amazonaws.com/backups/daily'))->getPath(), "\n"; // backups/daily
    /// ```
    pub fn path(&self) -> &[u8] {
        &self.buffer[self.path.clone()]
    }

    /// 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"));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// use Utopia\DSN\DSN;
    ///
    /// echo (new DSN('mariadb://db/appwrite?charset=utf8mb4&timeout=5'))->getQuery(), "\n"; // charset=utf8mb4&timeout=5
    /// ```
    pub fn query(&self) -> Option<&str> {
        self.query.as_ref().map(|range| self.text(range))
    }

    /// 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);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// 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) ""
    /// ```
    pub fn param(&self, key: impl AsRef<[u8]>) -> Result<Option<&[u8]>, Error> {
        let query = match self.query() {
            None | Some("" | "0") => return Ok(None),
            Some(q) => q,
        };
        let params = self.params.get_or_init(|| parse_str(query.as_bytes()));
        match params.get_ref(php_std::types::KeyRef::normalize(key.as_ref())) {
            None => Ok(None),
            Some(Value::Str(value)) => Ok(Some(value)),
            Some(_) => {
                Err(Error::Type("Utopia\\DSN\\DSN::getParam(): Return value must be of type string, array returned"))
            }
        }
    }

    /// 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");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// 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
    /// ```
    pub fn param_or<'a>(&'a self, key: impl AsRef<[u8]>, default: &'a [u8]) -> Result<&'a [u8], Error> {
        Ok(self.param(key)?.unwrap_or(default))
    }
}

/// `user[:password]@`, each part `rawurlencode`d.
fn write_credentials(dsn: &mut String, user: Option<&[u8]>, password: Option<&[u8]>) {
    if let Some(user) = user {
        dsn.push_str(&rawurlencode(user));
        if let Some(password) = password {
            dsn.push(':');
            dsn.push_str(&rawurlencode(password));
        }
        dsn.push('@');
    }
}

/// `/` and the path's segments, each `rawurlencode`d; nothing for an empty path.
fn write_path(dsn: &mut String, path: &[u8]) {
    if !path.is_empty() {
        for segment in path.split(|&b| b == b'/') {
            dsn.push('/');
            dsn.push_str(&rawurlencode(segment));
        }
    }
}

impl fmt::Display for Dsn {
    /// 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");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ```php
    /// 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
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut dsn = format!("{}://", self.scheme());
        write_credentials(&mut dsn, self.user(), self.password());
        dsn.push_str(self.host());
        if let Some(port) = self.port {
            dsn.push(':');
            dsn.push_str(&port.to_string());
        }
        write_path(&mut dsn, self.path());
        if let Some(query) = self.query() {
            dsn.push('?');
            dsn.push_str(query);
        }
        f.write_str(&dsn)
    }
}

/// The range of `buffer` that `write` appends.
fn append(buffer: &mut Vec<u8>, write: impl FnOnce(&mut Vec<u8>)) -> Range<usize> {
    let start = buffer.len();
    write(buffer);
    start..buffer.len()
}

impl Dsn {
    /// A UTF-8 component: the scheme, the host or the query.
    fn text(&self, range: &Range<usize>) -> &str {
        std::str::from_utf8(&self.buffer[range.clone()]).unwrap_or_default()
    }
}

/// PHP `empty()` on a string: `""` and `"0"`.
fn blank(s: &[u8]) -> bool {
    s.is_empty() || s == b"0"
}

impl fmt::Debug for Dsn {
    /// Formats every component except the password, which shows as
    /// `<hidden>`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Dsn")
            .field("scheme", &self.scheme())
            .field("user", &self.user().map(String::from_utf8_lossy))
            .field("password", &self.password.as_ref().map(|_| "<hidden>"))
            .field("host", &self.host())
            .field("port", &self.port)
            .field("path", &String::from_utf8_lossy(self.path()))
            .field("query", &self.query())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_like_php() {
        let d = Dsn::parse("mariadb://user:password@localhost:3306/database?charset=utf8&timezone=UTC").unwrap();
        assert_eq!(d.scheme(), "mariadb");
        assert_eq!(d.user(), Some(&b"user"[..]));
        assert_eq!(d.password(), Some(&b"password"[..]));
        assert_eq!(d.port(), Some(3306));
        assert_eq!(d.path(), b"database");
        assert_eq!(d.param("timezone").unwrap(), Some(&b"UTC"[..]));
        assert_eq!(d.param_or("region", b"us-east-1").unwrap(), b"us-east-1");
        assert_eq!(Dsn::parse("s://h?%ff=1&=2").unwrap().param(b"\xff").unwrap(), Some(&b"1"[..]));
        let d = Dsn::parse("sms://user:sl%2Fsh%2B%24%40no%3Aher@localhost").unwrap();
        assert_eq!(d.password(), Some(&b"sl/sh+$@no:her"[..]));
        assert_eq!(d.path(), b"");
        assert_eq!(d.query(), None);
    }

    #[test]
    fn refuses_without_quoting_credentials() {
        for dsn in
            ["s3://AKIAKEY:SECRETKEY@/backups", "//AKIAKEY:SECRETKEY@localhost/db", "AKIAKEY:SECRETKEY@localhost"]
        {
            let e = Dsn::parse(dsn).unwrap_err();
            let printed = format!("{e} {e:?}");
            assert!(!printed.contains("AKIAKEY") && !printed.contains("SECRETKEY"), "{printed}");
            assert_eq!(e.php_class(), "InvalidArgumentException");
        }
        let d = Dsn::parse("redis://user:SECRETKEY@host").unwrap();
        assert!(!format!("{d:?}").contains("SECRETKEY"));
    }

    #[test]
    fn array_params_are_type_errors() {
        let d = Dsn::parse("x://h?a[b]=1&c=2").unwrap();
        assert_eq!(d.param("a").unwrap_err().php_class(), "TypeError");
        assert_eq!(d.param("c").unwrap(), Some(&b"2"[..]));
        assert_eq!(Dsn::parse("x://h?0").unwrap().param("0").unwrap(), None);
    }
}