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
//! Defines [`Url`] and [`UrlBuf`] types which are analogous to the [`str`] and
//! [`String`] or [`std::path::Path`] and [`std::path::PathBuf`] pairs. These are
//! effectively newtypes for [`str`] and [`String`].

use gtmpl::Value;
use serde::{Deserialize, Deserializer};
use std::ops::Deref;

/// A newtype for URL strings, analogous to [`str`].
pub struct Url(str);

impl Url {
    /// Construct a new `&Url` from any [`AsRef<str>`].
    #[inline]
    pub fn new<S: AsRef<str> + ?Sized>(url: &S) -> &Url {
        unsafe { &*(url.as_ref().trim_end_matches('/') as *const str as *const Url) }
    }

    /// Join any [`AsRef<str>`] to the `&Url`.
    #[inline]
    pub fn join<S: AsRef<str>>(&self, rhs: S) -> UrlBuf {
        let mut buf = self.to_url_buf();
        buf.0.push('/');
        buf.0.push_str(rhs.as_ref());
        buf
    }

    /// Create a [`UrlBuf`] from the current `&Url`.
    #[inline]
    pub fn to_url_buf(&self) -> UrlBuf {
        UrlBuf::from(self.0.to_string())
    }

    /// Return the [`str`] representation of the current `&Url`.
    #[inline]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl ToOwned for Url {
    type Owned = UrlBuf;
    /// Returns the owned [`UrlBuf`] corresponding to the current `&Url`.
    #[inline]
    fn to_owned(&self) -> UrlBuf {
        UrlBuf(self.0.to_owned())
    }
}

impl AsRef<str> for Url {
    #[inline]
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<&Url> for String {
    fn from(url: &Url) -> String {
        url.0.into()
    }
}

impl std::fmt::Display for Url {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", &self.0)
    }
}

/// A newtype for URL strings, analogous to [`String`].
#[derive(Clone, Debug)]
pub struct UrlBuf(String);

impl UrlBuf {
    /// Constructs a new [`UrlBuf`]; corresponds to [`String::new`].
    #[inline]
    pub fn new() -> UrlBuf {
        UrlBuf(String::new())
    }

    /// Consumes the current [`UrlBuf`] and returns the [`String`]
    /// representation.
    #[inline]
    pub fn into_string(self) -> String {
        self.0
    }
}

impl Default for UrlBuf {
    /// Returns a default [`UrlBuf`]. This corresponds to [`String::default`].
    #[inline]
    fn default() -> UrlBuf {
        UrlBuf(String::default())
    }
}

impl std::fmt::Display for UrlBuf {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let url: &Url = self;
        url.fmt(f)
    }
}

impl From<UrlBuf> for String {
    #[inline]
    fn from(url_buf: UrlBuf) -> String {
        url_buf.0
    }
}

impl From<String> for UrlBuf {
    #[inline]
    fn from(s: String) -> UrlBuf {
        UrlBuf(s)
    }
}

impl From<&str> for UrlBuf {
    #[inline]
    fn from(s: &str) -> UrlBuf {
        UrlBuf(s.to_owned())
    }
}

impl AsRef<str> for UrlBuf {
    #[inline]
    fn as_ref(&self) -> &str {
        let url: &Url = self;
        url.as_ref()
    }
}

impl Deref for UrlBuf {
    type Target = Url;
    #[inline]
    fn deref(&self) -> &Url {
        Url::new(&self.0)
    }
}

impl std::borrow::Borrow<Url> for UrlBuf {
    #[inline]
    fn borrow(&self) -> &Url {
        self.deref()
    }
}

impl AsRef<Url> for UrlBuf {
    #[inline]
    fn as_ref(&self) -> &Url {
        self
    }
}

impl<'de> Deserialize<'de> for UrlBuf {
    fn deserialize<D>(d: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(UrlBuf(
            String::deserialize(d)?.trim_end_matches('/').to_owned(),
        ))
    }
}

impl From<&Url> for Value {
    fn from(url: &Url) -> Value {
        Value::String(url.to_string())
    }
}

impl From<UrlBuf> for Value {
    fn from(url: UrlBuf) -> Value {
        Value::from(&url)
    }
}

impl From<&UrlBuf> for Value {
    fn from(url: &UrlBuf) -> Value {
        let url: &Url = url;
        Value::from(url)
    }
}