Skip to content

Commit

Permalink
feat: implement builder for mi element (ident)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfejzic committed Nov 21, 2023
1 parent bcdfdca commit 14c0ca6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
13 changes: 1 addition & 12 deletions src/elements/mfrac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
///
/// let frac = Frac::builder().num("1").denom("2").build();
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FracBuilder<N, D> {
num: Option<MathMl>,
denom: Option<MathMl>,
Expand All @@ -77,17 +77,6 @@ pub struct FracBuilder<N, D> {
_marker: PhantomData<(N, D)>,
}

impl Default for FracBuilder<Uninit, Uninit> {
fn default() -> Self {
Self {
num: None,
denom: None,
attr: Vec::default(),
_marker: Default::default(),
}
}
}

impl<N, D> FracBuilder<N, D> {
/// Add or overwrite the numerator to the `mfrac` element.
pub fn num(self, num: impl Into<MathMl>) -> FracBuilder<Init, D> {
Expand Down
48 changes: 47 additions & 1 deletion src/elements/mi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::attributes::Attribute;
use std::marker::PhantomData;

use crate::{
attributes::Attribute,
markers::{Init, Uninit},
};

/// An attribute of `mi` element. Either one of the global [`Attribute`]s, or `mathvariant`
/// attribute.
Expand All @@ -24,6 +29,12 @@ pub struct Ident {
attributes: Vec<IdentAttr>,
}

impl Ident {
pub fn builder() -> IdentBuilder<Uninit> {
IdentBuilder::default()
}
}

impl<T> From<T> for Ident
where
T: Into<String>,
Expand All @@ -35,3 +46,38 @@ where
}
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct IdentBuilder<T> {
ident: Option<String>,
attributes: Vec<IdentAttr>,
_marker: PhantomData<(T,)>,
}

impl<T> IdentBuilder<T> {
pub fn ident(self, ident: impl Into<String>) -> IdentBuilder<Uninit> {
IdentBuilder {
ident: Some(ident.into()),
attributes: self.attributes,
_marker: PhantomData,
}
}

pub fn attr<I, A>(mut self, attr: I) -> Self
where
I: IntoIterator<Item = A>,
A: Into<IdentAttr>,
{
self.attributes.extend(attr.into_iter().map(Into::into));
self
}
}

impl IdentBuilder<Init> {
pub fn build(self) -> Ident {
Ident {
ident: self.ident.expect("Content is guaranteed to be init."),
attributes: self.attributes,
}
}
}

0 comments on commit 14c0ca6

Please sign in to comment.