Skip to content

Commit 7de8f9c

Browse files
committed
Add apply method
1 parent 9bc35ae commit 7de8f9c

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/styling.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,27 @@ pub enum MathStyle {
6060
Stretched,
6161
}
6262

63-
/// Returns an iterator that yields the styled equivalent of a `char`.
64-
///
65-
/// This `struct` is created by the [`to_style`] function. See its
66-
/// documentation for more.
67-
#[derive(Debug, Clone)]
68-
pub struct ToStyle(core::array::IntoIter<char, 2>);
69-
70-
impl ToStyle {
71-
fn new(c: char, style: MathStyle) -> ToStyle {
63+
impl MathStyle {
64+
/// Applies the style to a given `char`.
65+
///
66+
/// Note that some styles will convert a character into multiple
67+
/// characters, hence this function always returns an array of two
68+
/// characters.
69+
///
70+
/// # Examples
71+
///
72+
/// ```
73+
/// use codex::styling::MathStyle;
74+
///
75+
/// assert_eq!(['𝑴', '\0'], MathStyle::SerifItalicBold.apply('M'));
76+
/// assert_eq!(['𞸪', '\0'], MathStyle::Initial.apply('ك'));
77+
/// assert_eq!(['ⅈ', '\0'], MathStyle::DoubleStruckItalic.apply('i'));
78+
/// assert_eq!(['𝓴', '\u{fe01}'], MathStyle::RoundhandBold.apply('k'));
79+
/// ```
80+
pub fn apply(self, c: char) -> [char; 2] {
7281
use mappings::*;
7382
use MathStyle::*;
74-
let chars = match style {
83+
match self {
7584
Serif => [to_serif(c), '\0'],
7685
SerifBold => [to_serif_bold(c), '\0'],
7786
SerifItalic => [to_serif_italic(c), '\0'],
@@ -95,8 +104,20 @@ impl ToStyle {
95104
Tailed => [to_tailed(c), '\0'],
96105
Looped => [to_looped(c), '\0'],
97106
Stretched => [to_stretched(c), '\0'],
98-
};
107+
}
108+
}
109+
}
99110

111+
/// Returns an iterator that yields the styled equivalent of a `char`.
112+
///
113+
/// This `struct` is created by the [`to_style`] function. See its
114+
/// documentation for more.
115+
#[derive(Debug, Clone)]
116+
pub struct ToStyle(core::array::IntoIter<char, 2>);
117+
118+
impl ToStyle {
119+
#[inline]
120+
fn new(chars: [char; 2]) -> ToStyle {
100121
let mut iter = chars.into_iter();
101122
if chars[1] == '\0' {
102123
iter.next_back();
@@ -171,14 +192,18 @@ impl fmt::Display for ToStyle {
171192
/// ```
172193
/// use codex::styling::{to_style, MathStyle};
173194
///
174-
/// assert_eq!("𝑴", to_style('M', MathStyle::SerifItalicBold).to_string());
175-
/// assert_eq!("𞸪", to_style('ك', MathStyle::Initial).to_string());
176-
/// assert_eq!("ⅈ", to_style('i', MathStyle::DoubleStruckItalic).to_string());
177-
/// assert_eq!("𝓴\u{fe01}", to_style('k', MathStyle::RoundhandBold).to_string());
195+
/// assert_eq!("𞺚", to_style('ظ', MathStyle::Looped).to_string());
196+
/// assert_eq!("𝒬\u{fe00}", to_style('Q', MathStyle::Chancery).to_string());
197+
///
198+
/// let s = "xγΩAذح1∑س"
199+
/// .chars()
200+
/// .flat_map(|c| to_style(c, MathStyle::DoubleStruck))
201+
/// .collect::<String>();
202+
/// assert_eq!("𝕩ℽΩ𝔸𞺸𞺧𝟙⅀𞺮", s);
178203
/// ```
179204
#[inline]
180205
pub fn to_style(c: char, style: MathStyle) -> ToStyle {
181-
ToStyle::new(c, style)
206+
ToStyle::new(style.apply(c))
182207
}
183208

184209
/// Functions which map a `char` to its specified styled form.

0 commit comments

Comments
 (0)