Skip to content

Commit 8babe6c

Browse files
committed
feat: #175 update cli to use updated parsing
1 parent 1e39dde commit 8babe6c

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ missing_docs = { level = "warn" }
77

88
[workspace.lints.clippy]
99
pedantic = { level = "warn", priority = -1 }
10-
# Too noisy and not really an issue
11-
must_use_candidate = "allow"
12-
return_self_not_must_use = "allow"
10+
# Willing to accept lossy conversions
11+
cast_lossless = "allow"
12+
# Willing to accept lossy conversions
13+
cast_possible_truncation = "allow"
14+
# dbg should only be used for debugging
15+
dbg_macro = "warn"
1316
# Not a priority for svgo parity
1417
float_cmp = "allow"
15-
dbg_macro = "warn"
18+
# Too noisy and not really an issue
19+
must_use_candidate = "allow"
1620

1721
[workspace.package]
1822
authors = ["Noah <[email protected]>"]

crates/oxvg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ clap = { workspace = true }
3232
etcetera = "0.10"
3333
ignore = "0.4"
3434
log = { workspace = true }
35+
roxmltree = { workspace = true }
3536
serde = { workspace = true }
3637
serde_json = { workspace = true }
3738
typed-arena = { workspace = true }

crates/oxvg/src/optimise.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use std::{
88
use anyhow::anyhow;
99
use ignore::{WalkBuilder, WalkState};
1010
use oxvg_ast::{
11-
implementations::{
12-
roxmltree::{parse, parse_file},
13-
shared::{Arena, Element, Ref},
14-
},
11+
arena::Allocator,
12+
node::Ref,
13+
parse::roxmltree::parse,
1514
serialize::{Indent, Node as _, Options},
1615
visitor::Info,
1716
};
@@ -64,7 +63,7 @@ impl RunCommand for Optimise {
6463
}
6564

6665
impl Optimise {
67-
fn handle_out<W: Write>(dom: Ref<'_>, wr: W) -> anyhow::Result<W> {
66+
fn handle_out<W: Write>(dom: Ref, wr: W) -> anyhow::Result<W> {
6867
Ok(dom.serialize_into(
6968
wr,
7069
Options {
@@ -74,17 +73,22 @@ impl Optimise {
7473
)?)
7574
}
7675

77-
fn handle_stdin<'arena>(&self, jobs: &Jobs, arena: Arena<'arena>) -> anyhow::Result<()> {
76+
fn handle_stdin(&self, jobs: &Jobs) -> anyhow::Result<()> {
7877
let mut source = String::new();
7978
std::io::stdin().read_to_string(&mut source)?;
80-
let dom = parse(&source, arena)?;
79+
let xml = roxmltree::Document::parse(&source).unwrap();
80+
let values = Allocator::new_values();
81+
let mut arena = Allocator::new_arena();
82+
let mut allocator = Allocator::new(&mut arena, &values);
83+
let dom = parse(&xml, &mut allocator)?;
8184

82-
let info: Info<'arena, Element<'arena>> = Info {
85+
let info = Info {
8386
path: None,
8487
multipass_count: 0,
85-
arena,
88+
allocator,
8689
};
87-
jobs.run(&dom, &info)?;
90+
jobs.run(dom, &info)
91+
.map_err(|e| anyhow::Error::msg(e.to_string()))?;
8892

8993
if let Some(output) = &self.output.as_ref().and_then(|o| {
9094
eprintln!("Warning: Using empty `-o,--output` with stdin will print to stdout, you can instead omit `-o,--output`.");
@@ -106,23 +110,22 @@ impl Optimise {
106110
Ok(())
107111
}
108112

109-
fn handle_file<'arena>(
110-
jobs: &Jobs,
111-
path: &PathBuf,
112-
output: Option<&PathBuf>,
113-
arena: Arena<'arena>,
114-
) -> anyhow::Result<()> {
115-
let file = std::fs::File::open(path)?;
116-
let input_size = file.metadata()?.len() as f64 / 1000.0;
117-
let dom = parse_file(path, arena)?;
118-
drop(file);
119-
120-
let info: Info<'arena, Element<'arena>> = Info {
113+
fn handle_file(jobs: &Jobs, path: &PathBuf, output: Option<&PathBuf>) -> anyhow::Result<()> {
114+
let file = std::fs::read_to_string(path)?;
115+
let input_size = file.len() as f64 / 1000.0;
116+
let xml = roxmltree::Document::parse(&file).unwrap();
117+
let values = Allocator::new_values();
118+
let mut arena = Allocator::new_arena();
119+
let mut allocator = Allocator::new(&mut arena, &values);
120+
let dom = parse(&xml, &mut allocator).unwrap();
121+
122+
let info: Info = Info {
121123
path: Some(path.clone()),
122124
multipass_count: 0,
123-
arena,
125+
allocator,
124126
};
125-
jobs.run(&dom, &info)?;
127+
jobs.run(dom, &info)
128+
.map_err(|e| anyhow::Error::msg(e.to_string()))?;
126129

127130
if let Some(output_path) = output {
128131
if let Some(parent) = output_path.parent() {
@@ -165,7 +168,6 @@ impl Optimise {
165168
.build_parallel()
166169
.run(|| {
167170
Box::new(move |path| {
168-
let arena = typed_arena::Arena::new();
169171
let Ok(path) = path else {
170172
return WalkState::Continue;
171173
};
@@ -179,12 +181,12 @@ impl Optimise {
179181
let Ok(output_path) = output_path(&path) else {
180182
return WalkState::Continue;
181183
};
182-
if let Err(err) = Self::handle_file(jobs, &path, output_path.as_ref(), &arena) {
184+
if let Err(err) = Self::handle_file(jobs, &path, output_path.as_ref()) {
183185
eprintln!(
184186
"{}: \x1b[31m{err}\x1b[0m",
185187
path.to_str().unwrap_or_default()
186188
);
187-
};
189+
}
188190
WalkState::Continue
189191
})
190192
});
@@ -198,8 +200,7 @@ impl Optimise {
198200
.first()
199201
.is_none_or(|path| path == &PathBuf::from_str(".").unwrap())
200202
{
201-
let arena = typed_arena::Arena::new();
202-
return self.handle_stdin(jobs, &arena);
203+
return self.handle_stdin(jobs);
203204
}
204205
if self.paths.is_empty() {
205206
return Err(anyhow!(

0 commit comments

Comments
 (0)