38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
//! SVG trees as structure for the R3 oracles: namespace, local name, attributes by qualified
|
|
//! name (sorted), children with text, the shape the TypeScript generators freeze.
|
|
#![allow(dead_code, clippy::unwrap_used)]
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use faunosteo_core::anatomy::svg::serialize::serialize;
|
|
use faunosteo_core::anatomy::svg::tree::{SVG_NS, parse_svg};
|
|
use faunosteo_core::anatomy::svg::{Element, Node};
|
|
use serde_json::{Value, json};
|
|
|
|
pub fn tree_value(element: &Element) -> Value {
|
|
let attrs: BTreeMap<&str, &str> = element
|
|
.attrs
|
|
.iter()
|
|
.map(|a| (a.qname.as_str(), a.value.as_str()))
|
|
.collect();
|
|
let children: Vec<Value> = element
|
|
.children
|
|
.iter()
|
|
.map(|n| match n {
|
|
Node::Element(e) => tree_value(e),
|
|
Node::Text(t) => json!({ "text": t }),
|
|
})
|
|
.collect();
|
|
json!({
|
|
"ns": element.namespace.as_deref().unwrap_or(SVG_NS),
|
|
"name": element.local_name(),
|
|
"attrs": attrs,
|
|
"children": children,
|
|
})
|
|
}
|
|
|
|
/// The element serialised and read again by the core, as a tree.
|
|
pub fn reread(element: &Element) -> Value {
|
|
tree_value(&parse_svg(&serialize(element)).unwrap())
|
|
}
|