pdf_writer/
macros.rs

1/// Test that `buf` is the same as the result of concatenating the strings.
2#[cfg(test)]
3macro_rules! test {
4    ($buf:expr, $($line:literal),* $(,)?) => {{
5        let buf = $buf;
6        let expected = [$(&$line[..]),*].join(&b"\n"[..]);
7        if buf != expected {
8            println!("=========== EXPECTED =============");
9            println!("{}", String::from_utf8_lossy(&expected));
10            println!("============= FOUND ==============");
11            println!("{}", String::from_utf8_lossy(&buf));
12            println!("=============================");
13            panic!("assertion failed");
14        }
15    }}
16}
17
18/// Test how an object is written.
19#[cfg(test)]
20macro_rules! test_obj {
21    (|$obj:ident| $write:expr, $($tts:tt)*) => {{
22        test!(crate::tests::slice_obj(|$obj| { $write; }), $($tts)*)
23    }}
24}
25
26/// Test how a primitive object is written.
27#[cfg(test)]
28macro_rules! test_primitive {
29    ($value:expr, $($tts:tt)*) => {
30        test_obj!(|obj| obj.primitive($value), $($tts)*);
31    }
32}
33
34/// Implements the `Writer`, `Rewrite` and `Typed` traits.
35macro_rules! writer {
36    ($ty:ident: |$obj:ident| $($tts:tt)*) => {
37        impl<'a> Writer<'a> for $ty<'a> {
38            #[inline]
39            fn start($obj: Obj<'a>) -> Self {
40                $($tts)*
41            }
42        }
43
44        impl<'a, 'any> Rewrite<'a> for $ty<'any> {
45            type Output = $ty<'a>;
46        }
47    };
48}
49
50/// Implements `Deref` and `DerefMut` by delegating to a field of a struct.
51macro_rules! deref {
52    ($a:lifetime, $from:ty => $to:ty, $field:ident) => {
53        impl<$a> std::ops::Deref for $from {
54            type Target = $to;
55
56            #[inline]
57            fn deref(&self) -> &Self::Target {
58                &self.$field
59            }
60        }
61
62        impl<$a> std::ops::DerefMut for $from {
63            #[inline]
64            fn deref_mut(&mut self) -> &mut Self::Target {
65                &mut self.$field
66            }
67        }
68    };
69}