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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
extern crate indexmap;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;

use indexmap::IndexMap;
use serde::de;
use serde::de::{value, Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, SerializeSeq, Serializer};
use std::collections::{HashMap, HashSet};
use std::fmt;

/// Config represents the global configuration within a netlify.toml file.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct Config {
    pub build: Option<Build>,
    pub context: Option<HashMap<String, Context>>,
    pub redirects: Option<Vec<Redirect>>,
    pub headers: Option<Vec<Header>>,
    pub template: Option<Template>,
}

/// Context combines different settings grouped in a deploy context.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct Context {
    pub redirects: Option<Vec<Redirect>>,
    pub headers: Option<Vec<Header>>,
    #[serde(flatten)]
    pub build: Build,
}

/// Build holds the build variables Netlify uses to build a site before deploying it.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct Build {
    pub base: Option<String>,
    pub publish: Option<String>,
    pub command: Option<String>,
    pub functions: Option<String>,
    pub environment: Option<HashMap<String, String>>,
    #[serde(alias = "edge-handlers")]
    pub edge_handlers: Option<String>,
}

/// Redirect holds information about a url redirect.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Redirect {
    #[serde(alias = "origin")]
    pub from: String,
    #[serde(alias = "destination")]
    pub to: Option<String>,
    #[serde(default = "default_status")]
    pub status: u32,
    #[serde(default)]
    pub force: bool,
    pub headers: Option<HashMap<String, String>>,
    #[serde(alias = "params")]
    #[serde(alias = "parameters")]
    pub query: Option<HashMap<String, String>>,
    pub conditions: Option<HashMap<String, HashSet<String>>>,
    pub signed: Option<String>,
    #[serde(alias = "edge-handler")]
    pub edge_handler: Option<String>,
}

/// Header holds information to add response headers for a give url.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct Header {
    #[serde(rename = "for")]
    pub path: String,
    #[serde(rename = "values")]
    pub headers: HashMap<String, HeaderValues>,
}

#[derive(Clone, Debug, PartialEq, Default)]
pub struct HeaderValues {
    pub values: Vec<String>,
}

/// Template holds information to turn a repository into a Netlify template.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct Template {
    #[serde(rename = "incoming-hooks")]
    pub hooks: Option<Vec<String>>,
    pub environment: Option<HashMap<String, String>>,
}

/// Base crate error type
pub type Error = toml::de::Error;

/// Parses the contents of a netlify.toml file as a Config structure.
///
/// # Arguments
///
/// `io` - A string slice that holds the content of a toml file.
///
/// # Example
///
/// ```
/// let io = r#"
/// [build]
///   command = "make site"
/// "#;
///
/// let result = netlify_toml::from_str(io);
/// ```
pub fn from_str(io: &str) -> Result<Config, Error> {
    toml::from_str::<Config>(io)
}

impl Config {
    /// Return a HashMap that aggregates all environment variables for
    /// a context within a git branch.
    ///
    /// # Arguments
    ///
    /// `ctx` - The context name, for example `deploy-preview`, `branch-deploy` or `production`.
    /// `branch` - The deploy branch name, for example `make-changes-to-my-site`.
    ///
    /// # Example
    ///
    /// ```
    /// let io = r#"
    /// [build]
    ///   command = "make site"
    /// "#;
    ///
    /// let config = netlify_toml::from_str(io).unwrap();
    /// let env = config.scoped_env("deploy-preview", "new-styles");
    /// ```
    pub fn scoped_env(self, ctx: &str, branch: &str) -> HashMap<String, String> {
        let mut result = HashMap::new();

        // Read the env variables from the global "build" context.
        if let Some(env) = self.build.and_then(|b| b.environment) {
            result.extend(env);
        }

        let context = match &self.context {
            Some(c) => c,
            None => return result,
        };

        context
            .get(ctx)
            .and_then(|x| x.build.environment.clone())
            .and_then(|env| Some(result.extend(env)));

        context
            .get(branch)
            .and_then(|x| x.build.environment.clone())
            .and_then(|env| Some(result.extend(env)));

        result
    }

