Skip to content

Instantly share code, notes, and snippets.

@ndugger
Last active January 6, 2018 07:21
Show Gist options
  • Save ndugger/4e3628d7ad36abb7c6da108dedc5bce0 to your computer and use it in GitHub Desktop.
Save ndugger/4e3628d7ad36abb7c6da108dedc5bce0 to your computer and use it in GitHub Desktop.
use std::ptr::null;
use std::any::Any;
use std::collections::HashMap;
use std::string::String;
use std::vec::Vec
struct Element {
tag: String,
properties: HashMap<String, Any>,
children: Vec<Element>,
node: Any
}
enum Children {
Element,
String
}
pub fn element (tag: String, properties: HashMap<String, Any>, children: Vec<Children>) -> Element {
if let children.first() = Children::String && children.len() == 1 {
let properties_with_text = properties.clone()
properties_with_text.insert("textContent", children.first())
Element {
tag,
properties: properties_with_text,
children: Vec::new(),
node: null()
}
}
else {
let mapped_children: Vec<Element> = children.iter().map(| child: Children | {
if let child = Children::String {
element(null(), null(), child)
}
else {
child
}
})
Element {
tag,
properties,
children: mapped_children,
node: null()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment