-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.rs
174 lines (157 loc) · 5.87 KB
/
mod.rs
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
174
use std::fmt::Display;
use oxvg_ast::{
element::Element,
visitor::{ContextFlags, Info, PrepareOutcome, Visitor},
};
use serde::Deserialize;
macro_rules! jobs {
($($name:ident: $job:ident$(< $($t:ty),* >)? $((is_default: $default:ident))?,)+) => {
$(mod $name;)+
$(pub use self::$name::$job;)+
#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase", bound = "E: Element")]
pub struct Jobs<E: Element> {
$($name: Option<$job $( < $($t),* >)?>),+
}
impl<E: Element> Default for Jobs<E> {
fn default() -> Self {
macro_rules! is_default {
($_default:ident) => { $_default };
() => { false };
}
Self {
$($name: if is_default!($($default)?) {
Some($job::default())
} else {
None
}),+
}
}
}
impl<E: Element> Jobs<E> {
/// Runs each job in the config, returning the number of non-skipped jobs
fn run_jobs(&mut self, element: &mut E, info: &Info) -> Result<usize, String> {
let mut count = 0;
$(if let Some(job) = self.$name.as_mut() {
if !job.start(element, info)?.contains(PrepareOutcome::skip) {
count += 1;
}
})+
Ok(count)
}
}
};
}
jobs! {
// Non default plugins
add_attributes_to_svg_element: AddAttributesToSVGElement,
add_classes_to_svg: AddClassesToSVG,
cleanup_list_of_values: CleanupListOfValues,
prefix_ids: PrefixIds<E>,
// Default plugins
remove_doctype: RemoveDoctype (is_default: true),
remove_xml_proc_inst: RemoveXMLProcInst (is_default: true),
remove_comments: RemoveComments (is_default: true),
remove_metadata: RemoveMetadata (is_default: true),
remove_editors_ns_data: RemoveEditorsNSData (is_default: true),
cleanup_attributes: CleanupAttributes (is_default: true),
merge_styles: MergeStyles (is_default: true),
inline_styles: InlineStyles<E> (is_default: true),
minify_styles: MinifyStyles (is_default: true),
cleanup_ids: CleanupIds<E> (is_default: true),
remove_useless_defs: RemoveUselessDefs (is_default: true),
cleanup_numeric_values: CleanupNumericValues (is_default: true),
convert_colors: ConvertColors (is_default: true),
remove_unknowns_and_defaults: RemoveUnknownsAndDefaults (is_default: true),
remove_non_inheritable_group_attrs: RemoveNonInheritableGroupAttrs (is_default: true),
remove_useless_stroke_and_fill: RemoveUselessStrokeAndFill (is_default: true),
remove_view_box: RemoveViewBox (is_default: true),
cleanup_enable_background: CleanupEnableBackground (is_default: true),
remove_hidden_elems: RemoveHiddenElems<E> (is_default: true),
remove_empty_text: RemoveEmptyText (is_default: true),
convert_shape_to_path: ConvertShapeToPath (is_default: true),
convert_ellipse_to_circle: ConvertEllipseToCircle (is_default: true),
move_elems_attrs_to_group: MoveElemsAttrsToGroup (is_default: true),
move_group_attrs_to_elems: MoveGroupAttrsToElems (is_default: true),
collapse_groups: CollapseGroups (is_default: true),
// NOTE: `apply_transforms` should be before `convert_path_data` in case the order is ever changed
apply_transforms: ApplyTransforms (is_default: true),
convert_path_data: ConvertPathData (is_default: true),
convert_transform: ConvertTransform (is_default: true),
remove_empty_attrs: RemoveEmptyAttrs (is_default: true),
remove_empty_containers: RemoveEmptyContainers (is_default: true),
merge_paths: MergePaths (is_default: true),
sort_attrs: SortAttrs (is_default: true),
sort_defs_children: SortDefsChildren (is_default: true),
remove_title: RemoveTitle (is_default: true),
remove_desc: RemoveDesc (is_default: true),
}
#[derive(Debug)]
pub enum Error {
Generic(String),
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Generic(s) => s.fmt(f),
}
}
}
impl std::error::Error for Error {}
impl<E: Element> Jobs<E> {
/// # Errors
/// When any job fails for the first time
pub fn run(self, root: &E::ParentChild, info: &Info) -> Result<(), Error> {
let Some(mut root_element) = <E as Element>::from_parent(root.clone()) else {
log::warn!("No elements found in the document, skipping");
return Ok(());
};
let mut jobs = self.clone();
let count = jobs
.run_jobs(&mut root_element, info)
.map_err(Error::Generic)?;
log::debug!("completed {count} jobs");
Ok(())
}
}
#[cfg(test)]
pub(crate) fn test_config_default_svg_comment(
config_json: &str,
comment: &str,
) -> anyhow::Result<String> {
test_config(
config_json,
Some(&format!(
r#"<svg xmlns="http://www.w3.org/2000/svg">
<!-- {comment} -->
test
</svg>"#
)),
)
}
#[cfg(test)]
pub(crate) fn test_config(config_json: &str, svg: Option<&str>) -> anyhow::Result<String> {
use oxvg_ast::{
implementations::markup5ever::{Element5Ever, Node5Ever},
parse::Node,
serialize,
};
let jobs: Jobs<Element5Ever> = serde_json::from_str(config_json)?;
let dom: Node5Ever = Node::parse(svg.unwrap_or(
r#"<svg xmlns="http://www.w3.org/2000/svg">
test
</svg>"#,
))?;
jobs.run(&dom, &Info::default())?;
serialize::Node::serialize_with_options(&dom, serialize::Options::new().pretty())
}
#[test]
fn test_jobs() -> anyhow::Result<()> {
test_config(
r#"{ "addAttributesToSvgElement": {
"attributes": { "foo": "bar" }
} }"#,
None,
)
.map(|_| ())
}