diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-06-06 21:13:59 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-06-06 22:06:16 +0200 |
| commit | fd417da04f7ca4b995de7f6510abafd3e9c31307 (patch) | |
| tree | 3675529c75ca7363701ac8ea306de2cc1d3cbcb3 /library/src/math | |
| parent | 168bdf35bd773e67343c965cb473492cc5cae9e7 (diff) | |
Improve value casting infrastructure
Diffstat (limited to 'library/src/math')
| -rw-r--r-- | library/src/math/accent.rs | 7 | ||||
| -rw-r--r-- | library/src/math/delimited.rs | 18 | ||||
| -rw-r--r-- | library/src/math/mod.rs | 44 | ||||
| -rw-r--r-- | library/src/math/root.rs | 5 | ||||
| -rw-r--r-- | library/src/math/row.rs | 19 | ||||
| -rw-r--r-- | library/src/math/style.rs | 79 |
6 files changed, 72 insertions, 100 deletions
diff --git a/library/src/math/accent.rs b/library/src/math/accent.rs index 7878782b..4a12f5c5 100644 --- a/library/src/math/accent.rs +++ b/library/src/math/accent.rs @@ -128,15 +128,12 @@ impl Accent { } } -cast_from_value! { +cast! { Accent, + self => self.0.into_value(), v: char => Self::new(v), v: Content => match v.to::<TextElem>() { Some(elem) => Value::Str(elem.text().into()).cast()?, None => Err("expected text")?, }, } - -cast_to_value! { - v: Accent => v.0.into() -} diff --git a/library/src/math/delimited.rs b/library/src/math/delimited.rs index 403f7922..08d18a5e 100644 --- a/library/src/math/delimited.rs +++ b/library/src/math/delimited.rs @@ -113,12 +113,11 @@ fn scale( /// /// Display: Floor /// Category: math -/// Returns: content #[func] pub fn floor( /// The expression to floor. body: Content, -) -> Value { +) -> Content { delimited(body, '⌊', '⌋') } @@ -131,12 +130,11 @@ pub fn floor( /// /// Display: Ceil /// Category: math -/// Returns: content #[func] pub fn ceil( /// The expression to ceil. body: Content, -) -> Value { +) -> Content { delimited(body, '⌈', '⌉') } @@ -149,12 +147,11 @@ pub fn ceil( /// /// Display: Round /// Category: math -/// Returns: content #[func] pub fn round( /// The expression to round. body: Content, -) -> Value { +) -> Content { delimited(body, '⌊', '⌉') } @@ -168,12 +165,11 @@ pub fn round( /// /// Display: Abs /// Category: math -/// Returns: content #[func] pub fn abs( /// The expression to take the absolute value of. body: Content, -) -> Value { +) -> Content { delimited(body, '|', '|') } @@ -186,21 +182,19 @@ pub fn abs( /// /// Display: Norm /// Category: math -/// Returns: content #[func] pub fn norm( /// The expression to take the norm of. body: Content, -) -> Value { +) -> Content { delimited(body, '‖', '‖') } -fn delimited(body: Content, left: char, right: char) -> Value { +fn delimited(body: Content, left: char, right: char) -> Content { LrElem::new(Content::sequence([ TextElem::packed(left), body, TextElem::packed(right), ])) .pack() - .into() } diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 3ea96cfa..4ab23f23 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -59,11 +59,11 @@ pub fn module() -> Module { // Grouping. math.define("lr", LrElem::func()); - math.define("abs", abs); - math.define("norm", norm); - math.define("floor", floor); - math.define("ceil", ceil); - math.define("round", round); + math.define("abs", abs_func()); + math.define("norm", norm_func()); + math.define("floor", floor_func()); + math.define("ceil", ceil_func()); + math.define("round", round_func()); // Attachments and accents. math.define("attach", AttachElem::func()); @@ -86,24 +86,24 @@ pub fn module() -> Module { math.define("cases", CasesElem::func()); // Roots. - math.define("sqrt", sqrt); + math.define("sqrt", sqrt_func()); math.define("root", RootElem::func()); // Styles. - math.define("upright", upright); - math.define("bold", bold); - math.define("italic", italic); - math.define("serif", serif); - math.define("sans", sans); - math.define("cal", cal); - math.define("frak", frak); - math.define("mono", mono); - math.define("bb", bb); - - math.define("display", display); - math.define("inline", inline); - math.define("script", script); - math.define("sscript", sscript); + math.define("upright", upright_func()); + math.define("bold", bold_func()); + math.define("italic", italic_func()); + math.define("serif", serif_func()); + math.define("sans", sans_func()); + math.define("cal", cal_func()); + math.define("frak", frak_func()); + math.define("mono", mono_func()); + math.define("bb", bb_func()); + + math.define("display", display_func()); + math.define("inline", inline_func()); + math.define("script", script_func()); + math.define("sscript", sscript_func()); // Text operators. math.define("op", OpElem::func()); @@ -197,9 +197,7 @@ impl Synthesize for EquationElem { let supplement = match self.supplement(styles) { Smart::Auto => TextElem::packed(self.local_name_in(styles)), Smart::Custom(None) => Content::empty(), - Smart::Custom(Some(supplement)) => { - supplement.resolve(vt, [self.clone().into()])? - } + Smart::Custom(Some(supplement)) => supplement.resolve(vt, [self.clone()])?, }; self.push_block(self.block(styles)); diff --git a/library/src/math/root.rs b/library/src/math/root.rs index 05f3a2a7..d1c5f46a 100644 --- a/library/src/math/root.rs +++ b/library/src/math/root.rs @@ -9,13 +9,12 @@ use super::*; /// /// Display: Square Root /// Category: math -/// Returns: content #[func] pub fn sqrt( /// The expression to take the square root of. radicand: Content, -) -> Value { - RootElem::new(radicand).pack().into() +) -> Content { + RootElem::new(radicand).pack() } /// A general root. diff --git a/library/src/math/row.rs b/library/src/math/row.rs index 9b693bc1..6e666e89 100644 --- a/library/src/math/row.rs +++ b/library/src/math/row.rs @@ -233,3 +233,22 @@ impl<T: Into<MathFragment>> From<T> for MathRow { Self(vec![fragment.into()]) } } + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +enum LeftRightAlternator { + Left, + Right, +} + +impl Iterator for LeftRightAlternator { + type Item = LeftRightAlternator; + + fn next(&mut self) -> Option<Self::Item> { + let r = Some(*self); + match self { + Self::Left => *self = Self::Right, + Self::Right => *self = Self::Left, + } + r + } +} diff --git a/library/src/math/style.rs b/library/src/math/style.rs index e0e1ccad..05ff64dc 100644 --- a/library/src/math/style.rs +++ b/library/src/math/style.rs @@ -9,13 +9,12 @@ use super::*; /// /// Display: Bold /// Category: math -/// Returns: content #[func] pub fn bold( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body).with_bold(Some(true)).pack().into() +) -> Content { + MathStyleElem::new(body).with_bold(Some(true)).pack() } /// Upright (non-italic) font style in math. @@ -27,13 +26,12 @@ pub fn bold( /// /// Display: Upright /// Category: math -/// Returns: content #[func] pub fn upright( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body).with_italic(Some(false)).pack().into() +) -> Content { + MathStyleElem::new(body).with_italic(Some(false)).pack() } /// Italic font style in math. @@ -42,13 +40,12 @@ pub fn upright( /// /// Display: Italic /// Category: math -/// Returns: content #[func] pub fn italic( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body).with_italic(Some(true)).pack().into() +) -> Content { + MathStyleElem::new(body).with_italic(Some(true)).pack() } /// Serif (roman) font style in math. /// @@ -56,16 +53,12 @@ pub fn italic( /// /// Display: Serif /// Category: math -/// Returns: content #[func] pub fn serif( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body) - .with_variant(Some(MathVariant::Serif)) - .pack() - .into() +) -> Content { + MathStyleElem::new(body).with_variant(Some(MathVariant::Serif)).pack() } /// Sans-serif font style in math. @@ -77,16 +70,12 @@ pub fn serif( /// /// Display: Sans-serif /// Category: math -/// Returns: content #[func] pub fn sans( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body) - .with_variant(Some(MathVariant::Sans)) - .pack() - .into() +) -> Content { + MathStyleElem::new(body).with_variant(Some(MathVariant::Sans)).pack() } /// Calligraphic font style in math. @@ -98,16 +87,12 @@ pub fn sans( /// /// Display: Calligraphic /// Category: math -/// Returns: content #[func] pub fn cal( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body) - .with_variant(Some(MathVariant::Cal)) - .pack() - .into() +) -> Content { + MathStyleElem::new(body).with_variant(Some(MathVariant::Cal)).pack() } /// Fraktur font style in math. @@ -119,16 +104,12 @@ pub fn cal( /// /// Display: Fraktur /// Category: math -/// Returns: content #[func] pub fn frak( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body) - .with_variant(Some(MathVariant::Frak)) - .pack() - .into() +) -> Content { + MathStyleElem::new(body).with_variant(Some(MathVariant::Frak)).pack() } /// Monospace font style in math. @@ -140,16 +121,12 @@ pub fn frak( /// /// Display: Monospace /// Category: math -/// Returns: content #[func] pub fn mono( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body) - .with_variant(Some(MathVariant::Mono)) - .pack() - .into() +) -> Content { + MathStyleElem::new(body).with_variant(Some(MathVariant::Mono)).pack() } /// Blackboard bold (double-struck) font style in math. @@ -166,16 +143,12 @@ pub fn mono( /// /// Display: Blackboard Bold /// Category: math -/// Returns: content #[func] pub fn bb( /// The content to style. body: Content, -) -> Value { - MathStyleElem::new(body) - .with_variant(Some(MathVariant::Bb)) - .pack() - .into() +) -> Content { + MathStyleElem::new(body).with_variant(Some(MathVariant::Bb)).pack() } /// Forced display style in math. @@ -189,7 +162,6 @@ pub fn bb( /// /// Display: Display Size /// Category: math -/// Returns: content #[func] pub fn display( /// The content to size. @@ -199,12 +171,11 @@ pub fn display( #[named] #[default(false)] cramp: bool, -) -> Value { +) -> Content { MathStyleElem::new(body) .with_size(Some(MathSize::Display)) .with_cramp(Some(cramp)) .pack() - .into() } /// Forced inline (text) style in math. @@ -219,7 +190,6 @@ pub fn display( /// /// Display: Inline Size /// Category: math -/// Returns: content #[func] pub fn inline( /// The content to size. @@ -229,12 +199,11 @@ pub fn inline( #[named] #[default(false)] cramp: bool, -) -> Value { +) -> Content { MathStyleElem::new(body) .with_size(Some(MathSize::Text)) .with_cramp(Some(cramp)) .pack() - .into() } /// Forced script style in math. @@ -248,7 +217,6 @@ pub fn inline( /// /// Display: Script Size /// Category: math -/// Returns: content #[func] pub fn script( /// The content to size. @@ -258,12 +226,11 @@ pub fn script( #[named] #[default(true)] cramp: bool, -) -> Value { +) -> Content { MathStyleElem::new(body) .with_size(Some(MathSize::Script)) .with_cramp(Some(cramp)) .pack() - .into() } /// Forced second script style in math. @@ -278,7 +245,6 @@ pub fn script( /// /// Display: Script-Script Size /// Category: math -/// Returns: content #[func] pub fn sscript( /// The content to size. @@ -288,12 +254,11 @@ pub fn sscript( #[named] #[default(true)] cramp: bool, -) -> Value { +) -> Content { MathStyleElem::new(body) .with_size(Some(MathSize::ScriptScript)) .with_cramp(Some(cramp)) .pack() - .into() } /// A font variant in math. |
