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
use crate::{display::UriRefDisplay, Authority, ParseUriError, Resource, Scheme, UriRef};
use shared_bytes::SharedStr;
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct Uri {
    pub scheme: Scheme,
    pub authority: Option<Authority>,
    pub resource: Resource,
    #[doc(hidden)]
    pub __private: (),
}

impl Uri {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn parse(s: SharedStr) -> Result<Self, ParseUriError> {
        crate::parse::parse_uri(s)
    }
}

impl TryFrom<UriRef> for Uri {
    type Error = UriRef;

    fn try_from(uri_ref: UriRef) -> Result<Self, Self::Error> {
        match uri_ref.scheme {
            Some(scheme) => Ok(Self {
                scheme,
                authority: uri_ref.authority,
                resource: uri_ref.resource,
                __private: (),
            }),
            None => Err(uri_ref),
        }
    }
}

impl fmt::Display for Uri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", UriRefDisplay::from(self))
    }
}