diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-13 21:39:38 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-13 21:40:06 +0100 |
| commit | 880b1847bd4170ce80be5781c2163ba085cdcaff (patch) | |
| tree | 3fbfdb70cb04c4922f0ec9e3f29f2c63d11d753b /library | |
| parent | cb3c263c4a67f4d361dbdb5048a1c073bd1fff96 (diff) | |
Derive `Cast` for enums
Diffstat (limited to 'library')
| -rw-r--r-- | library/src/layout/par.rs | 19 | ||||
| -rw-r--r-- | library/src/math/matrix.rs | 36 | ||||
| -rw-r--r-- | library/src/text/misc.rs | 15 | ||||
| -rw-r--r-- | library/src/text/mod.rs | 44 | ||||
| -rw-r--r-- | library/src/visualize/image.rs | 24 |
5 files changed, 24 insertions, 114 deletions
diff --git a/library/src/layout/par.rs b/library/src/layout/par.rs index 6178c059..2d7c5d62 100644 --- a/library/src/layout/par.rs +++ b/library/src/layout/par.rs @@ -193,30 +193,15 @@ impl Resolve for HorizontalAlign { } /// How to determine line breaks in a paragraph. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum Linebreaks { /// Determine the line breaks in a simple first-fit style. Simple, /// Optimize the line breaks for the whole paragraph. - Optimized, -} - -cast_from_value! { - Linebreaks, - /// Determine the line breaks in a simple first-fit style. - "simple" => Self::Simple, - /// Optimize the line breaks for the whole paragraph. /// /// Typst will try to produce more evenly filled lines of text by /// considering the whole paragraph when calculating line breaks. - "optimized" => Self::Optimized, -} - -cast_to_value! { - v: Linebreaks => Value::from(match v { - Linebreaks::Simple => "simple", - Linebreaks::Optimized => "optimized", - }) + Optimized, } /// A paragraph break. diff --git a/library/src/math/matrix.rs b/library/src/math/matrix.rs index 148d79e8..d79c7ca5 100644 --- a/library/src/math/matrix.rs +++ b/library/src/math/matrix.rs @@ -169,12 +169,22 @@ impl LayoutMath for CasesNode { } /// A vector / matrix delimiter. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum Delimiter { + /// Delimit with parentheses. + #[string("(")] Paren, + /// Delimit with brackets. + #[string("[")] Bracket, + /// Delimit with curly braces. + #[string("{")] Brace, + /// Delimit with vertical bars. + #[string("|")] Bar, + /// Delimit with double vertical bars. + #[string("||")] DoubleBar, } @@ -202,30 +212,6 @@ impl Delimiter { } } -cast_from_value! { - Delimiter, - /// Delimit with parentheses. - "(" => Self::Paren, - /// Delimit with brackets. - "[" => Self::Bracket, - /// Delimit with curly braces. - "{" => Self::Brace, - /// Delimit with vertical bars. - "|" => Self::Bar, - /// Delimit with double vertical bars. - "||" => Self::DoubleBar, -} - -cast_to_value! { - v: Delimiter => Value::from(match v { - Delimiter::Paren => "(", - Delimiter::Bracket => "[", - Delimiter::Brace => "{", - Delimiter::Bar => "|", - Delimiter::DoubleBar => "||", - }) -} - /// Layout the inner contents of a vector. fn layout_vec_body( ctx: &mut MathContext, diff --git a/library/src/text/misc.rs b/library/src/text/misc.rs index 029e15f4..5a5c8514 100644 --- a/library/src/text/misc.rs +++ b/library/src/text/misc.rs @@ -246,7 +246,7 @@ cast_from_value! { } /// A case transformation on text. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum Case { /// Everything is lowercased. Lower, @@ -264,19 +264,6 @@ impl Case { } } -cast_from_value! { - Case, - "lower" => Self::Lower, - "upper" => Self::Upper, -} - -cast_to_value! { - v: Case => Value::from(match v { - Case::Lower => "lower", - Case::Upper => "upper", - }) -} - /// Display text in small capitals. /// /// _Note:_ This enables the OpenType `smcp` feature for the font. Not all fonts diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index a81ef3d7..845ffe29 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -686,53 +686,23 @@ cast_to_value! { } /// Which kind of numbers / figures to select. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum NumberType { - /// Numbers that fit well with capital text. ("lnum") - Lining, - /// Numbers that fit well into a flow of upper- and lowercase text. ("onum") - OldStyle, -} - -cast_from_value! { - NumberType, /// Numbers that fit well with capital text (the OpenType `lnum` /// font feature). - "lining" => Self::Lining, - // Numbers that fit well into a flow of upper- and lowercase text (the + Lining, + /// Numbers that fit well into a flow of upper- and lowercase text (the /// OpenType `onum` font feature). - "old-style" => Self::OldStyle, -} - -cast_to_value! { - v: NumberType => Value::from(match v { - NumberType::Lining => "lining", - NumberType::OldStyle => "old-style", - }) + OldStyle, } /// The width of numbers / figures. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum NumberWidth { - /// Number widths are glyph specific. ("pnum") - Proportional, - /// All numbers are of equal width / monospaced. ("tnum") - Tabular, -} - -cast_from_value! { - NumberWidth, /// Numbers with glyph-specific widths (the OpenType `pnum` font feature). - "proportional" => Self::Proportional, + Proportional, /// Numbers of equal width (the OpenType `tnum` font feature). - "tabular" => Self::Tabular, -} - -cast_to_value! { - v: NumberWidth => Value::from(match v { - NumberWidth::Proportional => "proportional", - NumberWidth::Tabular => "tabular", - }) + Tabular, } /// OpenType font features settings. diff --git a/library/src/visualize/image.rs b/library/src/visualize/image.rs index 29a04c96..4509cf5a 100644 --- a/library/src/visualize/image.rs +++ b/library/src/visualize/image.rs @@ -113,33 +113,15 @@ impl Layout for ImageNode { } /// How an image should adjust itself to a given area. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum ImageFit { - /// The image should completely cover the area. + /// The image should completely cover the area. This is the default. Cover, /// The image should be fully contained in the area. Contain, - /// The image should be stretched so that it exactly fills the area. - Stretch, -} - -cast_from_value! { - ImageFit, - /// The image should completely cover the area. This is the default. - "cover" => Self::Cover, - /// The image should be fully contained in the area. - "contain" => Self::Contain, /// The image should be stretched so that it exactly fills the area, even if /// this means that the image will be distorted. - "stretch" => Self::Stretch, -} - -cast_to_value! { - fit: ImageFit => Value::from(match fit { - ImageFit::Cover => "cover", - ImageFit::Contain => "contain", - ImageFit::Stretch => "stretch", - }) + Stretch, } /// Load an image from a path. |
