entities/
lib.rs

1//! Provides the raw data needed to convert to and from HTML entities.
2//!
3//! ```
4//! extern crate entities;
5//! use entities::ENTITIES;
6//!
7//! fn main() {
8//!     let entity = ENTITIES
9//!         .iter()
10//!         .find(|e| e.entity == ">")
11//!         .unwrap();
12//!
13//!     assert_eq!(entity.characters, ">");
14//!     assert_eq!(entity.entity, ">");
15//! }
16//! ```
17//!
18//! There isn't a 1-to-1 mapping of entities to "characters" which is why this
19//! crate provides a flat array rather than a map—the best way to map the
20//! entities depends on the problem _you're_ trying to solve.
21//!
22//! If you want to create a mapping structure you can make one using static `str`
23//! slices to reuse the statically allocated strings from this crate e.g.
24//!
25//! ```
26//! # use self::std::collections::HashMap;
27//! # use entities::ENTITIES;
28//! fn make_mapping() -> HashMap<&'static str, &'static str> {
29//!     let mut mapping = HashMap::new();
30//!     mapping.insert(ENTITIES[0].entity, ENTITIES[0].characters);
31//!     mapping
32//! }
33//! ```
34//!
35//! Data was generated from
36//! [https://www.w3.org/TR/html5/entities.json](https://www.w3.org/TR/html5/entities.json)
37
38
39#![no_std]
40
41
42mod entities;
43
44
45/// The unicode codepoint(s) for the "character" an entity is encoding.
46#[derive(Debug)]
47pub enum Codepoints {
48    Single(u32),
49    Double(u32, u32),
50}
51
52
53/// Represents an entry in the array of entity definitions.
54#[derive(Debug)]
55pub struct Entity {
56    pub entity: &'static str,
57    pub codepoints: Codepoints,
58    pub characters: &'static str,
59}
60
61
62pub use entities::ENTITIES;
63
64
65#[cfg(test)]
66mod tests {
67    extern crate std;
68    use self::std::collections::HashMap;
69    use super::ENTITIES;
70
71    #[test]
72    fn sanity() {
73        let found = ENTITIES.iter().find(|e| e.entity == "&gt;");
74        assert!(found.is_some());
75        assert_eq!(found.unwrap().characters, ">");
76    }
77
78    #[test]
79    fn make_map() {
80        let mut mapping = HashMap::<&'static str, &'static str>::new();
81        mapping.insert(ENTITIES[0].entity, ENTITIES[0].characters);
82        assert_eq!(mapping.get(ENTITIES[0].entity),
83                   Some(&ENTITIES[0].characters));
84    }
85}