Skip to main content

vimwiki_core/lang/elements/blocks/inline/
math.rs

1use crate::StrictEq;
2use derive_more::{AsRef, Constructor, Display};
3use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5
6#[derive(
7    AsRef,
8    Constructor,
9    Clone,
10    Debug,
11    Display,
12    Eq,
13    PartialEq,
14    Hash,
15    Serialize,
16    Deserialize,
17)]
18#[as_ref(forward)]
19pub struct MathInline<'a>(
20    /// Represents the text contained within the inline math snippet
21    Cow<'a, str>,
22);
23
24impl<'a> MathInline<'a> {
25    /// Extracts a string slice containing the entire math snippet
26    ///
27    /// # Examples
28    ///
29    /// Basic usage:
30    ///
31    /// ```
32    /// # use std::borrow::Cow;
33    /// # use vimwiki_core::MathInline;
34    /// let math = MathInline::new(Cow::Borrowed("some math"));
35    /// assert_eq!(math.as_str(), "some math");
36    /// ```
37    pub fn as_str(&self) -> &str {
38        self.0.as_ref()
39    }
40}
41
42impl MathInline<'_> {
43    pub fn as_borrowed(&self) -> MathInline {
44        use self::Cow::*;
45
46        let formula = Cow::Borrowed(match &self.0 {
47            Borrowed(x) => *x,
48            Owned(x) => x.as_str(),
49        });
50
51        MathInline::new(formula)
52    }
53
54    pub fn into_owned(self) -> MathInline<'static> {
55        let formula = Cow::Owned(self.0.into_owned());
56
57        MathInline::new(formula)
58    }
59}
60
61impl<'a> From<&'a str> for MathInline<'a> {
62    fn from(s: &'a str) -> Self {
63        Self::new(Cow::Borrowed(s))
64    }
65}
66
67impl From<String> for MathInline<'static> {
68    fn from(s: String) -> Self {
69        Self::new(Cow::Owned(s))
70    }
71}
72
73impl<'a> StrictEq for MathInline<'a> {
74    /// Same as PartialEq
75    #[inline]
76    fn strict_eq(&self, other: &Self) -> bool {
77        self == other
78    }
79}