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
// Copyright 2016  Jonas mg
// See the 'AUTHORS' file at the top-level directory for a full list of authors.

pub mod table;

use std::error;
use std::error::Error;
use std::fmt;

pub trait Check {
    /// is_alpha checks if it is (A-Z / a-z).
    fn is_alpha(self) -> bool;
    /// is_digit checks if it is (0-9).
    fn is_digit(self) -> bool;
    /// is_vchar checks if it is a visible (printing) character.
    fn is_vchar(self) -> bool;
    /// is_wsp checks if it is a white space (space / horizontal tabulation).
    fn is_wsp(self) -> bool;
}

impl Check for u8 {
    fn is_alpha(self) -> bool {
        match self {
            b'a'...b'z' | b'A'...b'Z' => true,
            _ => false,
        }
    }

    fn is_digit(self) -> bool {
        match self {
            b'0'...b'9' => true,
            _ => false,
        }
    }

    fn is_vchar(self) -> bool {
        match self {
            0x21...0x7E => true,
            _ => false,
        }
    }

    fn is_wsp(self) -> bool {
        match self {
            table::SPACE | table::HT => true,
            _ => false,
        }
    }
}

impl Check for char {
    fn is_alpha(self) -> bool {
        match self {
            'a'...'z' | 'A'...'Z' => true,
            _ => false,
        }
    }

    fn is_digit(self) -> bool {
        match self {
            '0'...'9' => true,
            _ => false,
        }
    }

    fn is_vchar(self) -> bool {
        match self as u8 {
            0x21...0x7E => true,
            _ => false,
        }
    }

    fn is_wsp(self) -> bool {
        match self {
            table::SPACE_char | table::HT_char => true,
            _ => false,
        }
    }
}

/// check_ascii checks for non-ASCII characters.
pub fn check_ascii(name: &str) -> Result<(), AsciiError> {
    let mut i: usize = 0;

    for byte in name.bytes() {
        match byte {
            0...127 => (),
            _ => {
                match name[i..].chars().next() {
                    Some(v) => return Err(AsciiError { c: v }),
                    None => unreachable!(),
                }
            }
	    }
        i = i + 1;
    }

    Ok(())
}

// == Errors
//

#[derive(Debug, PartialEq)]
pub struct AsciiError {
    pub c: char,
}

impl fmt::Display for AsciiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}: {}", self.description(), self.c)
    }
}

impl error::Error for AsciiError {
    fn description(&self) -> &str {
        "contain a non US-ASCII character"
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

// == Tests
//

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

    #[test]
    fn test_alpha() {
        assert!(Check::is_alpha(b'a'));
        assert!(Check::is_alpha('j'));
        assert!(Check::is_alpha('Z'));

        assert_eq!('0'.is_alpha(), false);
    }

    #[test]
    fn test_digit() {
        assert!(Check::is_digit(b'0'));
        assert!(Check::is_digit('5'));
        assert!(Check::is_digit('9'));

        assert_eq!(Check::is_digit('a'), false)
    }

    #[test]
    fn test_vchar() {
        assert!(Check::is_vchar(b'J'));
        assert!(Check::is_vchar('0'));
        assert!(Check::is_vchar('-'));

        assert_eq!(Check::is_vchar(SPACE), false);
    }

    #[test]
    fn test_wsp() {
        assert!(Check::is_wsp(HT));
        assert!(Check::is_wsp(SPACE));

        assert_eq!(Check::is_wsp(b'a'), false);
        assert_eq!(Check::is_wsp('-'), false);
    }

    #[test]
    fn test_check_ascii() {
        check_ascii("aeiou").unwrap();

        assert_eq!(check_ascii("äeiou"), Err(AsciiError { c: 'ä' }));
        assert_eq!(check_ascii("aeïou"), Err(AsciiError { c: 'ï' }));
        assert_eq!(check_ascii("aeioü"), Err(AsciiError { c: 'ü' }));
    }
}