diff --git a/src/lib.rs b/src/lib.rs index 789e030..3cde13a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>> 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, + #[cfg_attr(feature = "serde", serde(borrow))] + pub buffer: Vec>, #[cfg_attr(feature = "serde", serde(with = "MetricsSerde"))] pub metrics: Metrics, pub image_url: Option>, @@ -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>> 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::>(); let image_url = jot.image_url; let urls = jot.urls; diff --git a/src/serde_suck.rs b/src/serde_suck.rs index a4a891d..fa0c772 100644 --- a/src/serde_suck.rs +++ b/src/serde_suck.rs @@ -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, #[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> 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")] 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 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, - } - } -}