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
use std::fmt;

use once_cell::sync::Lazy;
use regex::Regex;
use serde::de;

use crate::{AsResult, Error, Parse};

/// The server keep-alive preference.
///
/// By default keep alive is set to 5 seconds. Takes a string value: Either "default", "disabled",
/// "os", or a string of the format "N seconds" where N is an integer > 0 e.g. "6 seconds".
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum KeepAlive {
    /// The default keep-alive as defined by Actix Web.
    Default,

    /// Disable keep-alive.
    Disabled,

    /// Let the OS determine keep-alive duration.
    ///
    /// Note: this is usually quite long.
    Os,

    /// A specific keep-alive duration (in seconds).
    Seconds(usize),
}

impl Parse for KeepAlive {
    fn parse(string: &str) -> AsResult<Self> {
        pub(crate) static FMT: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"^\d+ seconds$").expect("Failed to compile regex: FMT"));

        pub(crate) static DIGITS: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"^\d+").expect("Failed to compile regex: FMT"));

        macro_rules! invalid_value {
            ($got:expr) => {
                Err(InvalidValue! {
                    expected: "a string of the format \"N seconds\" where N is an integer > 0",
                    got: $got,
                })
            };
        }

        let digits_in = |m: regex::Match<'_>| &string[m.start()..m.end()];
        match string {
            "default" => Ok(KeepAlive::Default),
            "disabled" => Ok(KeepAlive::Disabled),
            "OS" | "os" => Ok(KeepAlive::Os),
            string if !FMT.is_match(string) => invalid_value!(string),
            string => match DIGITS.find(string) {
                None => invalid_value!(string),
                Some(mat) => match digits_in(mat).parse() {
                    Ok(val) => Ok(KeepAlive::Seconds(val)),
                    Err(_) => invalid_value!(string),
                },
            },
        }
    }
}

impl<'de> de::Deserialize<'de> for KeepAlive {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        struct KeepAliveVisitor;

        impl<'de> de::Visitor<'de> for KeepAliveVisitor {
            type Value = KeepAlive;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let msg = "Either \"default\", \"disabled\", \"os\", or a string of the format \"N seconds\" where N is an integer > 0";
                f.write_str(msg)
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                match KeepAlive::parse(value) {
                    Ok(keep_alive) => Ok(keep_alive),
                    Err(Error::InvalidValue { expected, got, .. }) => Err(
                        de::Error::invalid_value(de::Unexpected::Str(&got), &expected),
                    ),
                    Err(_) => unreachable!(),
                }
            }
        }

        deserializer.deserialize_string(KeepAliveVisitor)
    }
}