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
use std::fmt;

use crate::{Result, Tape, Value};

/// A tag.
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq)]
pub struct Tag(pub [u8; 4]);

deref! { Tag::0 => [u8; 4] }

impl fmt::Debug for Tag {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use std::str;

        match str::from_utf8(&self.0[..]) {
            Ok(name) => write!(formatter, "Tag({:?})", name),
            _ => write!(formatter, "Tag({:?})", self.0),
        }
    }
}

impl From<u32> for Tag {
    #[inline(always)]
    fn from(number: u32) -> Self {
        use std::mem;

        Tag(unsafe { mem::transmute(u32::from_be(number)) })
    }
}

impl Value for Tag {
    #[inline(always)]
    fn read<T: Tape>(tape: &mut T) -> Result<Self> {
        Ok(Tag(tape.take()?))
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use super::Tag;
    use crate::Value;

    #[test]
    fn debug() {
        assert!(format!("{:?}", Tag(*b"true")) == r#"Tag("true")"#);
    }

    #[test]
    fn from() {
        assert!(Tag(*b"true") == Tag::from(0x74727565));
    }

    #[test]
    fn read() {
        let mut tape = Cursor::new(b"true".to_vec());
        assert!(Tag::read(&mut tape).unwrap() == Tag(*b"true"));
    }
}