Skip to main content

hayro_syntax/object/
null.rs

1//! The null object.
2
3use crate::object::Object;
4use crate::object::macros::object;
5use crate::reader::Reader;
6use crate::reader::{Readable, ReaderContext, Skippable};
7use crate::trivia::is_regular_character;
8use core::fmt::{Display, Formatter};
9
10/// The null object.
11#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
12pub struct Null;
13
14impl Display for Null {
15    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
16        f.write_str("null")
17    }
18}
19
20object!(Null, Null);
21
22impl Skippable for Null {
23    fn skip(r: &mut Reader<'_>, _: bool) -> Option<()> {
24        r.forward_tag(b"null")?;
25
26        (!r.peek_byte().is_some_and(is_regular_character)).then_some(())
27    }
28}
29
30impl Readable<'_> for Null {
31    fn read(r: &mut Reader<'_>, ctx: &ReaderContext<'_>) -> Option<Self> {
32        Self::skip(r, ctx.in_content_stream())?;
33
34        Some(Self)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::object::Null;
41    use crate::reader::Reader;
42    use crate::reader::ReaderExt;
43
44    #[test]
45    fn display() {
46        assert_eq!(format!("{}", Null), "null");
47    }
48
49    #[test]
50    fn null() {
51        assert_eq!(
52            Reader::new("null".as_bytes())
53                .read_without_context::<Null>()
54                .unwrap(),
55            Null
56        );
57    }
58
59    #[test]
60    fn null_trailing() {
61        assert!(
62            Reader::new("nullabs".as_bytes())
63                .read_without_context::<Null>()
64                .is_none()
65        );
66    }
67}