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
#[macro_use]
extern crate lazy_static;
extern crate csv;

use std::char;

/// Character, name, hex code, int code.
#[derive(Debug, Clone)]
pub struct Character {
    pub character: char,
    pub name: String,
    pub hex_code: String,
    pub code: u32
}

lazy_static! {
    static ref UNICODE_DATA: Vec<Character> = {
        let mut rdr = csv::Reader::from_string(
        	String::from_utf8(include_bytes!("../assets/unicode.csv").to_vec()).unwrap()
    	).delimiter(b';');
        let mut unicode_data: Vec<Character> = Vec::new();
        for row in rdr.decode() {
            let (code, name): (String, String) = row.unwrap();
            let code_int = u32::from_str_radix(&code, 16).unwrap();
            unicode_data.push(Character {
                character: char::from_u32(code_int).unwrap(),
                name: name,
                hex_code: code,
                code: code_int
            });
        }
        unicode_data
    };
}

pub fn info_by_char(character: char) -> Option<Character> {
    for n in &*UNICODE_DATA {
        if character == n.character {
            return Some(n.clone());
        }
    }
    None
}

pub fn info_by_name(name: &str) -> Option<Character> {
    for n in &*UNICODE_DATA {
        if name == n.name {
            return Some(n.clone());
        }
    }
    None
}

pub fn raw_unicode_data<'a>() -> &'a Vec<Character> {
    &*UNICODE_DATA
}

#[cfg(test)]
mod test {
	#[test]
    fn check_name() {
    	assert_eq!(::info_by_name("HUNDRED POINTS SYMBOL").unwrap().character,
    			   '💯');
    }
	#[test]
    fn check_char() {
    	assert_eq!(::info_by_char('💯').unwrap().name,
    			   "HUNDRED POINTS SYMBOL".to_string());
    }
	#[test]
    fn check_all_data() {
    	assert_eq!(::raw_unicode_data().get(28695).unwrap().name,
    			   "HUNDRED POINTS SYMBOL".to_string());
    }
}