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
pub mod err;
pub mod part;

pub use self::err::{TexelError, Result};

use self::part::Part;
use std::fmt;

#[derive(Copy, Clone, Debug, Default)]
pub struct Texel {
    part: Part,
    glyph: char,
}

impl Texel {
    pub fn new(part: &str, glyph: char) -> Result<Self> {
        if let '\u{e000}'...'\u{efff}' = glyph {
            match Part::new(part) {
                Err(why) => Err(TexelError::PartFail(why)),
                Ok(part) => Ok(
                    Texel {
                        part: part,
                        glyph: glyph,
                    }
                ),
            }
        } else {
            Err(TexelError::ForbiddenGlyph(glyph))
        }
    }

    /// The accessor method `get_part` returns the Texel Part.
    pub fn get_part(&self) -> &Part {
        &self.part
    }

    /// The accessor method `get_glyph` returns the Texel Glyph.
    pub fn get_glyph(&self) -> char {
        self.glyph
    }

    pub fn set_glyph(&mut self, glyph: char) {
        self.glyph = glyph;
    }
}

impl PartialEq for Texel {
    fn eq(&self, rhs: &Texel) -> bool {
        self.part.eq(&rhs.part)
    }
}

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