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
// Rust Standard Library Imports.
use std::fmt::{self, Display};

// Module declarations.
pub mod constants;
pub mod util;

/// Re-exports.
pub use util::command_prelude;
pub use util::errors::{Error, Result};

/// Primary structure used to generate imgix URLs.
///
/// An imgix URL is comprised of four components:
///
/// * scheme - the scheme being used (https by default).
/// * domain - the imgix domain, i.e. example.imgix.net.
/// * path - the path to the image file.
/// * query - the query string constructed from `params`.
///
/// ```text
///             domain
///         ┌──────┴──────┐
/// https://www.example.com/image/path.png/?w=320&h=640
/// └─┬─┘                  └──────┬───────┘ └────┬────┘
/// scheme                      path           params
/// ```
pub struct Url {
    /// The scheme component of a URL, i.e. https, http, etc.
    scheme: Scheme,
    /// The imgix domain, i.e. example.imgix.net
    domain: String,
    /// The imgix library generating the `Url`.
    lib: String,
    /// The path to the image file.
    path: Option<String>,
    /// The parameters used to construct the query string.
    ///
    /// This structure is a _key-value list_ and been chosen over HashMap,
    /// BTreeMap, and BTreeSet for the following reasons:
    ///
    /// * to give users __flexibility__, by accepting a range of inputs
    /// * to seek __consistency__, by parameters are ordered
    ///   as they are defined, WYSIWYG
    /// * to give users control
    ///
    /// The query-string is built up during a single iterative pass over this
    /// key-value list, visiting each key-value pair in the order the user
    /// has specified. Therefore, the order in which parameters are listed
    /// is the same order they will appear in the generated `Url`'s query
    /// string.
    params: Vec<(&'static str, &'static str)>,
    /// Optional signing token used to sign URLs.
    token: Option<String>,
}

impl Default for Url {
    /// By default a imgix URL is created with its `scheme` set
    /// to `Scheme::Https` and the `lib` value set to the version
    /// specified in this library's Cargo.toml
    fn default() -> Self {
        Url {
            scheme: Scheme::Https,
            domain: String::new(),
            lib: constants::lib_version(),
            params: vec![],
            path: None,
            token: None,
        }
    }
}

impl Url {
    /// Constructs a new `Url` given a domain.
    ///
    /// # Panics
    ///
    /// This constructor will fail if the `domain` is an empty string.
    pub fn new(domain: &'static str) -> Self {
        assert!(!domain.is_empty());
        Url {
            domain: String::from(domain),
            ..Default::default()
        }
    }

    /// Sets the domain value (i.e. "example.domain.com").
    ///
    /// # Panics
    ///
    /// This method panics if the domain `h` is an empty string.
    pub fn domain(mut self, h: &str) -> Self {
        assert!(!h.is_empty());
        self.domain = String::from(h);
        self
    }

    /// Sets the path value to the image file (i.e. 'image/path.png').
    ///
    /// # Panics
    ///
    /// This method panics if the path string `r` is empty (i.e. "").
    pub fn path(mut self, r: &str) -> Self {
        assert!(!r.is_empty());
        self.path = Some(String::from(r));
        self
    }

    /// Sets an arbitrary key-value parameter (i.e. k='w', v='100'
    /// or k='fit', v='crop').
    ///
    /// # Panics
    ///
    /// This method panics if the key `k` or the value `v` is an empty string.
    pub fn param(mut self, k: &'static str, v: &'static str) -> Self {
        assert!(!k.is_empty());
        assert!(!v.is_empty());
        self.params.push((k, v));
        self
    }

    /// Sets an arbitrary number of key-value parameters.
    ///
    /// # Panics
    ///
    /// This method panics if any key `k` or any value `v` is an empty string.
    pub fn params(mut self, p: &[(&'static str, &'static str)]) -> Self {
        for (k, v) in p.iter() {
            assert!(!k.is_empty());
            assert!(!v.is_empty());
            self.params.push((k, v));
        }
        self
    }

    /// Sets the `lib` or library.
    pub fn lib(mut self, l: &str) -> Self {
        self.lib = String::from(l);
        self
    }

    /// Sets the signing `token`.
    pub fn token(mut self, token: &str) -> Self {
        self.token = Some(String::from(token));
        self
    }

    // Sets the `scheme` value (i.e. Scheme::Https).
    pub fn scheme(mut self, s: Scheme) -> Self {
        self.scheme = s;
        self
    }

