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

use std::fmt::Display;
use regex::Regex;
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};

#[cfg(test)]
mod test;

pub type ValidationResult = Result<(), Error>;

#[derive(Debug)]
pub struct Error{
	message: String
}
impl Error{
	pub fn new(msg: &str) -> Error{
		Error{
			message: msg.to_owned()
		}
	}
	pub fn get_message(&self) -> &str{
		&self.message
	}
}

pub struct Schema<'a, T>{
	validator: Box<Rule<T> + 'a>,
	name: String
}

impl<'a, T: 'a> Schema<'a, T>{
	pub fn new(name: &str) -> Schema<'a, T>{
		Schema{
			name: name.to_owned(),
			validator: Box::new(|_: &T|{
				Ok(())
			})
		}
	}
	
	pub fn validate(&self, data: &T) -> ValidationResult{
		self.validator.validate(data)
	}
	
	pub fn rule<R>(self, rule: R) -> Schema<'a, T> 
	where R: Rule<T> + 'a{
		Schema{
			name: self.name.clone(),
			validator: Box::new(move |data: &T|{
				try!(self.validate(data));
				rule.validate(data)
			})
		}
	}
}

impl<'a> Schema<'a, &'a str>{
	pub fn whitelist_chars(self, whitelist: &str) -> Self{
		let whitelist = whitelist.to_owned();
		let name = self.name.clone();
		self.rule(move |input: &&str|{
			for c in input.chars(){
				if !whitelist.contains(c){
					return Err(Error::new(&format!("{} cannot contain the character '{}'", name, c)))
				}
			}
			Ok(())
		})
	}
	pub fn match_regex(self, pattern: &str, description: &str) -> Self{	
		//TODO: remove unwrap and use compile time regex check
		let regex = Regex::new(pattern).unwrap();
		let name = self.name.clone();
		let description = description.to_owned();
		self.rule(move |input: &&str|{
			match regex.is_match(input){
				true => Ok(()),
				false => Err(Error::new(&format!("{} must be {}", name, description)))
			}
		})
	}
	
	pub fn email(self) -> Self{	
		self.match_regex(r"^[^@]+@[^@]+\.[^@]+$", "a valid email")
	}
	
	pub fn length<A>(self, bounds: A) -> Self
	where A: Bounds<usize>{
		let name = self.name.clone();
		match bounds.get_bounds(){
			Bound::Unbounded => self,
			Bound::RangeFrom(a) => self.rule(move |input: &&str|{
				match input.len() >= a {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must be at least {} characters long", name, a)))
				}
			}),
			Bound::RangeTo(b)=> self.rule(move |input: &&str|{
				match input.len() < b {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must be less than {} characters long", name, b)))
				}
			}),
			Bound::Range(a,b) => self.rule(move |input: &&str|{
				match input.len() >= a && input.len() < b {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must be {} to {} characters long", name, a, b-1)))
				}
			}),
			Bound::Exact(a) => self.rule(move |input: &&str|{
				match input.len() == a {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must be {} characters long", name, a)))
				}
			})
		}
	} 
}

impl<'a, T> Schema<'a, T>
where T: PartialOrd + 'a + Display{
	pub fn range<A>(self, bounds: A) -> Self
	where A: Bounds<T>{
		let name = self.name.clone();
		match bounds.get_bounds(){
			Bound::Unbounded => self,
			Bound::RangeFrom(a) => self.rule(move |input: &T|{
				match input >= &a {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must be >= {}", name, a)))
				}
			}),
			Bound::RangeTo(b)=> self.rule(move |input: &T|{
				match input < &b {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must be < {}", name, b)))
				}
			}),
			Bound::Range(a,b) => self.rule(move |input: &T|{
				match input >= &a && input < &b {
					true => Ok(()),
					false => Err(Error::new(&format!("{} must >= {} and < {}", name, a, b)))
				}
			}),
			Bound::Exact(a) => self.rule(move |input: &T|{
				match input == &a {
					true => Ok(()),
					false => Err(Error::new(&format!("{}, must be {}", name, a)))
				}
			})
		}
	}
}

pub trait Rule<T>{
	fn validate(&self, input:&T) -> ValidationResult;
}

impl<F, Type> Rule<Type> for F
where F: Fn(&Type) -> ValidationResult{
	fn validate(&self, input:&Type) -> ValidationResult{
		(*self)(input)
	}
}

pub trait Bounds<T>{
	fn get_bounds(&self) -> Bound<T>;
}

pub enum Bound<T>{
	Exact(T),
	Range(T, T),
	RangeFrom(T),
	RangeTo(T),
	Unbounded
}

impl Bounds<usize> for usize{
	fn get_bounds(&self) -> Bound<usize>{
		Bound::Exact(*self)
	}
}

impl Bounds<usize> for Range<usize>{
	fn get_bounds(&self) -> Bound<usize>{
		Bound::Range(self.start, self.end)
	}
}

impl Bounds<usize> for RangeFrom<usize>{
	fn get_bounds(&self) -> Bound<usize>{
		Bound::RangeFrom(self.start)
	}
}

impl Bounds<usize> for RangeTo<usize>{
	fn get_bounds(&self) -> Bound<usize>{
		Bound::RangeTo(self.end)
	}
}

impl Bounds<usize> for RangeFull{
	fn get_bounds(&self) -> Bound<usize>{
		Bound::Unbounded
	}
}