From b304c0790f9e8ab6ad0b15a8dcf7bb05f45180f9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 03:13:43 +0000 Subject: [PATCH 1/3] Generalize XFL document schema views --- crates/xfl/src/document.rs | 118 ++++++++++++++++++++++++++++--------- 1 file changed, 90 insertions(+), 28 deletions(-) diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index 8cde65513..33b047744 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -3,6 +3,7 @@ //! This layer recognizes the document, stage, scene, layer and keyframe fields defined by XFL. //! It does not construct a consumer's animation model. More detailed payloads remain available //! through [`Document::root`] for the application adapter. +use crate::shape::XflElement; use craft_xml::{Doctype, El, Error as XmlError, Limits, Local, Options, Pat, UnknownEntity}; use std::borrow::Cow; @@ -104,36 +105,60 @@ pub struct FrameModel { } /// A timeline (`DOMTimeline`) listed under `DOMDocument/timelines`. -#[derive(Clone, Copy, Debug)] -pub struct Timeline<'a> { - element: &'a El, +#[derive(Debug)] +pub struct Timeline<'a, E: XflElement = El> { + element: &'a E, +} + +impl Copy for Timeline<'_, E> {} + +impl Clone for Timeline<'_, E> { + fn clone(&self) -> Self { + *self + } } /// A native `DOMLayer` element. -#[derive(Clone, Copy, Debug)] -pub struct Layer<'a> { - element: &'a El, +#[derive(Debug)] +pub struct Layer<'a, E: XflElement = El> { + element: &'a E, +} + +impl Copy for Layer<'_, E> {} + +impl Clone for Layer<'_, E> { + fn clone(&self) -> Self { + *self + } } /// A native `DOMFrame` element. -#[derive(Clone, Copy, Debug)] -pub struct Frame<'a> { - element: &'a El, +#[derive(Debug)] +pub struct Frame<'a, E: XflElement = El> { + element: &'a E, } -fn attr_last<'a>(element: &'a El, name: &str) -> Option<&'a str> { - element.attrs.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) +impl Copy for Frame<'_, E> {} + +impl Clone for Frame<'_, E> { + fn clone(&self) -> Self { + *self + } } -fn number_f32(element: &El, name: &str) -> Option { +fn attr_last<'a>(element: &'a impl XflElement, name: &str) -> Option<&'a str> { + element.attr(name) +} + +fn number_f32(element: &impl XflElement, name: &str) -> Option { attr_last(element, name).and_then(|value| value.trim().parse::().ok()).filter(|value| value.is_finite()) } -fn number_u32(element: &El, name: &str) -> Option { +fn number_u32(element: &impl XflElement, name: &str) -> Option { attr_last(element, name).and_then(|value| value.trim().parse::().ok()) } -fn boolean(element: &El, name: &str) -> Option { +fn boolean(element: &impl XflElement, name: &str) -> Option { attr_last(element, name).map(|value| value.trim() == "true") } @@ -187,7 +212,7 @@ impl Document { let profile = self .root .elements() - .find(|element| Local("flash_profile").matches(&element.name) && boolean(element, "current") == Some(true)) + .find(|element| Local("flash_profile").matches(&element.name) && boolean(*element, "current") == Some(true)) .or_else(|| self.root.elements().find(|element| Local("flash_profile").matches(&element.name))) .unwrap_or(&self.root); let properties = profile.child(Local("PublishFlashProperties")).unwrap_or(profile); @@ -237,23 +262,28 @@ impl Document { } } -impl<'a> Timeline<'a> { +impl<'a, E: XflElement> Timeline<'a, E> { + /// Creates a borrowed XFL timeline view over a consumer's compatible XML element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + pub fn name(self) -> Option<&'a str> { attr_last(self.element, "name") } /// Raw timeline XML for application-specific payload conversion. - pub fn element(self) -> &'a El { + pub fn element(self) -> &'a E { self.element } /// Layers in native order. Non-`DOMLayer` extension nodes are skipped. - pub fn layers(self) -> impl Iterator> { + pub fn layers(self) -> impl Iterator> { self.element - .child(Local("layers")) + .child("layers") .into_iter() - .flat_map(El::elements) - .filter(|element| Local("DOMLayer").matches(&element.name)) + .flat_map(|element| element.elements()) + .filter(|element| element.local_name() == "DOMLayer") .map(|element| Layer { element }) } @@ -262,7 +292,12 @@ impl<'a> Timeline<'a> { } } -impl<'a> Layer<'a> { +impl<'a, E: XflElement> Layer<'a, E> { + /// Creates a borrowed XFL layer view over a consumer's compatible XML element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + pub fn name(self) -> Option<&'a str> { attr_last(self.element, "name") } @@ -296,12 +331,16 @@ impl<'a> Layer<'a> { } /// Frames in native order. Non-`DOMFrame` extension nodes are skipped. - pub fn frames(self) -> impl Iterator> { + pub fn element(self) -> &'a E { self.element - .child(Local("frames")) + } + + pub fn frames(self) -> impl Iterator> { + self.element + .child("frames") .into_iter() - .flat_map(El::elements) - .filter(|element| Local("DOMFrame").matches(&element.name)) + .flat_map(|element| element.elements()) + .filter(|element| element.local_name() == "DOMFrame") .map(|element| Frame { element }) } @@ -325,7 +364,12 @@ impl<'a> Layer<'a> { } } -impl<'a> Frame<'a> { +impl<'a, E: XflElement> Frame<'a, E> { + /// Creates a borrowed XFL frame view over a consumer's compatible XML element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + pub fn index(self) -> Option { number_u32(self.element, "index") } @@ -347,7 +391,7 @@ impl<'a> Frame<'a> { } /// Raw frame XML for application-specific shape, action, sound and tween conversion. - pub fn element(self) -> &'a El { + pub fn element(self) -> &'a E { self.element } @@ -408,6 +452,24 @@ mod tests { assert_eq!(frame.element().child("elements").unwrap().child("Shape").unwrap().name, "Shape"); } + #[test] + fn borrowed_timeline_views_accept_compatible_element_adapters() { + let root = craft_xml::parse( + br##""##, + ) + .unwrap(); + let timeline_element = root.child(Local("timelines")).unwrap().child(Local("DOMTimeline")).unwrap(); + let timeline = Timeline::new(timeline_element); + assert_eq!(timeline.name(), Some("Scene")); + let layer = timeline.layers().next().unwrap(); + assert_eq!(layer.name(), Some("Layer")); + assert_eq!(layer.color(), Some("#abcdef")); + assert_eq!(layer.visible(), Some(false)); + let frame = layer.frames().next().unwrap(); + assert_eq!((frame.index(), frame.duration()), (Some(3), Some(2))); + assert_eq!((frame.name(), frame.label_type(), frame.tween_type()), (Some("Intro"), Some("name"), Some("motion"))); + } + #[test] fn uses_last_duplicate_attribute_like_the_legacy_xfl_adapter() { let document = Document::parse(br##""##).unwrap(); From a9c1d06a0fb183e5d19ef97dd499a53b01d54686 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 03:24:35 +0000 Subject: [PATCH 2/3] Test XFL document views with app adapter --- crates/xfl/src/document.rs | 73 +++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index 33b047744..d82d52f2d 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -454,11 +454,74 @@ mod tests { #[test] fn borrowed_timeline_views_accept_compatible_element_adapters() { - let root = craft_xml::parse( - br##""##, - ) - .unwrap(); - let timeline_element = root.child(Local("timelines")).unwrap().child(Local("DOMTimeline")).unwrap(); + #[derive(Debug)] + struct AppElement { + name: String, + attributes: Vec<(String, String)>, + children: Vec, + } + + impl XflElement for AppElement { + type Elements<'a> = std::slice::Iter<'a, Self>; + + fn local_name(&self) -> &str { + self.name.rsplit(':').next().unwrap_or(&self.name) + } + + fn attr(&self, name: &str) -> Option<&str> { + self.attributes.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) + } + + fn elements(&self) -> Self::Elements<'_> { + self.children.iter() + } + } + + fn element(name: &str, attributes: &[(&str, &str)], children: Vec) -> AppElement { + AppElement { + name: name.to_string(), + attributes: attributes.iter().map(|(key, value)| ((*key).to_string(), (*value).to_string())).collect(), + children, + } + } + + let root = element( + "x:DOMDocument", + &[], + vec![element( + "x:timelines", + &[], + vec![element( + "x:DOMTimeline", + &[("name", "Old"), ("name", "Scene")], + vec![element( + "x:layers", + &[], + vec![element( + "x:DOMLayer", + &[("name", "Old"), ("name", "Layer"), ("color", "#abcdef"), ("visible", "false")], + vec![element( + "x:frames", + &[], + vec![element( + "x:DOMFrame", + &[ + ("index", "999"), + ("index", "3"), + ("duration", "2"), + ("name", "Intro"), + ("labelType", "name"), + ("tweenType", "motion"), + ], + vec![], + )], + )], + )], + )], + )], + )], + ); + let timeline_element = root.child("timelines").unwrap().child("DOMTimeline").unwrap(); let timeline = Timeline::new(timeline_element); assert_eq!(timeline.name(), Some("Scene")); let layer = timeline.layers().next().unwrap(); From c0ff073356815044a628515886c0fe9d03fc7327 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 03:29:34 +0000 Subject: [PATCH 3/3] feat(xfl): expose shared color attribute parser --- crates/xfl/src/shape.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index bcd3e9373..ddd33fb8f 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -108,8 +108,15 @@ fn boolean(element: &impl XflElement, name: &str) -> Option { /// Parses XFL's hexadecimal color and alpha attributes. Missing or malformed colors use black; /// missing, malformed, or non-finite alpha uses full opacity, matching the legacy importer. pub fn color(element: &impl XflElement, color_attribute: &str, alpha_attribute: &str) -> Color { - let rgb = element.attr(color_attribute).and_then(|value| u32::from_str_radix(value.trim().trim_start_matches('#'), 16).ok()).unwrap_or(0); - let alpha = number(element, alpha_attribute).unwrap_or(1.0).clamp(0.0, 1.0); + color_from_attributes(element.attr(color_attribute), element.attr(alpha_attribute)) +} + +/// Parses native color and alpha strings after a consumer has read them from its own element +/// adapter. This keeps wire-value interpretation shared even when an application already has a +/// borrowed schema view for the containing element. +pub fn color_from_attributes(color: Option<&str>, alpha: Option<&str>) -> Color { + let rgb = color.and_then(|value| u32::from_str_radix(value.trim().trim_start_matches('#'), 16).ok()).unwrap_or(0); + let alpha = alpha.and_then(|value| value.trim().parse::().ok()).filter(|value| value.is_finite()).unwrap_or(1.0).clamp(0.0, 1.0); Color { rgb, alpha } } @@ -534,6 +541,14 @@ mod tests { assert_eq!(fill.color_value(), Color { rgb: 0, alpha: 1.0 }); } + #[test] + fn color_attribute_values_keep_native_defaults_and_clamping() { + assert_eq!(color_from_attributes(Some(" #123456 "), Some("0.4")), Color { rgb: 0x123456, alpha: 0.4 }); + assert_eq!(color_from_attributes(Some("invalid"), Some("NaN")), Color { rgb: 0, alpha: 1.0 }); + assert_eq!(color_from_attributes(Some("#abcdef"), Some("2")), Color { rgb: 0xabcdef, alpha: 1.0 }); + assert_eq!(color_from_attributes(None, Some("-1")), Color { rgb: 0, alpha: 0.0 }); + } + #[test] fn style_table_views_keep_the_legacy_first_child_and_index_behavior() { let root = parse(b"");