    /// Return a list of aggregated redirects for a context within a branch.
    ///
    /// If a context includes a redirect that's defined by the global list, the redirect
    /// is replaced in the global list to preserve this ordering. This match is based on the exact
    /// comparison of the origin value.
    ///
    /// If a context includes a redirect that's not defined by the global list, the redirect
    /// is appended before any other redirect in the global list to give it a higher precedence.
    ///
    /// # Arguments
    ///
    /// `ctx` - The context name, for example `deploy-preview`, `branch-deploy` or `production`.
    /// `branch` - The deploy branch name, for example `make-changes-to-my-site`.
    ///
    /// # Example
    ///
    /// ```
    /// let io = r#"
    /// [[redirects]]
    ///   from = "/api/*"
    ///   to = "https://production.api.com/:splat"
    ///
    /// [[context.deploy-preview.redirects]]
    ///   from = "/api/*"
    ///   to = "https://staging.api.com/:splat"
    /// "#;
    ///
    /// let config = netlify_toml::from_str(io).unwrap();
    /// let redirects = config.scoped_redirects("deploy-preview", "new-styles").expect("missing redirects");
    /// assert_eq!(1, redirects.len());
    /// let dest = redirects.first().and_then(|r| r.to.as_ref()).expect("missing destination");
    /// assert_eq!("https://staging.api.com/:splat", dest.as_str());
    /// ```
    pub fn scoped_redirects(&self, ctx: &str, branch: &str) -> Option<Vec<Redirect>> {
        let context = match &self.context {
            Some(c) => c,
            None => return self.redirects.clone(),
        };

        let mut global_index = IndexMap::new();
        if let Some(global) = &self.redirects {
            for r in global {
                global_index.insert(r.from.clone(), r.clone());
            }
        }

        if let Some(ct) = context.get(ctx).and_then(|x| x.redirects.as_ref()) {
            for r in ct {
                if global_index.contains_key(&r.from) {
                    global_index.insert(r.from.clone(), r.clone());
                } else {
                    let mut swap = IndexMap::<String, Redirect>::new();
                    swap.insert(r.from.clone(), r.clone());
                    swap.extend(global_index);
                    global_index = swap;
                }
            }
        };

        if let Some(ct) = context.get(branch).and_then(|x| x.redirects.as_ref()) {
            for r in ct {
                if global_index.contains_key(&r.from) {
                    global_index.insert(r.from.clone(), r.clone());
                } else {
                    let mut swap = IndexMap::<String, Redirect>::new();
                    swap.insert(r.from.clone(), r.clone());
                    swap.extend(global_index);
                    global_index = swap;
                }
            }
        };

        if !global_index.is_empty() {
            Some(global_index.values().map(|r| r.clone()).collect())
        } else {
            None
        }
    }

