Skip to main content

pdf_lib_rs/core/objects/
pdf_null.rs

1use super::pdf_object::PdfObjectTrait;
2
3/// The PDF null singleton. Use `PDF_NULL` constant.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct PdfNull;
6
7/// The singleton PDF null value.
8pub const PDF_NULL: PdfNull = PdfNull;
9
10impl std::fmt::Display for PdfNull {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        write!(f, "null")
13    }
14}
15
16impl PdfObjectTrait for PdfNull {
17    fn size_in_bytes(&self) -> usize {
18        4
19    }
20
21    fn copy_bytes_into(&self, buffer: &mut [u8], offset: usize) -> usize {
22        buffer[offset] = b'n';
23        buffer[offset + 1] = b'u';
24        buffer[offset + 2] = b'l';
25        buffer[offset + 3] = b'l';
26        4
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::utils::typed_array_for;
34
35    #[test]
36    fn can_be_converted_to_string() {
37        assert_eq!(PDF_NULL.to_string(), "null");
38    }
39
40    #[test]
41    fn can_provide_size_in_bytes() {
42        assert_eq!(PDF_NULL.size_in_bytes(), 4);
43    }
44
45    #[test]
46    fn can_be_serialized() {
47        let mut buffer = vec![b' '; 8];
48        assert_eq!(PDF_NULL.copy_bytes_into(&mut buffer, 3), 4);
49        assert_eq!(buffer, typed_array_for("   null "));
50    }
51}