From b33a81e40620b8b3eaeeec9d0e0b34ca5958dead Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 28 Sep 2022 17:54:39 +0200 Subject: Add support for turning mdast to hast --- tests/test_utils/hast.rs | 279 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 279 insertions(+) create mode 100644 tests/test_utils/hast.rs (limited to 'tests/test_utils/hast.rs') diff --git a/tests/test_utils/hast.rs b/tests/test_utils/hast.rs new file mode 100644 index 0000000..4adf0ca --- /dev/null +++ b/tests/test_utils/hast.rs @@ -0,0 +1,279 @@ +#![allow(dead_code)] + +// ^-- fix later + +extern crate alloc; +extern crate micromark; +use alloc::{ + fmt, + string::{String, ToString}, + vec::Vec, +}; +use micromark::{mdast::AttributeContent, unist::Position}; + +/// Nodes. +#[derive(Clone, PartialEq)] +pub enum Node { + /// Root. + Root(Root), + /// Element. + Element(Element), + /// Document type. + Doctype(Doctype), + /// Comment. + Comment(Comment), + /// Text. + Text(Text), + + // MDX being passed through. + /// MDX: JSX element. + MdxJsxElement(MdxJsxElement), + /// MDX.js ESM. + MdxjsEsm(MdxjsEsm), + // MDX: expression. + MdxExpression(MdxExpression), +} + +impl fmt::Debug for Node { + // Debug the wrapped struct. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Node::Root(x) => write!(f, "{:?}", x), + Node::Element(x) => write!(f, "{:?}", x), + Node::Doctype(x) => write!(f, "{:?}", x), + Node::Comment(x) => write!(f, "{:?}", x), + Node::Text(x) => write!(f, "{:?}", x), + Node::MdxJsxElement(x) => write!(f, "{:?}", x), + Node::MdxExpression(x) => write!(f, "{:?}", x), + Node::MdxjsEsm(x) => write!(f, "{:?}", x), + } + } +} + +fn children_to_string(children: &[Node]) -> String { + children.iter().map(ToString::to_string).collect() +} + +impl ToString for Node { + fn to_string(&self) -> String { + match self { + // Parents. + Node::Root(x) => children_to_string(&x.children), + Node::Element(x) => children_to_string(&x.children), + Node::MdxJsxElement(x) => children_to_string(&x.children), + // Literals. + Node::Comment(x) => x.value.clone(), + Node::Text(x) => x.value.clone(), + Node::MdxExpression(x) => x.value.clone(), + Node::MdxjsEsm(x) => x.value.clone(), + // Voids. + Node::Doctype(_) => "".to_string(), + } + } +} + +impl Node { + #[must_use] + pub fn children(&self) -> Option<&Vec> { + match self { + // Parent. + Node::Root(x) => Some(&x.children), + Node::Element(x) => Some(&x.children), + Node::MdxJsxElement(x) => Some(&x.children), + // Non-parent. + _ => None, + } + } + + pub fn children_mut(&mut self) -> Option<&mut Vec> { + match self { + // Parent. + Node::Root(x) => Some(&mut x.children), + Node::Element(x) => Some(&mut x.children), + Node::MdxJsxElement(x) => Some(&mut x.children), + // Non-parent. + _ => None, + } + } + + pub fn position(&self) -> Option<&Position> { + match self { + Node::Root(x) => x.position.as_ref(), + Node::Element(x) => x.position.as_ref(), + Node::Doctype(x) => x.position.as_ref(), + Node::Comment(x) => x.position.as_ref(), + Node::Text(x) => x.position.as_ref(), + Node::MdxJsxElement(x) => x.position.as_ref(), + Node::MdxExpression(x) => x.position.as_ref(), + Node::MdxjsEsm(x) => x.position.as_ref(), + } + } + + pub fn position_mut(&mut self) -> Option<&mut Position> { + match self { + Node::Root(x) => x.position.as_mut(), + Node::Element(x) => x.position.as_mut(), + Node::Doctype(x) => x.position.as_mut(), + Node::Comment(x) => x.position.as_mut(), + Node::Text(x) => x.position.as_mut(), + Node::MdxJsxElement(x) => x.position.as_mut(), + Node::MdxExpression(x) => x.position.as_mut(), + Node::MdxjsEsm(x) => x.position.as_mut(), + } + } + + pub fn position_set(&mut self, position: Option) { + match self { + Node::Root(x) => x.position = position, + Node::Element(x) => x.position = position, + Node::Doctype(x) => x.position = position, + Node::Comment(x) => x.position = position, + Node::Text(x) => x.position = position, + Node::MdxJsxElement(x) => x.position = position, + Node::MdxExpression(x) => x.position = position, + Node::MdxjsEsm(x) => x.position = position, + } + } +} + +/// Document. +/// +/// ```html +/// > | a +/// ^ +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct Root { + // Parent. + /// Content model. + pub children: Vec, + /// Positional info. + pub position: Option, +} + +/// Document type. +/// +/// ```html +/// > | +/// ^^^^^^^^^^^^^^^ +/// ``` +// To do: clone. +#[derive(Clone, Debug, PartialEq)] +pub struct Element { + pub tag_name: String, + pub properties: Vec<(String, PropertyValue)>, + // Parent. + pub children: Vec, + /// Positional info. + pub position: Option, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum PropertyItem { + Number(f32), + String(String), +} + +#[derive(Clone, Debug, PartialEq)] +pub enum PropertyValue { + Number(f32), + Boolean(bool), + String(String), + CommaSeparated(Vec), + SpaceSeparated(Vec), +} + +/// Document type. +/// +/// ```html +/// > | +/// ^^^^^^^^^^^^^^^ +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Doctype { + // Void. + /// Positional info. + pub position: Option, +} + +/// Comment. +/// +/// ```html +/// > | +/// ^^^^^^^^^^ +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Comment { + // Text. + /// Content model. + pub value: String, + /// Positional info. + pub position: Option, +} + +/// Text. +/// +/// ```html +/// > | a +/// ^ +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Text { + // Text. + /// Content model. + pub value: String, + /// Positional info. + pub position: Option, +} + +/// MDX: JSX element. +/// +/// ```markdown +/// > | +/// ^^^^^ +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct MdxJsxElement { + // Parent. + /// Content model. + pub children: Vec, + /// Positional info. + pub position: Option, + // JSX element. + /// Name. + /// + /// Fragments have no name. + pub name: Option, + /// Attributes. + pub attributes: Vec, +} + +/// MDX: expression. +/// +/// ```markdown +/// > | {a} +/// ^^^ +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct MdxExpression { + // Literal. + /// Content model. + pub value: String, + /// Positional info. + pub position: Option, +} + +/// MDX: ESM. +/// +/// ```markdown +/// > | import a from 'b' +/// ^^^^^^^^^^^^^^^^^ +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct MdxjsEsm { + // Literal. + /// Content model. + pub value: String, + /// Positional info. + pub position: Option, +} -- cgit