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
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter, Result as FmtRes};

pub trait Extension: Display {
    type Value: ExtensionValue;
    fn parse(&self, input: &str) -> Result<Option<Self::Value>, Error>;
}
pub trait ExtensionValue: Display + Debug + Clone {
    type Extension: Extension;
    fn extension(&self) -> &Self::Extension;
}

#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
pub enum Error {
    Incomplete,
    Invalid(usize),
}
impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtRes {
        write!(f, "Parsing SMTP extension failed. ")?;
        match self {
            Error::Incomplete => write!(f, "The input is incomplete."),
            Error::Invalid(at) => write!(f, "The input is invalid at {}.", at),
        }
    }
}

#[derive(Eq, PartialEq, Debug, Clone, Default)]
pub struct ExtensionSet {
    map: HashMap<String, String>,
}

impl ExtensionSet {
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }
    pub fn iter(&self) -> impl Iterator<Item = &str> {
        self.map.values().map(|s| s.as_str())
    }
    pub fn get<T: Extension>(&self, ext: &T) -> Result<Option<T::Value>, Error> {
        self.get_string(ext.to_string().as_str())
            .map(|s| ext.parse(s))
            .transpose()
            .map(Option::flatten)
    }
    pub fn get_string<'s>(&'s self, code: &str) -> Option<&'s str> {
        self.map.get(code).map(|s| s.as_str())
    }
    pub fn is_enabled<T: Extension>(&self, ext: &T) -> bool {
        self.is_enabled_code(ext.to_string().as_str())
    }
    pub fn is_enabled_code(&self, code: &str) -> bool {
        self.map.contains_key(code)
    }
    pub fn enable<T: ExtensionValue>(&mut self, ext: &T) -> bool {
        self.enable_string(
            ext.extension().to_string().as_str(),
            ext.to_string().as_str(),
        )
    }
    pub fn enable_string(&mut self, code: &str, ext: &str) -> bool {
        self.map.insert(code.to_string(), ext.to_string()).is_some()
    }
    pub fn disable<T: Extension>(&mut self, ext: &T) -> bool {
        self.disable_code(ext.to_string().as_str())
    }
    pub fn disable_code(&mut self, code: &str) -> bool {
        self.map.remove(code).is_some()
    }
}

#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
pub struct Flag {
    pub code: &'static str,
}
impl Extension for Flag {
    type Value = Self;
    fn parse(&self, input: &str) -> Result<Option<Self>, Error> {
        match input == self.code {
            true => Ok(Some(*self)),
            false => match self.code.starts_with(input) {
                // The input is part of the code, but too short
                true => Err(Error::Incomplete),
                false => match input.starts_with(self.code) {
                    false => Ok(None),
                    true => match &input.as_bytes()[self.code.len()..] {
                        // input starts with our code but it is a different longer word
                        [b'a'..=b'z', ..] | [b'A'..=b'Z', ..] | [b'0'..=b'9', ..] => Ok(None),
                        // this is meant to be an extension without params,
                        // but apparently there are some params
                        // or other mess
                        _ => Err(Error::Invalid(self.code.len())),
                    },
                },
            },
        }
    }
}
impl ExtensionValue for Flag {
    type Extension = Self;
    fn extension(&self) -> &Self::Extension {
        self
    }
}
impl Display for Flag {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtRes {
        f.write_str(self.code)
    }
}

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

    #[test]
    fn enable_extension() {
        let mut sut = ExtensionSet::new();
        // extension is not enabled yet so enable returns false
        assert!(!sut.enable(&STARTTLS));
        // extension is already enabled so enable returns true
        assert!(sut.enable(&STARTTLS));
    }
    #[test]
    fn disable_extension() {
        let mut sut = ExtensionSet::new();
        sut.enable(&STARTTLS);
        // extension is enabled so disable returns true
        assert!(sut.disable(&STARTTLS));
        // extension is not enabled anymore so disable returns false
        assert!(!sut.disable(&STARTTLS));
    }
    #[test]
    fn get_extension() {
        let mut sut = ExtensionSet::new();
        // extension is disabled so gives None
        assert_eq!(sut.get(&STARTTLS).unwrap(), None);
        sut.enable(&STARTTLS);
        // extension is enabled so gives Some
        assert_eq!(sut.get(&STARTTLS).unwrap(), Some(STARTTLS));
    }
    #[test]
    fn check_extension() {
        let mut sut = ExtensionSet::new();
        // extension is disabled so gives None
        assert_eq!(sut.is_enabled(&STARTTLS), false);
        sut.enable(&STARTTLS);
        // extension is enabled so gives Some
        assert_eq!(sut.is_enabled(&STARTTLS), true);
    }
}

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

    #[test]
    fn parse_starttls() {
        assert_eq!(STARTTLS.parse("STARTTLS").unwrap().unwrap(), STARTTLS);
    }
    #[test]
    fn parse_incomplete() {
        assert_eq!(STARTTLS.parse("STARTT").unwrap_err(), Error::Incomplete);
    }
    #[test]
    fn parse_invalid() {
        assert_eq!(STARTTLS.parse("STARTTLS ").unwrap_err(), Error::Invalid(8));
        assert_eq!(STARTTLS.parse("STARTTLS\t").unwrap_err(), Error::Invalid(8));
        assert_eq!(
            STARTTLS.parse("STARTTLS param").unwrap_err(),
            Error::Invalid(8)
        );
    }
    #[test]
    fn parse_mismatch() {
        assert_eq!(STARTTLS.parse("OTHER").unwrap(), None);
        assert_eq!(STARTTLS.parse("STARTTLSx").unwrap(), None);
    }
}