rocket_http/uri/fmt/
part.rs

1use crate::parse::IndexedStr;
2
3/// Marker trait for types that mark a part of a URI.
4///
5/// This trait exists solely to categorize types that mark a part of the URI,
6/// currently [`Path`] and [`Query`]. Said another way, types that implement
7/// this trait are marker types that represent a part of a URI at the
8/// type-level.
9///
10/// This trait is _sealed_: it cannot be implemented outside of Rocket.
11///
12/// # Usage
13///
14/// You will find this trait in traits like [`UriDisplay`] or structs like
15/// [`Formatter`] as the bound on a generic parameter: `P: Part`. Because the
16/// trait is sealed, the generic type is guaranteed to be instantiated as one of
17/// [`Query`] or [`Path`], effectively creating two instances of the generic
18/// items: `UriDisplay<Query>` and `UriDisplay<Path>`, and `Formatter<Query>`
19/// and `Formatter<Path>`. Unlike having two distinct, non-generic traits, this
20/// approach enables succinct, type-checked generic implementations of these
21/// items.
22///
23/// [`UriDisplay`]: crate::uri::fmt::UriDisplay
24/// [`Formatter`]: crate::uri::fmt::Formatter
25pub trait Part: private::Sealed {
26    /// The dynamic version of `Self`.
27    #[doc(hidden)]
28    const KIND: Kind;
29
30    /// The delimiter used to separate components of this URI part.
31    /// Specifically, `/` for `Path` and `&` for `Query`.
32    #[doc(hidden)]
33    const DELIMITER: char;
34
35    /// The raw form of a segment in this part.
36    #[doc(hidden)]
37    type Raw: Send + Sync + 'static;
38}
39
40mod private {
41    pub trait Sealed {}
42    impl Sealed for super::Path {}
43    impl Sealed for super::Query {}
44}
45
46/// Dynamic version of the `Path` and `Query` parts.
47#[doc(hidden)]
48#[derive(Debug, PartialEq, Eq, Copy, Clone)]
49pub enum Kind { Path, Query }
50
51/// Marker type indicating use of a type for the path [`Part`] of a URI.
52///
53/// In route URIs, this corresponds to all of the text before a `?`, if any, or
54/// all of the text in the URI otherwise:
55///
56/// ```text
57/// #[get("/home/<name>/<page>?<item>")]
58///        ^------------------ Path
59/// ```
60#[derive(Debug, Clone, Copy)]
61pub enum Path {  }
62
63/// Marker type indicating use of a type for the query [`Part`] of a URI.
64///
65/// In route URIs, this corresponds to all of the text after a `?`, if any.
66///
67/// ```text
68/// #[get("/home/<name>/<page>?<item>&<form..>")]
69///                            ^-------------- Query
70/// ```
71#[derive(Debug, Clone, Copy)]
72pub enum Query {  }
73
74impl Part for Path {
75    const KIND: Kind = Kind::Path;
76    const DELIMITER: char = '/';
77    type Raw = IndexedStr<'static>;
78}
79
80impl Part for Query {
81    const KIND: Kind = Kind::Query;
82    const DELIMITER: char = '&';
83    type Raw = (IndexedStr<'static>, IndexedStr<'static>);
84}