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
use crate::scheme::RedirectScheme;

#[derive(Clone, Default)]
pub struct RedirectSchemeBuilder {
    // Redirect to HTTP (true: HTTP -> HTTPS, false: HTTPS -> HTTP)
    https_to_http: bool,
    // Temporary redirect (true: 307 Temporary Redirect, false: 301 Moved Permanently)
    temporary: bool,
    // List of string replacements
    replacements: Vec<(String, String)>,
}

impl RedirectSchemeBuilder {
    /// Create new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set redirection to HTTPS flag
    pub fn http_to_https(&mut self, value: bool) -> &mut Self {
        let mut new = self;
        new.https_to_http = !value;
        new
    }

    /// Set redirection to HTTP
    pub fn https_to_http(&mut self) -> &mut Self {
        let mut new = self;
        new.https_to_http = true;
        new
    }

    /// Set answer code for permanent redirection
    pub fn permanent(&mut self, value: bool) -> &mut Self {
        let mut new = self;
        new.temporary = !value;
        new
    }

    /// Set answer code for temporary redirection
    pub fn temporary(&mut self) -> &mut Self {
        let mut new = self;
        new.temporary = true;
        new
    }

    /// Set list of replacements
    pub fn replacements(&mut self, value: &[(&str, &str)]) -> &mut Self {
        let mut new = self;
        new.replacements = value
            .iter()
            .map(|(a, b)| ((*a).into(), (*b).into()))
            .collect();
        new
    }

    /// Build RedirectScheme
    pub fn build(&self) -> RedirectScheme {
        RedirectScheme {
            https_to_http: self.https_to_http,
            temporary: self.temporary,
            replacements: self.replacements.clone(),
        }
    }
}