actix_web_middleware_redirect_scheme/
builder.rs

1use crate::scheme::RedirectScheme;
2
3#[derive(Clone, Default)]
4pub struct RedirectSchemeBuilder {
5    // Disabled redirections
6    disable: bool,
7    // Redirect to HTTP (true: HTTP -> HTTPS, false: HTTPS -> HTTP)
8    https_to_http: bool,
9    // Temporary redirect (true: 307 Temporary Redirect, false: 301 Moved Permanently)
10    temporary: bool,
11    // List of string replacements
12    replacements: Vec<(String, String)>,
13}
14
15impl RedirectSchemeBuilder {
16    /// Create new builder
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Enabling or disabling of redirections
22    pub fn enable(&mut self, value: bool) -> &mut Self {
23        let mut new = self;
24        new.disable = !value;
25        new
26    }
27
28    /// Set redirection to HTTPS flag
29    pub fn http_to_https(&mut self, value: bool) -> &mut Self {
30        let mut new = self;
31        new.https_to_http = !value;
32        new
33    }
34
35    /// Set redirection to HTTP
36    pub fn https_to_http(&mut self) -> &mut Self {
37        let mut new = self;
38        new.https_to_http = true;
39        new
40    }
41
42    /// Set answer code for permanent redirection
43    pub fn permanent(&mut self, value: bool) -> &mut Self {
44        let mut new = self;
45        new.temporary = !value;
46        new
47    }
48
49    /// Set answer code for temporary redirection
50    pub fn temporary(&mut self) -> &mut Self {
51        let mut new = self;
52        new.temporary = true;
53        new
54    }
55
56    /// Set list of replacements
57    pub fn replacements<S: ToString>(&mut self, value: &[(S, S)]) -> &mut Self {
58        if !self.disable {
59            self.replacements = value
60                .iter()
61                .map(|(a, b)| ((*a).to_string(), (*b).to_string()))
62                .collect();
63        }
64        self
65    }
66
67    /// Build RedirectScheme
68    pub fn build(&self) -> RedirectScheme {
69        RedirectScheme {
70            disable: self.disable,
71            https_to_http: self.https_to_http,
72            temporary: self.temporary,
73            replacements: self.replacements.clone(),
74        }
75    }
76}