Add databake support

This commit is contained in:
Isaac Mills 2024-04-19 22:55:26 -04:00
parent 56f00e1150
commit 5336755cbd
Signed by: fnmain
GPG key ID: B67D7410F33A0F61
2 changed files with 67 additions and 67 deletions

View file

@ -17,5 +17,6 @@ image = { version = "0.24.9", default-features = false }
databake = { version = "0.1.7", features = ["derive"], optional = true }
[features]
default = ["serde", "databake"]
serde = ["dep:serde", "rangemap/serde1"]
databake = ["dep:databake"]

View file

@ -1,5 +1,4 @@
use cosmic_text::{Align, Attrs, CacheKeyFlags, Color, Family, Stretch, Style, Weight};
use cosmic_text::CacheKeyFlags;
#[cfg(feature = "databake")]
use databake::Bake;
use jotdown::{ListKind, OrderedListNumbering, OrderedListStyle};
@ -51,18 +50,18 @@ pub enum OrderedListStyleSerde {
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Metrics"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Metrics))]
pub struct MetricsSerde {
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub struct Metrics {
pub font_size: f32,
pub line_height: f32,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Family"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Family"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Family))]
pub enum FamilySerde<'a> {
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub enum Family<'a> {
Name(&'a str),
Serif,
SansSerif,
@ -71,25 +70,25 @@ pub enum FamilySerde<'a> {
Monospace,
}
impl<'a> From<Family<'a>> for FamilySerde<'a> {
fn from(value: Family<'a>) -> Self {
impl<'a> From<cosmic_text::Family<'a>> for Family<'a> {
fn from(value: cosmic_text::Family<'a>) -> Self {
match value {
Family::Name(n) => FamilySerde::Name(n),
Family::Serif => FamilySerde::Serif,
Family::SansSerif => FamilySerde::SansSerif,
Family::Cursive => FamilySerde::Cursive,
Family::Fantasy => FamilySerde::Fantasy,
Family::Monospace => FamilySerde::Monospace,
cosmic_text::Family::Name(n) => Self::Name(n),
cosmic_text::Family::Serif => Self::Serif,
cosmic_text::Family::SansSerif => Self::SansSerif,
cosmic_text::Family::Cursive => Self::Cursive,
cosmic_text::Family::Fantasy => Self::Fantasy,
cosmic_text::Family::Monospace => Self::Monospace,
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Stretch"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Stretch"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Stretch))]
pub enum StretchSerde {
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub enum Stretch {
UltraCondensed,
ExtraCondensed,
Condensed,
@ -101,62 +100,62 @@ pub enum StretchSerde {
UltraExpanded,
}
impl From<Stretch> for StretchSerde {
fn from(value: Stretch) -> Self {
impl From<cosmic_text::Stretch> for Stretch {
fn from(value: cosmic_text::Stretch) -> Self {
match value {
Stretch::UltraCondensed => StretchSerde::UltraCondensed,
Stretch::ExtraCondensed => StretchSerde::ExtraCondensed,
Stretch::Condensed => StretchSerde::Condensed,
Stretch::SemiCondensed => StretchSerde::SemiCondensed,
Stretch::Normal => StretchSerde::Normal,
Stretch::SemiExpanded => StretchSerde::SemiExpanded,
Stretch::Expanded => StretchSerde::Expanded,
Stretch::ExtraExpanded => StretchSerde::ExtraExpanded,
Stretch::UltraExpanded => StretchSerde::UltraExpanded,
cosmic_text::Stretch::UltraCondensed => Self::UltraCondensed,
cosmic_text::Stretch::ExtraCondensed => Self::ExtraCondensed,
cosmic_text::Stretch::Condensed => Self::Condensed,
cosmic_text::Stretch::SemiCondensed => Self::SemiCondensed,
cosmic_text::Stretch::Normal => Self::Normal,
cosmic_text::Stretch::SemiExpanded => Self::SemiExpanded,
cosmic_text::Stretch::Expanded => Self::Expanded,
cosmic_text::Stretch::ExtraExpanded => Self::ExtraExpanded,
cosmic_text::Stretch::UltraExpanded => Self::UltraExpanded,
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Style"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Style"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Style))]
pub enum StyleSerde {
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub enum Style {
Normal,
Italic,
Oblique,
}
impl From<Style> for StyleSerde {
fn from(value: Style) -> Self {
impl From<cosmic_text::Style> for Style {
fn from(value: cosmic_text::Style) -> Self {
match value {
Style::Normal => StyleSerde::Normal,
Style::Italic => StyleSerde::Italic,
Style::Oblique => StyleSerde::Oblique,
cosmic_text::Style::Normal => Style::Normal,
cosmic_text::Style::Italic => Style::Italic,
cosmic_text::Style::Oblique => Style::Oblique,
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Weight"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Weight"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Weight))]
pub struct WeightSerde(pub u16);
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub struct Weight(pub u16);
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Color"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Color"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Color))]
pub struct ColorSerde(pub u32);
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub struct Color(pub u32);
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Option<Color>"))]
#[cfg_attr(feature = "serde", serde(remote = "Option<cosmic_text::Color>"))]
pub enum ColorOpt {
Some(#[cfg_attr(feature = "serde", serde(with = "ColorSerde"))] Color),
Some(#[cfg_attr(feature = "serde", serde(with = "Color"))] cosmic_text::Color),
None,
}
@ -183,26 +182,26 @@ mod cache_key_flags {
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Attrs"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Attrs"))]
pub struct AttrsSerde<'a> {
#[cfg_attr(feature = "serde", serde(with = "ColorOpt"))]
pub color_opt: Option<Color>,
#[cfg_attr(feature = "serde", serde(with = "FamilySerde"))]
pub color_opt: Option<cosmic_text::Color>,
#[cfg_attr(feature = "serde", serde(with = "Family"))]
#[cfg_attr(feature = "serde", serde(borrow))]
pub family: Family<'a>,
#[cfg_attr(feature = "serde", serde(with = "StretchSerde"))]
pub stretch: Stretch,
#[cfg_attr(feature = "serde", serde(with = "StyleSerde"))]
pub style: Style,
#[cfg_attr(feature = "serde", serde(with = "WeightSerde"))]
pub weight: Weight,
pub family: cosmic_text::Family<'a>,
#[cfg_attr(feature = "serde", serde(with = "Stretch"))]
pub stretch: cosmic_text::Stretch,
#[cfg_attr(feature = "serde", serde(with = "Style"))]
pub style: cosmic_text::Style,
#[cfg_attr(feature = "serde", serde(with = "Weight"))]
pub weight: cosmic_text::Weight,
pub metadata: usize,
#[cfg_attr(feature = "serde", serde(with = "cache_key_flags"))]
pub cache_key_flags: CacheKeyFlags,
}
impl<'a> From<Attrs<'a>> for AttrsSerde<'a> {
fn from(value: Attrs<'a>) -> Self {
impl<'a> From<cosmic_text::Attrs<'a>> for AttrsSerde<'a> {
fn from(value: cosmic_text::Attrs<'a>) -> Self {
AttrsSerde {
color_opt: value.color_opt,
family: value.family,
@ -218,14 +217,14 @@ impl<'a> From<Attrs<'a>> for AttrsSerde<'a> {
#[cfg(feature = "databake")]
impl<'a> Bake for AttrsSerde<'a> {
fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream {
let color = self.color_opt.map(|c| ColorSerde(c.0)).bake(ctx);
let family: FamilySerde = self.family.into();
let color = self.color_opt.map(|c| Color(c.0)).bake(ctx);
let family: Family = self.family.into();
let family = family.bake(ctx);
let stretch: StretchSerde = self.stretch.into();
let stretch: Stretch = self.stretch.into();
let stretch = stretch.bake(ctx);
let style: StyleSerde = self.style.into();
let style: Style = self.style.into();
let style = style.bake(ctx);
let weight: WeightSerde = WeightSerde(self.weight.0);
let weight: Weight = Weight(self.weight.0);
let weight = weight.bake(ctx);
let metadata = self.metadata.bake(ctx);
databake::quote! {
@ -244,18 +243,18 @@ impl<'a> Bake for AttrsSerde<'a> {
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Option<Align>"))]
#[cfg_attr(feature = "serde", serde(remote = "Option<cosmic_text::Align>"))]
pub enum AlignSerde {
Some(#[cfg_attr(feature = "serde", serde(with = "AlignRef"))] Align),
Some(#[cfg_attr(feature = "serde", serde(with = "Align"))] cosmic_text::Align),
None,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(remote = "Align"))]
#[cfg_attr(feature = "serde", serde(remote = "cosmic_text::Align"))]
#[cfg_attr(feature = "databake", derive(Bake))]
#[cfg_attr(feature = "databake", databake(path = cosmic_text::Align))]
pub enum AlignRef {
#[cfg_attr(feature = "databake", databake(path = cosmic_text))]
pub enum Align {
Left,
Right,
Center,