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
extern crate alloc;

use alloc::vec::Vec;
use core::convert::{TryFrom, From};

use crate::error::HttpError;

#[derive(Debug, PartialEq)]
pub enum Scheme {
    Http,
    Https,
}

impl TryFrom<&str> for Scheme {
    type Error = HttpError;

    fn try_from(src: &str) -> Result<Self, Self::Error> {
        match src {
            "http" => Ok(Scheme::Http),
            "https" => Ok(Scheme::Https),
            _ => Err(HttpError::InvalidScheme),
        }
    }
}

impl From<Scheme> for Vec<u8> {
    fn from(scheme: Scheme) -> Self {
        match scheme {
            Scheme::Http => b"http".to_vec(),
            Scheme::Https => b"https".to_vec(),
        }
    }
}

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

    #[test]
    fn from_str_http_test() {
        assert_eq!(Scheme::try_from("http"), Ok(Scheme::Http));
    }

    #[test]
    fn to_bytes_http_test() {
        assert_eq!(Vec::<u8>::from(Scheme::Http), b"http".to_vec());
    }

    #[test]
    fn from_str_https_test() {
        assert_eq!(Scheme::try_from("https"), Ok(Scheme::Https));
    }

    #[test]
    fn to_bytes_https_test() {
        assert_eq!(Vec::<u8>::from(Scheme::Https), b"https".to_vec());
    }

    #[test]
    fn from_str_invalid_scheme_test() {
        assert_eq!(Scheme::try_from("smtp"), Err(HttpError::InvalidScheme));
    }
}