Skip to content

Commit

Permalink
refactor(mrml-core): move fragment element behind non-default feature
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Ellis <[email protected]>
  • Loading branch information
JadedBlueEyes committed Jun 27, 2024
1 parent 850661f commit 3fb0081
Show file tree
Hide file tree
Showing 28 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/mrml-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ travis-ci = { repository = "jdrouet/mrml", branch = "main" }
[features]
default = ["json", "parse", "print", "render"]
json = ["dep:mrml-json-macros", "dep:serde", "dep:serde_json", "indexmap/serde"]
fragment = []
parse = ["dep:xmlparser", "dep:thiserror"]
print = ["dep:enum_dispatch"]
render = ["dep:enum-as-inner", "dep:thiserror"]
Expand Down
1 change: 1 addition & 0 deletions packages/mrml-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub mod text;
// Only used to ignore the comments at the root level
mod root;

#[cfg(feature = "fragment")]
pub mod fragment;
mod helper;

Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_accordion/children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::comment::Comment;
#[cfg(feature = "fragment")]
use crate::fragment::Fragment;
use crate::mj_accordion_element::MjAccordionElement;

Expand All @@ -8,6 +9,7 @@ use crate::mj_accordion_element::MjAccordionElement;
#[cfg_attr(feature = "print", enum_dispatch::enum_dispatch)]
pub enum MjAccordionChild {
Comment(Comment),
#[cfg(feature = "fragment")]
Fragment(Fragment<Self>),
MjAccordionElement(MjAccordionElement),
}
7 changes: 7 additions & 0 deletions packages/mrml-core/src/mj_accordion/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const STYLE: &str = r#"noinput.mj-accordion-checkbox { display: block! important
"#;

