forked from typst/pdf-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacros.rs
More file actions
77 lines (69 loc) · 2.22 KB
/
macros.rs
File metadata and controls
77 lines (69 loc) · 2.22 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// Test that `buf` is the same as the result of concatenating the strings.
#[cfg(test)]
macro_rules! test {
($buf:expr, $($line:literal),* $(,)?) => {{
let buf = $buf;
let expected = [$(&$line[..]),*].join(&b"\n"[..]);
if buf != expected {
println!("=========== EXPECTED =============");
println!("{}", String::from_utf8_lossy(&expected));
println!("============= FOUND ==============");
println!("{}", String::from_utf8_lossy(&buf));
println!("=============================");
panic!("assertion failed");
}
}}
}
/// Test how an object is written.
#[cfg(test)]
macro_rules! test_obj {
(|$obj:ident| $write:expr, $($tts:tt)*) => {{
test!(crate::tests::slice_obj(|$obj| { $write; }, crate::WriteSettings::default()), $($tts)*)
}}
}
/// Test how an object is written, without pretty-printing.
#[cfg(test)]
macro_rules! test_obj_no_pretty {
(|$obj:ident| $write:expr, $($tts:tt)*) => {{
test!(crate::tests::slice_obj(|$obj| { $write; }, crate::WriteSettings { pretty: false }), $($tts)*)
}}
}
/// Test how a primitive object is written.
#[cfg(test)]
macro_rules! test_primitive {
($value:expr, $($tts:tt)*) => {
test_obj!(|obj| obj.primitive($value), $($tts)*);
}
}
/// Implements the `Writer`, `Rewrite` and `Typed` traits.
macro_rules! writer {
($ty:ident: |$obj:ident| $($tts:tt)*) => {
impl<'a> Writer<'a> for $ty<'a> {
#[inline]
fn start($obj: Obj<'a>) -> Self {
$($tts)*
}
}
impl<'a, 'any> Rewrite<'a> for $ty<'any> {
type Output = $ty<'a>;
}
};
}
/// Implements `Deref` and `DerefMut` by delegating to a field of a struct.
macro_rules! deref {
($a:lifetime, $from:ty => $to:ty, $field:ident) => {
impl<$a> std::ops::Deref for $from {
type Target = $to;
#[inline]
fn deref(&self) -> &Self::Target {
&self.$field
}
}
impl<$a> std::ops::DerefMut for $from {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.$field
}
}
};
}