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