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

use self::regex::Regex;
use super::{Validated, ValidatedWrapper};

use std::fmt::{self, Display, Debug, Formatter};

#[derive(Debug, PartialEq, Clone)]
pub enum ShortCryptUrlComponentError {
    IncorrectFormat,
}

pub type ShortCryptUrlComponentResult = Result<ShortCryptUrlComponent, ShortCryptUrlComponentError>;

pub struct ShortCryptUrlComponentValidator {}

#[derive(Clone)]
pub struct ShortCryptUrlComponent {
    short_crypt_url_component_url: String,
}

impl ShortCryptUrlComponent {
    pub fn get_short_crypt_url_component_url(&self) -> &str {
        &self.short_crypt_url_component_url
    }
}

impl Validated for ShortCryptUrlComponent {}

impl Debug for ShortCryptUrlComponent {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_fmt(format_args!("ShortCryptUrlComponent({})", self.short_crypt_url_component_url))?;
        Ok(())
    }
}

impl Display for ShortCryptUrlComponent {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(&self.short_crypt_url_component_url)?;
        Ok(())
    }
}

impl PartialEq for ShortCryptUrlComponent {
    fn eq(&self, other: &Self) -> bool {
        self.short_crypt_url_component_url.eq(&other.short_crypt_url_component_url)
    }

    fn ne(&self, other: &Self) -> bool {
        self.short_crypt_url_component_url.ne(&other.short_crypt_url_component_url)
    }
}

impl ShortCryptUrlComponentValidator {
    pub fn is_short_crypt_url_component_url(&self, short_crypt_url_component_url: &str) -> bool {
        self.parse_inner(short_crypt_url_component_url).is_ok()
    }

    pub fn parse_string(&self, short_crypt_url_component_url: String) -> ShortCryptUrlComponentResult {
        let mut short_crypt_url_component_url_inner = self.parse_inner(&short_crypt_url_component_url)?;

        short_crypt_url_component_url_inner.short_crypt_url_component_url = short_crypt_url_component_url;

        Ok(short_crypt_url_component_url_inner)
    }

    pub fn parse_str(&self, short_crypt_url_component_url: &str) -> ShortCryptUrlComponentResult {
        let mut short_crypt_url_component_url_inner = self.parse_inner(short_crypt_url_component_url)?;

        short_crypt_url_component_url_inner.short_crypt_url_component_url = short_crypt_url_component_url.to_string();

        Ok(short_crypt_url_component_url_inner)
    }

    fn parse_inner(&self, short_crypt_url_component_url: &str) -> ShortCryptUrlComponentResult {
        let re = Regex::new(r"^([A-Za-z0-9\-_]{4})*([A-Za-z0-9\-_]|[A-Za-z0-9\-_]{3,4})$").unwrap();

        if re.is_match(short_crypt_url_component_url) {
            Ok(ShortCryptUrlComponent {
                short_crypt_url_component_url: String::new(),
            })
        } else {
            Err(ShortCryptUrlComponentError::IncorrectFormat)
        }
    }
}

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

    #[test]
    fn test_short_crypt_url_component_url_methods() {
        let short_crypt_url_component_url = "2E87Wx52-Tvo".to_string();

        let scuv = ShortCryptUrlComponentValidator {};

        let short_crypt_url_component_url = scuv.parse_string(short_crypt_url_component_url).unwrap();

        assert_eq!("2E87Wx52-Tvo", short_crypt_url_component_url.get_short_crypt_url_component_url());
    }

    #[test]
    fn test_short_crypt_url_component_url_lv1() {
        let short_crypt_url_component_url = "2E87Wx52-Tvo".to_string();

        let scuv = ShortCryptUrlComponentValidator {};

        scuv.parse_string(short_crypt_url_component_url).unwrap();
    }
}

// ShortCryptUrlComponent's wrapper struct is itself
impl ValidatedWrapper for ShortCryptUrlComponent {
    type Error = ShortCryptUrlComponentError;

    fn from_string(short_crypt_url_component_url: String) -> Result<Self, Self::Error> {
        ShortCryptUrlComponent::from_string(short_crypt_url_component_url)
    }

    fn from_str(short_crypt_url_component_url: &str) -> Result<Self, Self::Error> {
        ShortCryptUrlComponent::from_str(short_crypt_url_component_url)
    }
}

impl ShortCryptUrlComponent {
    pub fn from_string(short_crypt_url_component_url: String) -> Result<Self, ShortCryptUrlComponentError> {
        let bv = ShortCryptUrlComponentValidator {};

        bv.parse_string(short_crypt_url_component_url)
    }

    pub fn from_str(short_crypt_url_component_url: &str) -> Result<Self, ShortCryptUrlComponentError> {
        let bv = ShortCryptUrlComponentValidator {};

        bv.parse_str(short_crypt_url_component_url)
    }
}

#[cfg(feature = "rocketly")]
impl<'a> ::rocket::request::FromFormValue<'a> for ShortCryptUrlComponent {
    type Error = ShortCryptUrlComponentError;

    fn from_form_value(form_value: &'a ::rocket::http::RawStr) -> Result<Self, Self::Error> {
        ShortCryptUrlComponent::from_str(form_value)
    }
}