Move engine XML parsing to craft-xml

This commit is contained in:
Dylan M. Taylor committed 2026-11-13 07:39:10 -05:00
1 parent 81c7e48f34
commit a192ed0c59
4 files changed
+28 -23

No files matched your search

Generated
+1 -1
View File
@@ -5500,6 +5500,7 @@ dependencies = [
"craft-image-codecs",
"craft-print",
"craft-psd",
"craft-xml",
"exr",
"flate2",
"photocraft-algo",
@@ -5520,7 +5521,6 @@ dependencies = [
"photocraft-text",
"photocraft-vector",
"rayon",
"roxmltree 0.21.2",
"same-file",
"semver",
"serde",
+1 -3
View File
@@ -30,6 +30,7 @@ photocraft-algo = { workspace = true }
photocraft-text = { workspace = true }
photocraft-vector = { workspace = true }
photocraft-io = { workspace = true }
craft-xml = { workspace = true }
photocraft-psd = { workspace = true }
photocraft-format = { workspace = true }
photocraft-plugins = { workspace = true }
@@ -40,9 +41,6 @@ photocraft-formats = { workspace = true }
flate2 = { workspace = true }
craft-print = { workspace = true }
# Photoshop `.kys` shortcut sets (`edit.keyboardShortcuts` `importKys`); already in the tree
# through photocraft-ui-egui and usvg.
roxmltree = "0.21"
# Release tags use the same semantic versions as Cargo.
semver = "1"
+7 -3
View File
@@ -33,9 +33,13 @@ fn check_packet(raw: &str) -> Result<()> {
if raw.contains('\0') {
return Err(bad("`rawData` can't contain NUL characters"));
}
let xml = photocraft_io::xml::parse(raw, roxmltree::ParsingOptions::default()).map_err(|e| bad(format!("`rawData` isn't well-formed XML: {e}")))?;
let root = xml.root_element().tag_name();
let xmp = matches!((root.namespace(), root.name()), (Some("adobe:ns:meta/"), "xmpmeta") | (Some("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), "RDF"));
let namespaces = [("x", "adobe:ns:meta/"), ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")];
let canonical = craft_xml::Canonical::new(&namespaces);
let options = craft_xml::Options { doctype: craft_xml::Doctype::Reject, ..craft_xml::Options::canonical(&canonical) }
.with_limits(craft_xml::Limits { max_input: MAX_LAYER_XMP, max_depth: 512, ..craft_xml::Limits::default() })
.strict();
let xml = options.parse(raw.as_bytes()).map_err(|e| bad(format!("`rawData` isn't well-formed XML: {e}")))?;
let xmp = matches!(xml.name.as_str(), "x:xmpmeta" | "rdf:RDF");
if !xmp {
return Err(bad("`rawData` must be an XMP packet: its root element is x:xmpmeta or rdf:RDF"));
}
+19 -16
View File
@@ -68,34 +68,37 @@ pub fn parse(text: &str) -> Result<KysSet, String> {
return Err("not a keyboard shortcut set (.kys): larger than 4 MiB".into());
}
let text = text.trim_start_matches('\u{feff}');
let doc = photocraft_io::xml::parse(text, roxmltree::ParsingOptions::default()).map_err(|e| format!("not a keyboard shortcut set (.kys): {e}"))?;
let root = doc.root_element();
if root.tag_name().name() != "photoshop-keyboard-shortcuts" {
let options = craft_xml::Options {
limits: craft_xml::Limits { max_input: MAX_BYTES, max_depth: 512, ..craft_xml::Limits::default() },
doctype: craft_xml::Doctype::Reject,
strict: true,
..craft_xml::Options::default()
};
let doc = options.parse(text.as_bytes()).map_err(|e| format!("not a keyboard shortcut set (.kys): {e}"))?;
if doc.name != "photoshop-keyboard-shortcuts" {
return Err("not a keyboard shortcut set (.kys): no <photoshop-keyboard-shortcuts> root".into());
}
let name = root.attribute("filename").map(filename_to_name).filter(|n| !n.is_empty()).map(str::to_string);
let name = doc.attr("filename").map(filename_to_name).filter(|n| !n.is_empty()).map(str::to_string);
let mut set = KysSet { name, ..Default::default() };
for node in root.children().filter(|n| n.is_element()) {
match node.tag_name().name() {
for node in doc.elements() {
match node.name.as_str() {
"command" => {
let Some(name) = node.attribute("name").map(str::trim).filter(|n| !n.is_empty()) else { continue };
let shortcuts: Vec<String> = node
.children()
.filter(|n| n.is_element() && n.tag_name().name() == "shortcut")
.filter_map(|n| n.text().map(str::trim).filter(|t| !t.is_empty()).map(str::to_string))
.collect();
let Some(name) = node.attr("name").map(str::trim).filter(|n| !n.is_empty()) else { continue };
let shortcuts: Vec<String> = node.children("shortcut").map(|n| n.text()).map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect();
if !shortcuts.is_empty() {
let id = node.attribute(PC_ID).map(str::trim).filter(|i| !i.is_empty()).map(str::to_string);
let id = node.attr(PC_ID).map(str::trim).filter(|i| !i.is_empty()).map(str::to_string);
set.commands.push(KysCommand { name: name.to_string(), shortcuts, id });
}
}
"tool" => {
let key = node.text().map(str::trim).filter(|t| !t.is_empty());
let name = node.attribute("name").map(str::trim).filter(|n| !n.is_empty());
let text = node.text();
let key = text.trim();
let key = (!key.is_empty()).then_some(key);
let name = node.attr("name").map(str::trim).filter(|n| !n.is_empty());
if let Some(key) = key {
set.tool_keys += 1;
if let Some(name) = name {
let id = node.attribute(PC_ID).map(str::trim).filter(|i| !i.is_empty()).map(str::to_string);
let id = node.attr(PC_ID).map(str::trim).filter(|i| !i.is_empty()).map(str::to_string);
set.tools.push(KysCommand { name: name.to_string(), shortcuts: vec![key.to_string()], id });
}
}