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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
extern crate regex;

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

use std::error::Error;
use std::fmt::{self, Display, Debug, Formatter};
use std::ops::Deref;

lazy_static! {
    static ref SHORT_CRYPT_QR_CODE_ALPHANUMERIC_RE: Regex = {
        Regex::new(r"^([A-Z0-9]{8})*([A-Z0-9]|[A-Z0-9]{3}|[A-Z0-9]{5,6}|[A-Z0-9]{8})$").unwrap()
    };
}

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

impl Display for ShortCryptQRCodeAlphanumericError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Debug::fmt(self, f)
    }
}

impl Error for ShortCryptQRCodeAlphanumericError {}

pub type ShortCryptQRCodeAlphanumericResult = Result<ShortCryptQRCodeAlphanumeric, ShortCryptQRCodeAlphanumericError>;

#[derive(Debug, PartialEq)]
pub struct ShortCryptQRCodeAlphanumericValidator {}

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ShortCryptQRCodeAlphanumeric {
    short_crypt_qr_code_alphanumeric: String,
}

impl ShortCryptQRCodeAlphanumeric {
    pub fn get_short_crypt_qr_code_alphanumeric_url(&self) -> &str {
        &self.short_crypt_qr_code_alphanumeric
    }

    pub fn into_string(self) -> String {
        self.short_crypt_qr_code_alphanumeric
    }

    pub unsafe fn from_string_unchecked(short_crypt_qr_code_alphanumeric: String) -> ShortCryptQRCodeAlphanumeric {
        ShortCryptQRCodeAlphanumeric {
            short_crypt_qr_code_alphanumeric
        }
    }
}

impl Deref for ShortCryptQRCodeAlphanumeric {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.short_crypt_qr_code_alphanumeric
    }
}

impl Validated for ShortCryptQRCodeAlphanumeric {}

impl Debug for ShortCryptQRCodeAlphanumeric {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        impl_debug_for_tuple_struct!(ShortCryptQRCodeAlphanumeric, f, self, let .0 = self.short_crypt_qr_code_alphanumeric);
    }
}

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

impl ShortCryptQRCodeAlphanumericValidator {
    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) -> ShortCryptQRCodeAlphanumericResult {
        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 = 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) -> ShortCryptQRCodeAlphanumericResult {
        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.push_str(short_crypt_qr_code_alphanumeric_url);

        Ok(short_crypt_qr_code_alphanumeric_url_inner)
    }

    fn parse_inner(&self, short_crypt_qr_code_alphanumeric_url: &str) -> ShortCryptQRCodeAlphanumericResult {
        if SHORT_CRYPT_QR_CODE_ALPHANUMERIC_RE.is_match(short_crypt_qr_code_alphanumeric_url) {
            Ok(ShortCryptQRCodeAlphanumeric {
                short_crypt_qr_code_alphanumeric: String::new(),
            })
        } else {
            Err(ShortCryptQRCodeAlphanumericError::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 = ShortCryptQRCodeAlphanumericValidator {};

        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 = ShortCryptQRCodeAlphanumericValidator {};

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

// ShortCryptQRCodeAlphanumeric's wrapper struct is itself
impl ValidatedWrapper for ShortCryptQRCodeAlphanumeric {
    type Error = ShortCryptQRCodeAlphanumericError;

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

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

impl ShortCryptQRCodeAlphanumeric {
    pub fn from_string(short_crypt_qr_code_alphanumeric_url: String) -> Result<Self, ShortCryptQRCodeAlphanumericError> {
        ShortCryptQRCodeAlphanumeric::create_validator().parse_string(short_crypt_qr_code_alphanumeric_url)
    }

    pub fn from_str(short_crypt_qr_code_alphanumeric_url: &str) -> Result<Self, ShortCryptQRCodeAlphanumericError> {
        ShortCryptQRCodeAlphanumeric::create_validator().parse_str(short_crypt_qr_code_alphanumeric_url)
    }

    fn create_validator() -> ShortCryptQRCodeAlphanumericValidator {
        ShortCryptQRCodeAlphanumericValidator {}
    }
}

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

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

#[cfg(feature = "rocketly")]
impl<'a> ::rocket::request::FromParam<'a> for ShortCryptQRCodeAlphanumeric {
    type Error = ShortCryptQRCodeAlphanumericError;

    fn from_param(param: &'a ::rocket::http::RawStr) -> Result<Self, Self::Error> {
        ShortCryptQRCodeAlphanumeric::from_str(param)
    }
}

#[cfg(feature = "serdely")]
struct StringVisitor;

#[cfg(feature = "serdely")]
impl<'de> ::serde::de::Visitor<'de> for StringVisitor {
    type Value = ShortCryptQRCodeAlphanumeric;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a ShortCrypt QR code alphanumeric string")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: ::serde::de::Error {
        ShortCryptQRCodeAlphanumeric::from_str(v).map_err(|err| {
            E::custom(err.to_string())
        })
    }

    fn visit_string<E>(self, v: String) -> Result<Self::Value, E> where E: ::serde::de::Error {
        ShortCryptQRCodeAlphanumeric::from_string(v).map_err(|err| {
            E::custom(err.to_string())
        })
    }
}

#[cfg(feature = "serdely")]
impl<'de> ::serde::Deserialize<'de> for ShortCryptQRCodeAlphanumeric {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'de> {deserializer.deserialize_string(StringVisitor)
    }
}

#[cfg(feature = "serdely")]
impl ::serde::Serialize for ShortCryptQRCodeAlphanumeric {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
        serializer.serialize_str(&self.short_crypt_qr_code_alphanumeric)
    }
}