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 ShortCryptQRCodeAlphanumericComponentError {
    IncorrectFormat,
}

pub type ShortCryptQRCodeAlphanumericComponentResult = Result<ShortCryptQRCodeAlphanumericComponent, ShortCryptQRCodeAlphanumericComponentError>;

pub struct ShortCryptQRCodeAlphanumericComponentValidator {}

#[derive(Clone)]
pub struct ShortCryptQRCodeAlphanumericComponent {
    short_crypt_qr_code_alphanumeric_url: String,
}

impl ShortCryptQRCodeAlphanumericComponent {
    pub fn get_short_crypt_qr_code_alphanumeric_url(&self) -> &str {
        &self.short_crypt_qr_code_alphanumeric_url
    }
}

impl Validated for ShortCryptQRCodeAlphanumericComponent {}

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

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

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

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

impl ShortCryptQRCodeAlphanumericComponentValidator {
    pub fn is_short_crypt_qr_code_alphanumeric_url(&self, short_crypt_qr_code_alphanumeric_url: &str) -> bool {
        self.parse_inner(short_crypt_qr_code_alphanumeric_url).is_ok()
    }

    pub fn parse_string(&self, short_crypt_qr_code_alphanumeric_url: String) -> ShortCryptQRCodeAlphanumericComponentResult {
        let mut short_crypt_qr_code_alphanumeric_url_inner = self.parse_inner(&short_crypt_qr_code_alphanumeric_url)?;

        short_crypt_qr_code_alphanumeric_url_inner.short_crypt_qr_code_alphanumeric_url = short_crypt_qr_code_alphanumeric_url;

        Ok(short_crypt_qr_code_alphanumeric_url_inner)
    }

    pub fn parse_str(&self, short_crypt_qr_code_alphanumeric_url: &str) -> ShortCryptQRCodeAlphanumericComponentResult {
        let mut short_crypt_qr_code_alphanumeric_url_inner = self.parse_inner(short_crypt_qr_code_alphanumeric_url)?;

        short_crypt_qr_code_alphanumeric_url_inner.short_crypt_qr_code_alphanumeric_url = short_crypt_qr_code_alphanumeric_url.to_string();

        Ok(short_crypt_qr_code_alphanumeric_url_inner)
    }

    fn parse_inner(&self, short_crypt_qr_code_alphanumeric_url: &str) -> ShortCryptQRCodeAlphanumericComponentResult {
        let re = Regex::new(r"^([A-Z0-9]{8})*([A-Z0-9]|[A-Z0-9]{3}|[A-Z0-9]{5,6}|[A-Z0-9]{8})$").unwrap();

        if re.is_match(short_crypt_qr_code_alphanumeric_url) {
            Ok(ShortCryptQRCodeAlphanumericComponent {
                short_crypt_qr_code_alphanumeric_url: String::new(),
            })
        } else {
            Err(ShortCryptQRCodeAlphanumericComponentError::IncorrectFormat)
        }
    }
}

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

    #[test]
    fn test_short_crypt_qr_code_alphanumeric_url_methods() {
        let short_crypt_qr_code_alphanumeric_url = "3BHNNR45XZH8PU".to_string();

        let scqacv = ShortCryptQRCodeAlphanumericComponentValidator {};

        let short_crypt_qr_code_alphanumeric_url = scqacv.parse_string(short_crypt_qr_code_alphanumeric_url).unwrap();

        assert_eq!("3BHNNR45XZH8PU", short_crypt_qr_code_alphanumeric_url.get_short_crypt_qr_code_alphanumeric_url());
    }

    #[test]
    fn test_short_crypt_qr_code_alphanumeric_url_lv1() {
        let short_crypt_qr_code_alphanumeric_url = "3BHNNR45XZH8PU".to_string();

        let scqacv = ShortCryptQRCodeAlphanumericComponentValidator {};

        scqacv.parse_string(short_crypt_qr_code_alphanumeric_url).unwrap();
    }
}

// ShortCryptQRCodeAlphanumericComponent's wrapper struct is itself
impl ValidatedWrapper for ShortCryptQRCodeAlphanumericComponent {
    type Error = ShortCryptQRCodeAlphanumericComponentError;

    fn from_string(short_crypt_qr_code_alphanumeric_url: String) -> Result<Self, Self::Error> {
        ShortCryptQRCodeAlphanumericComponent::from_string(short_crypt_qr_code_alphanumeric_url)
    }

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

impl ShortCryptQRCodeAlphanumericComponent {
    pub fn from_string(short_crypt_qr_code_alphanumeric_url: String) -> Result<Self, ShortCryptQRCodeAlphanumericComponentError> {
        let bv = ShortCryptQRCodeAlphanumericComponentValidator {};

        bv.parse_string(short_crypt_qr_code_alphanumeric_url)
    }

    pub fn from_str(short_crypt_qr_code_alphanumeric_url: &str) -> Result<Self, ShortCryptQRCodeAlphanumericComponentError> {
        let bv = ShortCryptQRCodeAlphanumericComponentValidator {};

        bv.parse_str(short_crypt_qr_code_alphanumeric_url)
    }
}

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

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