1use crate::{Decodable, Encodable};
3use std::{num::ParseIntError, str::FromStr};
4
5#[derive(Debug, PartialEq, Clone)]
7pub struct Glyph(pub char);
8
9impl Glyph {
10    pub fn new(glyph: char) -> Self {
12        Self(glyph)
13    }
14}
15
16impl Encodable<EncodedGlyph> for Glyph {
18    fn encode(&self) -> Result<EncodedGlyph, ParseIntError> {
19        Ok(EncodedGlyph::new(format!("{:04x}", self.0 as u32)))
20    }
21}
22
23#[derive(Debug, PartialEq, Clone)]
25pub struct EncodedGlyph(pub String);
26
27impl EncodedGlyph {
28    pub fn new(encoded_glyph: String) -> Self {
30        Self(encoded_glyph)
31    }
32}
33
34impl FromStr for EncodedGlyph {
35    type Err = ();
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        Ok(EncodedGlyph::new(s.to_string()))
39    }
40}
41
42impl From<char> for EncodedGlyph {
43    fn from(c_enc: char) -> Self {
44        Self::new(c_enc.to_string())
45    }
46}
47
48impl From<String> for EncodedGlyph {
49    fn from(s_enc: String) -> Self {
50        EncodedGlyph::from_str(s_enc.as_str()).unwrap()
51    }
52}
53
54impl Decodable<Glyph> for EncodedGlyph {
56    fn decode(&self) -> Result<Glyph, ParseIntError> {
57        let decimal = u32::from_str_radix(&self.0, 16).unwrap();
58        Ok(Glyph::new(char::from_u32(decimal).unwrap()))
59    }
60}