    /// Return a list of aggregated header rules for a context within a branch.
    ///
    /// If a context includes a header rule that's defined by the global list, the header rule
    /// is replaced in the global list to preserve this ordering. This match is based on the exact
    /// comparison of the origin value.
    ///
    /// If a context includes a header rule that's not defined by the global list, the header rule
    /// is appended before any other header rules in the global list to give it a higher precedence.
    ///
    /// # Arguments
    ///
    /// `ctx` - The context name, for example `deploy-preview`, `branch-deploy` or `production`.
    /// `branch` - The deploy branch name, for example `make-changes-to-my-site`.
    ///
    /// # Example
    ///
    /// ```
    ///     let io = r#"
    ///     [[headers]]
    ///     for = "/foo"
    ///     values = {X-Foo = "Bar, Baz, Qux"}
    ///
    ///     [[headers]]
    ///     for = "/bar"
    ///     values = {X-Foo = "Bar, Baz, Qux"}
    ///
    ///     [[context.deploy-preview.headers]]
    ///     for = "/foo"
    ///     values = {X-BAR = "QUUX"}
    /// "#;
    ///
    ///     let config = netlify_toml::from_str(&io).unwrap();
    ///     let headers = config
    ///         .scoped_headers("deploy-preview", "new-styles")
    ///         .expect("missing headers");
    ///     assert_eq!(2, headers.len());
    ///
    ///     let header = headers.first().unwrap();
    ///     assert_eq!("/foo", header.path);
    ///     assert!(header.headers.contains_key("X-BAR"));
    ///     assert!(!header.headers.contains_key("X-Foo"));
    /// ```
    pub fn scoped_headers(&self, ctx: &str, branch: &str) -> Option<Vec<Header>> {
        let context = match &self.context {
            Some(c) => c,
            None => return self.headers.clone(),
        };

        let mut global_index = IndexMap::new();
        if let Some(global) = &self.headers {
            for r in global {
                global_index.insert(r.path.clone(), r.clone());
            }
        }

        if let Some(ct) = context.get(ctx).and_then(|x| x.headers.as_ref()) {
            for r in ct {
                if global_index.contains_key(&r.path) {
                    global_index.insert(r.path.clone(), r.clone());
                } else {
                    let mut swap = IndexMap::<String, Header>::new();
                    swap.insert(r.path.clone(), r.clone());
                    swap.extend(global_index);
                    global_index = swap;
                }
            }
        };

        if let Some(ct) = context.get(branch).and_then(|x| x.headers.as_ref()) {
            for r in ct {
                if global_index.contains_key(&r.path) {
                    global_index.insert(r.path.clone(), r.clone());
                } else {
                    let mut swap = IndexMap::<String, Header>::new();
                    swap.insert(r.path.clone(), r.clone());
                    swap.extend(global_index);
                    global_index = swap;
                }
            }
        };

        if !global_index.is_empty() {
            Some(global_index.values().map(|r| r.clone()).collect())
        } else {
            None
        }
    }
}

// This is the trait that informs Serde how to deserialize HeaderValues.
impl<'de> Deserialize<'de> for HeaderValues {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct HeaderValuesVisitor;
        impl<'de> Visitor<'de> for HeaderValuesVisitor {
            type Value = HeaderValues;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("string or vector")
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                if v.contains(',') {
                    let values = v.split(',').map(|s| String::from(s.trim())).collect();
                    return Ok(HeaderValues { values });
                }

                Ok(HeaderValues {
                    values: vec![v.to_owned()],
                })
            }

            fn visit_seq<V>(self, v: V) -> Result<Self::Value, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let items = Deserialize::deserialize(value::SeqAccessDeserializer::new(v))?;
                Ok(HeaderValues { values: items })
            }
        }
        // Instantiate our Visitor and ask the Deserializer to drive
        // it over the input data, resulting in an instance of MyMap.
        deserializer.deserialize_any(HeaderValuesVisitor {})
    }
}

// This is the trait that informs Serde how to serialize HeaderValues.
impl Serialize for HeaderValues {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if self.values.len() > 1 {
            let mut seq = serializer.serialize_seq(Some(self.values.len()))?;
            for e in self.values.to_owned() {
                seq.serialize_element(&e)?;
            }
            seq.end()
        } else {
            serializer.serialize_str(&self.values[0])
        }
    }
}

fn default_status() -> u32 {
    301
}

impl Default for Redirect {
    fn default() -> Redirect {
        Redirect {
            from: String::new(),
            to: None,
            status: default_status(),
            force: false,
            signed: None,
            conditions: None,
            query: None,
            headers: None,
            edge_handler: None,
        }
    }
}

impl fmt::Display for Redirect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let string = toml::ser::to_string_pretty(&self);
        write!(f, "{:?}", string)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_partial_equal() {
        let r = Redirect {
            from: "/foo".to_string(),
            to: Some("/bar".to_string()),
            status: 301,
            force: false,
            headers: None,
            query: None,
            conditions: None,
            signed: None,
            edge_handler: None,
        };

        let r2 = Redirect {
            from: "/foo".to_string(),
            to: Some("/bar".to_string()),
            status: 301,
            force: false,
            headers: None,
            query: None,
            conditions: None,
            signed: None,
            edge_handler: None,
        };
        assert_eq!(r, r2)
    }

    #[test]
    fn test_default_redirect() {
        let r = Redirect {
            from: "/foo".to_string(),
            to: Some("/bar".to_string()),
            ..Default::default()
        };
        assert_eq!("/foo", r.from);
        assert_eq!(Some("/bar".to_string()), r.to);
        assert_eq!(301, r.status);
    }
}