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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::borrow::Cow;
use tracing::debug;
use crate::{
IncompleteMemeDefinition,
meme::{
Color, Meme, MemeBase, TextBox, TextOutline,
partial::{
PartialCanvas, PartialMeme, PartialMemeBase, PartialText, PartialTextBox,
PartialTextOutline, PartialTextOutlineParameters,
},
},
};
impl Meme {
pub fn from_partial(meme: PartialMeme) -> crate::Result<Self> {
debug!(?meme);
macro_rules! field_name {
( $static_:literal ) => {
Cow::Borrowed($static_)
};
( $dynamic:ident ) => {
Cow::Owned($dynamic.clone())
};
}
macro_rules! field_or_error {
( $field:ident, $errors:expr, [ $( $path_item:tt ),* $(,)? ]) => {
field_or_error!(
$field,
fallback: Default::default(),
$errors,
[ $( $path_item, )* ]
)
};
( $field:ident, fallback: $fallback:expr, $errors:expr, [ $( $path_item:tt ),* $(,)? ]) => {
match $field {
Some(field) => field,
None => {
$errors.push(IncompleteMemeDefinition::MissingField(vec![
$( field_name!( $path_item ) ),* ,
Cow::Borrowed(stringify!( $field ))
]));
$fallback
}
}
};
}
let mut errors = Vec::new();
let base = match meme.base {
PartialMemeBase::Image(path) => MemeBase::Image(path.into()),
PartialMemeBase::Canvas(PartialCanvas { color, size }) => MemeBase::Canvas {
color: field_or_error!(
color,
fallback: Color::WHITE,
&mut errors,
["canvas"]
),
size: field_or_error!(size, &mut errors, ["canvas"]),
},
PartialMemeBase::Extends(_) => {
unreachable!("dependencies should all be resolved")
}
};
let text = meme
.text
.into_iter()
.flat_map(|(name, text)| {
let PartialText::TextBox(PartialTextBox {
text,
position,
size,
rotate,
font,
caps,
color,
outline,
font_size,
line_height,
halign,
valign,
}) = text
else {
errors.push(IncompleteMemeDefinition::MissingParameters(vec![
Cow::Borrowed("text"),
Cow::Owned(name),
]));
return None;
};
let text = field_or_error!(text, &mut errors, ["text", name]);
let position = field_or_error!(position, &mut errors, ["text", name]);
let size = field_or_error!(size, &mut errors, ["text", name]);
let rotate = rotate.and_then(|angle| (angle != 0.0).then_some(angle));
let font = font.unwrap_or_else(|| "sans-serif".to_owned());
let caps = caps.unwrap_or(false);
let color = field_or_error!(
color,
fallback: Color::WHITE,
&mut errors,
["text", name]
);
let outline = match outline {
Some(outline) => match outline {
PartialTextOutline::Enabled(_) => {
errors.push(IncompleteMemeDefinition::MissingParameters(vec![
Cow::Borrowed("text"),
Cow::Owned(name.clone()),
Cow::Borrowed("outline"),
]));
None
}
PartialTextOutline::Parameters(PartialTextOutlineParameters {
enabled,
color,
width,
}) => enabled.then(|| TextOutline {
color: field_or_error!(
color,
fallback: Color::WHITE,
&mut errors,
["text", name, "outline"]
),
width: width.unwrap_or(TextOutline::DEFAULT_WIDTH),
}),
},
None => None,
};
let font_size = font_size.unwrap_or(
outline
.map(|outline| {
if outline.width < 1.0 {
size.1 - (2.0 * outline.width * size.1)
} else {
size.1 - outline.width
}
})
.unwrap_or(size.1),
);
let line_height = line_height.unwrap_or(1.0);
let halign = halign.unwrap_or_default();
let valign = valign.unwrap_or_default();
Some(TextBox {
text,
position,
size,
rotate,
font,
caps,
color,
outline,
font_size,
line_height,
halign,
valign,
})
})
.collect();
if errors.is_empty() {
Ok(Self { base, text })
} else {
Err(crate::Error::new(crate::ErrorKind::Incomplete(errors)))
}
}
}