Last active
June 18, 2024 10:02
-
-
Save kana-sama/97ecffdd69707ec78485ef27d040592d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "xmltest" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
futures = "0.3.30" | |
quick-xml = "0.32.0" | |
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread", "fs"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::path::PathBuf; | |
fn svgs() -> impl Iterator<Item = PathBuf> { | |
let content = std::path::Path::new("data").read_dir().expect("no data dir"); | |
let files = content.filter_map(Result::ok).map(|entry| entry.path()); | |
let svgs = files.filter(|path| path.extension().map_or(false, |ext| ext == "svg")); | |
return svgs; | |
} | |
fn sizes(path: PathBuf) -> Option<(PathBuf, String, String)> { | |
let mut reader = quick_xml::reader::Reader::from_file(&path).ok()?; | |
let mut buffer = Vec::new(); | |
let quick_xml::events::Event::Start(e) = reader.read_event_into(&mut buffer).ok()? else { | |
return None; | |
}; | |
let width = e.try_get_attribute("width").ok()??.unescape_value().ok()?.to_string(); | |
let height = e.try_get_attribute("height").ok()??.unescape_value().ok()?.to_string(); | |
return Some((path, width, height)); | |
} | |
#[tokio::main] | |
async fn main() { | |
futures::future::join_all(svgs().map(|path| async { | |
if let Ok(Some((path, width, height))) = tokio::task::spawn_blocking(|| sizes(path)).await { | |
println!("{}: {width}x{height}", path.display()); | |
} | |
})) | |
.await; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment