rocket_http/uri/
asterisk.rs

1use crate::ext::IntoOwned;
2use crate::uri::Error;
3
4/// The literal `*` URI.
5///
6/// # (De)serialization
7///
8/// `Asterisk` is both `Serialize` and `Deserialize`:
9///
10/// ```rust
11/// # #[cfg(feature = "serde")] mod serde {
12/// # use serde_ as serde;
13/// use serde::{Serialize, Deserialize};
14/// use rocket::http::uri::Asterisk;
15///
16/// #[derive(Deserialize, Serialize)]
17/// # #[serde(crate = "serde_")]
18/// struct UriOwned {
19///     uri: Asterisk,
20/// }
21/// # }
22/// ```
23#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
24pub struct Asterisk;
25
26impl Asterisk {
27    /// Parses the string `string` into an `Asterisk`. Parsing will never
28    /// allocate. Returns an `Error` if `string` is not a valid asterisk URI.
29    ///
30    /// # Example
31    ///
32    /// ```rust
33    /// # #[macro_use] extern crate rocket;
34    /// use rocket::http::uri::Asterisk;
35    ///
36    /// assert!(Asterisk::parse("*").is_ok());
37    /// assert!(Asterisk::parse("/foo/bar").is_err());
38    ///
39    /// // Prefer to use `uri!()` when the input is statically known:
40    /// let uri = uri!("*");
41    /// assert_eq!(uri, Asterisk);
42    /// ```
43    pub fn parse(string: &str) -> Result<Asterisk, Error<'_>> {
44        crate::parse::uri::asterisk_from_str(string)
45    }
46
47    /// Parses the string `string` into an `Asterisk`. This is equivalent to
48    /// [`Asterisk::parse()`].
49    ///
50    /// # Example
51    ///
52    /// ```rust
53    /// # #[macro_use] extern crate rocket;
54    /// use rocket::http::uri::Asterisk;
55    ///
56    /// assert!(Asterisk::parse_owned("*".to_string()).is_ok());
57    /// assert!(Asterisk::parse_owned("/foo/bar".to_string()).is_err());
58    /// ```
59    pub fn parse_owned(string: String) -> Result<Asterisk, Error<'static>> {
60        Asterisk::parse(&string).map_err(|e| e.into_owned())
61    }
62}
63
64impl std::fmt::Display for Asterisk {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        "*".fmt(f)
67    }
68}
69
70impl_serde!(Asterisk, "an asterisk-form URI, '*'");