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
use derive_more::Display;
#[derive(Debug,PartialEq,Eq)]
pub struct InvalidValueError {}
#[derive(Clone, Debug, PartialEq, Eq, Display)]
pub struct Value<'a>(&'a str);
macro_rules! value {
($name:ident $tag:expr) => {
pub const $name: Value<'static> = Value($tag);
};
}
impl<'a> Value<'a> {
pub fn create(str: &'a str) -> Result<Value<'a>,InvalidValueError>
{
let allowed = |c:char| -> bool {
char::is_alphabetic(c)
|| c == ':'
|| c == '/'
|| c == '.'
};
match str.chars().all(allowed) {
true => Ok(Value(str)),
false => Err (InvalidValueError{})
}
}
value!(TEXT_CSS "text/css");
value!(STYLESHEET "stylesheet");
value!(UTF_8 "UTF-8");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_is_valid() {
let url = "http://google.com";
let node = Value::create(url);
let expected = Result::Ok(Value(url));
assert_eq!(node, expected);
}
}