Add borrowed serde support

This commit is contained in:
Isaac Mills 2024-04-10 14:29:10 -04:00
parent b3339bc4c7
commit ce024a120e
Signed by: fnmain
GPG key ID: B67D7410F33A0F61
2 changed files with 18 additions and 60 deletions

View file

@ -1,8 +1,6 @@
use std::borrow::Cow;
use cosmic_text::{
Attrs, AttrsOwned, Buffer, Color, Family, FontSystem, Metrics, Shaping, Style, Weight,
};
use cosmic_text::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, Style, Weight};
use jotdown::{Container, Event, ListKind};
pub use jotdown;
@ -141,15 +139,18 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, '
}
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct RichText(
String,
#[cfg_attr(feature = "serde", serde(with = "AttrsSerde"))] AttrsOwned,
pub struct RichText<'a>(
Cow<'a, str>,
#[cfg_attr(feature = "serde", serde(with = "AttrsSerde"))]
#[cfg_attr(feature = "serde", serde(borrow))]
Attrs<'a>,
);
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct JotdownItem<'a> {
pub indent: Indent,
pub buffer: Vec<RichText>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub buffer: Vec<RichText<'a>>,
#[cfg_attr(feature = "serde", serde(with = "MetricsSerde"))]
pub metrics: Metrics,
pub image_url: Option<Cow<'a, str>>,
@ -172,7 +173,7 @@ impl<'a> JotdownItem<'a> {
);
buffer.set_rich_text(
font_system,
self.buffer.iter().map(|r| (r.0.as_str(), r.1.as_attrs())),
self.buffer.iter().map(|r| (r.0.as_ref(), r.1)),
Attrs::new().family(Family::SansSerif),
Shaping::Advanced,
);
@ -201,7 +202,7 @@ impl<'a, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, T> {
};
let buffer = (&mut jot)
.map(|r| RichText(r.0.to_owned(), AttrsOwned::new(r.1)))
.map(|r| RichText(Cow::Borrowed(r.0), r.1))
.collect::<Vec<_>>();
let image_url = jot.image_url;
let urls = jot.urls;

View file

@ -1,6 +1,4 @@
use cosmic_text::{
Align, Attrs, AttrsOwned, CacheKeyFlags, Color, FamilyOwned, Metrics, Stretch, Style, Weight,
};
use cosmic_text::{Align, Attrs, CacheKeyFlags, Color, Family, Metrics, Stretch, Style, Weight};
use jotdown::{ListKind, OrderedListNumbering, OrderedListStyle};
use serde::{Deserialize, Serialize};
@ -50,9 +48,9 @@ pub struct MetricsSerde {
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(remote = "FamilyOwned")]
pub enum FamilySerde {
Name(String),
#[serde(remote = "Family")]
pub enum FamilySerde<'a> {
Name(&'a str),
Serif,
SansSerif,
Cursive,
@ -118,12 +116,13 @@ mod cache_key_flags {
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(remote = "AttrsOwned")]
pub struct AttrsSerde {
#[serde(remote = "Attrs")]
pub struct AttrsSerde<'a> {
#[serde(with = "ColorOpt")]
pub color_opt: Option<Color>,
#[serde(with = "FamilySerde")]
pub family_owned: FamilyOwned,
#[serde(borrow)]
pub family: Family<'a>,
#[serde(with = "StretchSerde")]
pub stretch: Stretch,
#[serde(with = "StyleSerde")]
@ -135,20 +134,6 @@ pub struct AttrsSerde {
pub cache_key_flags: CacheKeyFlags,
}
impl<'a> From<Attrs<'a>> for AttrsSerde {
fn from(value: Attrs<'a>) -> Self {
Self {
color_opt: value.color_opt,
family_owned: FamilyOwned::new(value.family),
stretch: value.stretch,
style: value.style,
weight: value.weight,
metadata: value.metadata,
cache_key_flags: value.cache_key_flags,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(remote = "Option<Align>")]
pub enum AlignSerde {
@ -165,31 +150,3 @@ pub enum AlignRef {
Justified,
End,
}
impl AttrsSerde {
pub fn as_attrs(&self) -> Attrs<'_> {
Attrs {
color_opt: self.color_opt,
family: self.family_owned.as_family(),
stretch: self.stretch,
style: self.style,
weight: self.weight,
metadata: self.metadata,
cache_key_flags: CacheKeyFlags::empty(),
}
}
}
impl From<AttrsOwned> for AttrsSerde {
fn from(value: AttrsOwned) -> Self {
Self {
color_opt: value.color_opt,
family_owned: value.family_owned,
stretch: value.stretch,
style: value.style,
weight: value.weight,
metadata: value.metadata,
cache_key_flags: value.cache_key_flags,
}
}
}