impl<'root> Renderer<'root, MjAccordion, ()> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjAccordionChild> {
fn folder<'root>(
c: &'root MjAccordionChild,
Expand All @@ -44,6 +45,11 @@ impl<'root> Renderer<'root, MjAccordion, ()> {
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjAccordionChild> {
self.element.children.iter()
}

fn update_header(&self, header: &mut VariableHeader) {
let font_families = self.attribute("font-family");
header.maybe_add_font_families(font_families);
Expand Down Expand Up @@ -151,6 +157,7 @@ impl<'render, 'root: 'render> Renderable<'render, 'root> for MjAccordionChild {
) -> Box<dyn Render<'root> + 'render> {
match self {
Self::MjAccordionElement(elt) => elt.renderer(context),
#[cfg(feature = "fragment")]
Self::Fragment(elt) => elt.renderer(context),
Self::Comment(elt) => elt.renderer(context),
}
Expand Down
4 changes: 4 additions & 0 deletions packages/mrml-core/src/mj_body/children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::comment::Comment;
#[cfg(feature = "fragment")]
use crate::fragment::Fragment;
use crate::mj_accordion::MjAccordion;
use crate::mj_button::MjButton;
Expand Down Expand Up @@ -28,6 +29,7 @@ use crate::text::Text;
#[cfg_attr(feature = "print", enum_dispatch::enum_dispatch)]
pub enum MjBodyChild {
Comment(Comment),
#[cfg(feature = "fragment")]
Fragment(Fragment<Self>),
MjAccordion(MjAccordion),
MjButton(MjButton),
Expand Down Expand Up @@ -55,6 +57,7 @@ impl<'render, 'root: 'render> Renderable<'render, 'root> for MjBodyChild {
fn is_raw(&self) -> bool {
match self {
Self::Comment(elt) => elt.is_raw(),
#[cfg(feature = "fragment")]
Self::Fragment(elt) => elt.is_raw(),
Self::MjAccordion(elt) => elt.is_raw(),
Self::MjButton(elt) => elt.is_raw(),
Expand Down Expand Up @@ -84,6 +87,7 @@ impl<'render, 'root: 'render> Renderable<'render, 'root> for MjBodyChild {
) -> Box<dyn Render<'root> + 'render> {
match self {
Self::Comment(elt) => elt.renderer(context),
#[cfg(feature = "fragment")]
Self::Fragment(elt) => elt.renderer(context),
Self::MjAccordion(elt) => elt.renderer(context),
Self::MjButton(elt) => elt.renderer(context),
Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_body/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl<'opts> ParseElement<MjBodyChild> for MrmlParser<'opts> {
MJ_TABLE => Ok(MjBodyChild::MjTable(self.parse(cursor, tag)?)),
MJ_TEXT => Ok(MjBodyChild::MjText(self.parse(cursor, tag)?)),
MJ_WRAPPER => Ok(MjBodyChild::MjWrapper(self.parse(cursor, tag)?)),
#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjBodyChild::Fragment(self.parse(cursor, tag)?)),
_ => Ok(MjBodyChild::Node(self.parse(cursor, tag)?)),
}
Expand Down Expand Up @@ -149,6 +150,7 @@ impl AsyncParseElement<MjBodyChild> for AsyncMrmlParser {
MJ_TABLE => Ok(MjBodyChild::MjTable(self.async_parse(cursor, tag).await?)),
MJ_TEXT => Ok(MjBodyChild::MjText(self.async_parse(cursor, tag).await?)),
MJ_WRAPPER => Ok(MjBodyChild::MjWrapper(self.async_parse(cursor, tag).await?)),
#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjBodyChild::Fragment(self.async_parse(cursor, tag).await?)),
_ => Ok(MjBodyChild::Node(self.async_parse(cursor, tag).await?)),
}
Expand Down
6 changes: 6 additions & 0 deletions packages/mrml-core/src/mj_body/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::helper::size::Pixel;
use crate::prelude::render::*;

impl<'root> Renderer<'root, MjBody, ()> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
fn folder<'root>(c: &'root MjBodyChild) -> Box<dyn Iterator<Item = &MjBodyChild> + 'root> {
match c {
Expand All @@ -15,6 +16,11 @@ impl<'root> Renderer<'root, MjBody, ()> {
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
self.element.children.iter()
}

fn get_width(&self) -> Option<Pixel> {
self.attribute("width")
.and_then(|value| Pixel::try_from(value).ok())
Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_carousel/children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::comment::Comment;
#[cfg(feature = "fragment")]
use crate::fragment::Fragment;
use crate::mj_carousel_image::MjCarouselImage;

Expand All @@ -8,6 +9,7 @@ use crate::mj_carousel_image::MjCarouselImage;
#[cfg_attr(feature = "print", enum_dispatch::enum_dispatch)]
pub enum MjCarouselChild {
Comment(Comment),
#[cfg(feature = "fragment")]
Fragment(Fragment<Self>),
MjCarouselImage(MjCarouselImage),
}
Expand Down
7 changes: 7 additions & 0 deletions packages/mrml-core/src/mj_carousel/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ impl<'render, 'root: 'render> Renderable<'render, 'root> for MjCarouselChild {
) -> Box<dyn Render<'root> + 'render> {
match self {
Self::MjCarouselImage(elt) => elt.renderer(context),
#[cfg(feature = "fragment")]
Self::Fragment(elt) => elt.renderer(context),
Self::Comment(elt) => elt.renderer(context),
}
Expand All @@ -25,6 +26,8 @@ struct MjCarouselExtra {
}

impl<'root> Renderer<'root, MjCarousel, MjCarouselExtra> {
#[cfg(feature = "fragment")]

fn children_iter(&self) -> impl Iterator<Item = &MjCarouselChild> {
fn folder<'root>(
c: &'root MjCarouselChild,
Expand All @@ -37,6 +40,10 @@ impl<'root> Renderer<'root, MjCarousel, MjCarouselExtra> {
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjCarouselChild> {
self.element.children.iter()
}
fn get_thumbnails_width(&self) -> Pixel {
let count = self.children_iter().count();
if count == 0 {
Expand Down
7 changes: 7 additions & 0 deletions packages/mrml-core/src/mj_column/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct MjColumnExtra<'a> {
}

impl<'root> Renderer<'root, MjColumn, MjColumnExtra<'root>> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
fn folder<'root>(c: &'root MjBodyChild) -> Box<dyn Iterator<Item = &MjBodyChild> + 'root> {
match c {
Expand All @@ -18,6 +19,12 @@ impl<'root> Renderer<'root, MjColumn, MjColumnExtra<'root>> {
}
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
self.element.children.iter()
}

fn current_width(&self) -> Option<Pixel> {
let parent_width = self.container_width.as_ref()?;
let non_raw_siblings = self.non_raw_siblings();
Expand Down
6 changes: 6 additions & 0 deletions packages/mrml-core/src/mj_group/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::mj_body::MjBodyChild;
use crate::prelude::render::*;

impl<'root> Renderer<'root, MjGroup, ()> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
fn folder<'root>(c: &'root MjBodyChild) -> Box<dyn Iterator<Item = &MjBodyChild> + 'root> {
match c {
Expand All @@ -16,6 +17,11 @@ impl<'root> Renderer<'root, MjGroup, ()> {
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
self.element.children.iter()
}

fn current_width(&self) -> Pixel {
let parent_width = self.container_width.as_ref().unwrap();
let non_raw_siblings = self.non_raw_siblings();
Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_head/children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::comment::Comment;
#[cfg(feature = "fragment")]
use crate::fragment::Fragment;
use crate::mj_attributes::MjAttributes;
use crate::mj_breakpoint::MjBreakpoint;
Expand All @@ -16,6 +17,7 @@ use crate::mj_title::MjTitle;
#[cfg_attr(feature = "render", derive(enum_as_inner::EnumAsInner))]
pub enum MjHeadChild {
Comment(Comment),
#[cfg(feature = "fragment")]
Fragment(Fragment<Self>),
MjAttributes(MjAttributes),
MjBreakpoint(MjBreakpoint),
Expand Down
6 changes: 6 additions & 0 deletions packages/mrml-core/src/mj_head/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl MjHead {
.last()
}

#[cfg(feature = "fragment")]
pub fn children(&self) -> Vec<&MjHeadChild> {
fn folder<'root>(
mut acc: Vec<&'root MjHeadChild>,
Expand All @@ -93,4 +94,9 @@ impl MjHead {
}
self.children.iter().fold(Vec::new(), folder)
}

#[cfg(not(feature = "fragment"))]
pub fn children(&self) -> Vec<&MjHeadChild> {
self.children.iter().collect()
}
}
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_head/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl<'opts> ParseElement<MjHeadChild> for MrmlParser<'opts> {
MJ_RAW => self.parse(cursor, tag).map(MjHeadChild::MjRaw),
MJ_STYLE => self.parse(cursor, tag).map(MjHeadChild::MjStyle),
MJ_TITLE => self.parse(cursor, tag).map(MjHeadChild::MjTitle),
#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjHeadChild::Fragment(self.parse(cursor, tag)?)),
_ => Err(Error::UnexpectedElement(tag.into())),
}
Expand Down Expand Up @@ -161,6 +162,7 @@ impl AsyncParseElement<MjHeadChild> for AsyncMrmlParser {
.await
.map(MjHeadChild::MjTitle),

#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjHeadChild::Fragment(self.async_parse(cursor, tag).await?)),
_ => Err(Error::UnexpectedElement(tag.into())),
}
Expand Down
6 changes: 6 additions & 0 deletions packages/mrml-core/src/mj_hero/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::mj_body::MjBodyChild;
use crate::prelude::render::*;

impl<'root> Renderer<'root, MjHero, ()> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
fn folder<'root>(c: &'root MjBodyChild) -> Box<dyn Iterator<Item = &MjBodyChild> + 'root> {
match c {
Expand All @@ -16,6 +17,11 @@ impl<'root> Renderer<'root, MjHero, ()> {
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjBodyChild> {
self.element.children.iter()
}

fn set_style_div<'t>(&self, tag: Tag<'t>) -> Tag<'t> {
tag.add_style("margin", "0 auto").maybe_add_style(
"max-width",
Expand Down
1 change: 1 addition & 0 deletions packages/mrml-core/src/mj_include/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::NAME;
#[cfg_attr(feature = "json", serde(untagged))]
pub enum MjIncludeBodyChild {
Comment(crate::comment::Comment),
#[cfg(feature = "fragment")]
Fragment(crate::fragment::Fragment<Self>),
MjAccordion(crate::mj_accordion::MjAccordion),
MjButton(crate::mj_button::MjButton),
Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_include/body/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl<'opts> ParseElement<MjIncludeBodyChild> for MrmlParser<'opts> {
MJ_TABLE => Ok(MjIncludeBodyChild::MjTable(self.parse(cursor, tag)?)),
MJ_TEXT => Ok(MjIncludeBodyChild::MjText(self.parse(cursor, tag)?)),
MJ_WRAPPER => Ok(MjIncludeBodyChild::MjWrapper(self.parse(cursor, tag)?)),
#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjIncludeBodyChild::Fragment(self.parse(cursor, tag)?)),
_ => Err(Error::UnexpectedElement(tag.into())),
}
Expand Down Expand Up @@ -115,6 +116,7 @@ impl AsyncParseElement<MjIncludeBodyChild> for AsyncMrmlParser {
MJ_WRAPPER => Ok(MjIncludeBodyChild::MjWrapper(
self.async_parse(cursor, tag).await?,
)),
#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjIncludeBodyChild::Fragment(
self.async_parse(cursor, tag).await?,
)),
Expand Down
7 changes: 7 additions & 0 deletions packages/mrml-core/src/mj_include/body/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ impl MjIncludeBodyChild {
) -> &'root (dyn Renderable<'render, 'root> + 'root) {
match self {
Self::Comment(elt) => elt,
#[cfg(feature = "fragment")]
Self::Fragment(elt) => elt,
Self::MjAccordion(elt) => elt,
Self::MjButton(elt) => elt,
Expand Down Expand Up @@ -43,6 +44,7 @@ impl<'render, 'root: 'render> Renderable<'render, 'root> for MjIncludeBodyChild
}
}
impl<'root> Renderer<'root, MjIncludeBody, ()> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjIncludeBodyChild> {
fn folder<'root>(
c: &'root MjIncludeBodyChild,
Expand All @@ -54,6 +56,11 @@ impl<'root> Renderer<'root, MjIncludeBody, ()> {
}
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjIncludeBodyChild> {
self.element.children.iter()
}
}

impl<'root> Render<'root> for Renderer<'root, MjIncludeBody, ()> {
Expand Down
1 change: 1 addition & 0 deletions packages/mrml-core/src/mj_include/head/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::NAME;
#[cfg_attr(feature = "render", derive(enum_as_inner::EnumAsInner))]
pub enum MjIncludeHeadChild {
Comment(crate::comment::Comment),
#[cfg(feature = "fragment")]
Fragment(crate::fragment::Fragment<Self>),
MjAttributes(crate::mj_attributes::MjAttributes),
MjBreakpoint(crate::mj_breakpoint::MjBreakpoint),
Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_include/head/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl<'opts> ParseElement<MjIncludeHeadChild> for MrmlParser<'opts> {
MJ_RAW => self.parse(cursor, tag).map(MjIncludeHeadChild::MjRaw),
MJ_STYLE => self.parse(cursor, tag).map(MjIncludeHeadChild::MjStyle),
MJ_TITLE => self.parse(cursor, tag).map(MjIncludeHeadChild::MjTitle),
#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjIncludeHeadChild::Fragment(self.parse(cursor, tag)?)),
_ => Err(Error::UnexpectedElement(tag.into())),
}
Expand Down Expand Up @@ -81,6 +82,7 @@ impl AsyncParseElement<MjIncludeHeadChild> for AsyncMrmlParser {
.await
.map(MjIncludeHeadChild::MjTitle),

#[cfg(feature = "fragment")]
_ if tag.is_empty() => Ok(MjIncludeHeadChild::Fragment(
self.async_parse(cursor, tag).await?,
)),
Expand Down
2 changes: 2 additions & 0 deletions packages/mrml-core/src/mj_raw/children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::comment::Comment;
#[cfg(feature = "fragment")]
use crate::fragment::Fragment;
use crate::node::Node;
use crate::text::Text;
Expand All @@ -9,6 +10,7 @@ use crate::text::Text;
#[cfg_attr(feature = "print", enum_dispatch::enum_dispatch)]
pub enum MjRawChild {
Comment(Comment),
#[cfg(feature = "fragment")]
Fragment(Fragment<Self>),
Node(Node<MjRawChild>),
Text(Text),
Expand Down
7 changes: 7 additions & 0 deletions packages/mrml-core/src/mj_raw/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ impl<'render, 'root: 'render> Renderable<'render, 'root> for MjRawChild {
) -> Box<dyn Render<'root> + 'render> {
match self {
Self::Comment(elt) => elt.renderer(context),
#[cfg(feature = "fragment")]
Self::Fragment(elt) => elt.renderer(context),
Self::Node(elt) => elt.renderer(context),
Self::Text(elt) => elt.renderer(context),
}
}
}
impl<'root> Renderer<'root, MjRaw, ()> {
#[cfg(feature = "fragment")]
fn children_iter(&self) -> impl Iterator<Item = &MjRawChild> {
fn folder<'root>(c: &'root MjRawChild) -> Box<dyn Iterator<Item = &MjRawChild> + 'root> {
match c {
Expand All @@ -25,6 +27,11 @@ impl<'root> Renderer<'root, MjRaw, ()> {
}
self.element.children.iter().flat_map(folder)
}

#[cfg(not(feature = "fragment"))]
fn children_iter(&self) -> impl Iterator<Item = &MjRawChild> {
self.element.children.iter()
}
}

impl<'root> Render<'root> for Renderer<'root, MjRaw, ()> {
Expand Down
Loading

0 comments on commit 3fb0081

Please sign in to comment.