    /// Joins the components of a `Url` (i.e. `scheme` + `domain` + `path` +
    /// `params`) where the resulting string has the following form:
    ///
    /// {scheme}://{domain}/{path}?{lib}{query}
    ///
    /// This function will only `join` the components of a `Url` if a `path`
    /// has been specified.
    ///
    /// # Panics
    ///
    /// This function will panic if the image `path` has not been specified.
    /// (i.e. if the `path` is `None`).
    pub fn join(&self) -> String {
        // Join this url, only-if a `path` has been specified.
        match self.path {
            Some(ref path) => {
                let query = Self::join_params(&self.params);

                format!(
                    "{scheme}://{domain}/{path}?{lib}{query}",
                    scheme = self.scheme,
                    domain = self.domain,
                    path = path,
                    lib = self.lib,
                    query = query,
                )
            }
            None => {
                panic!("failed: cannot `Url::join` when `path` is `None`.");
            }
        }
    }

    /// Joins a list of key-value parameter pairs.
    ///
    /// # Examples
    ///
    /// # Panics
    ///
    /// This function panics if any key `k` or any value `v` is an empty string.
    pub fn join_params(p: &[(&'static str, &'static str)]) -> String {
        let mut result = String::new();

        // I the parameter list is empty, do no work.
        if p.is_empty() {
            return result;
        }

        // Otherwise, construct the result by appending parameters one after another
        // (i.e. {key}={value}{"&" | ""}).
        // The result has the form: k0=v0&k1=v1&k2=v2
        let mut it = 1usize;
        let end = p.len();
        for (k, v) in p.iter() {
            assert!(!k.is_empty());
            assert!(!v.is_empty());
            result.push_str(k);
            result.push('=');
            result.push_str(v);

            // Avoid pushing a trailing '&' if there are no more parameter pairs.
            if it < end {
                result.push('&');
            }
            it += 1;
        }
        return result;
    }
}

#[derive(Debug, PartialEq)]
pub enum Scheme {
    Https,
    Http,
}

impl Display for Scheme {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Scheme::Https => write!(f, "{}", "https"),
            Scheme::Http => write!(f, "{}", "http"),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    const HTTPS: &str = "https";
    const HTTP: &str = "http";
    const HOST: &str = "test.domain.com";
    const PNG_PATH: &str = "test-image.png";

    #[test]
    fn test_join_params() {
        let left = Url::join_params(&[("w", "300")]);
        assert_eq!(left, String::from("w=300"));

        let left = Url::join_params(&[("w", "300"), ("h", "600")]);
        assert_eq!(left, String::from("w=300&h=600"));

        let left = Url::join_params(&[("w", "300"), ("h", "600"), ("fit", "crop")]);
        assert_eq!(left, String::from("w=300&h=600&fit=crop"));
    }

    #[test]
    fn test_basic_imgix_url() {
        let right = format!(
            "{scheme}://{domain}/{path}?{ixlib}",
            scheme = HTTPS,
            domain = HOST,
            path = PNG_PATH,
            ixlib = constants::lib_version(),
        );
        let url = Url::new(HOST).path(PNG_PATH);

        // Test all fields.
        assert_eq!(url.scheme, Scheme::Https);
        assert_eq!(url.domain, HOST);
        assert_eq!(url.lib, constants::lib_version());
        assert_eq!(url.path, Some(String::from(PNG_PATH)));
        assert!(url.params.is_empty());
        assert!(url.token.is_none());

        // Test the joined url.
        assert_eq!(url.join(), right);
    }

    #[test]
    fn test_basic_imgix_url_scheme() {
        let right = format!(
            "{scheme}://{domain}/{path}?{ixlib}",
            scheme = HTTP,
            domain = HOST,
            path = PNG_PATH,
            ixlib = constants::lib_version()
        );

        // Construct a url with http scheme.
        // Note: https is the default scheme.
        let url = Url::new(HOST).path(PNG_PATH).scheme(Scheme::Http);

        assert_eq!(url.scheme, Scheme::Http);
        assert_eq!(url.domain, HOST);
        assert_eq!(url.lib, constants::lib_version());
        assert_eq!(url.path, Some(String::from(PNG_PATH)));
        assert!(url.params.is_empty());
        assert_eq!(url.join(), right);

        // Now switch back to https.
        let url = url.scheme(Scheme::Https);
        assert_eq!(url.scheme, Scheme::Https);
    }
}