Add borrowed serde support
This commit is contained in:
parent
b3339bc4c7
commit
ce024a120e
2 changed files with 18 additions and 60 deletions
19
src/lib.rs
19
src/lib.rs
|
@ -1,8 +1,6 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use cosmic_text::{
|
use cosmic_text::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, Style, Weight};
|
||||||
Attrs, AttrsOwned, Buffer, Color, Family, FontSystem, Metrics, Shaping, Style, Weight,
|
|
||||||
};
|
|
||||||
use jotdown::{Container, Event, ListKind};
|
use jotdown::{Container, Event, ListKind};
|
||||||
|
|
||||||
pub use jotdown;
|
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))]
|
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||||
pub struct RichText(
|
pub struct RichText<'a>(
|
||||||
String,
|
Cow<'a, str>,
|
||||||
#[cfg_attr(feature = "serde", serde(with = "AttrsSerde"))] AttrsOwned,
|
#[cfg_attr(feature = "serde", serde(with = "AttrsSerde"))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(borrow))]
|
||||||
|
Attrs<'a>,
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||||
pub struct JotdownItem<'a> {
|
pub struct JotdownItem<'a> {
|
||||||
pub indent: Indent,
|
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"))]
|
#[cfg_attr(feature = "serde", serde(with = "MetricsSerde"))]
|
||||||
pub metrics: Metrics,
|
pub metrics: Metrics,
|
||||||
pub image_url: Option<Cow<'a, str>>,
|
pub image_url: Option<Cow<'a, str>>,
|
||||||
|
@ -172,7 +173,7 @@ impl<'a> JotdownItem<'a> {
|
||||||
);
|
);
|
||||||
buffer.set_rich_text(
|
buffer.set_rich_text(
|
||||||
font_system,
|
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),
|
Attrs::new().family(Family::SansSerif),
|
||||||
Shaping::Advanced,
|
Shaping::Advanced,
|
||||||
);
|
);
|
||||||
|
@ -201,7 +202,7 @@ impl<'a, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, T> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let buffer = (&mut jot)
|
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<_>>();
|
.collect::<Vec<_>>();
|
||||||
let image_url = jot.image_url;
|
let image_url = jot.image_url;
|
||||||
let urls = jot.urls;
|
let urls = jot.urls;
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use cosmic_text::{
|
use cosmic_text::{Align, Attrs, CacheKeyFlags, Color, Family, Metrics, Stretch, Style, Weight};
|
||||||
Align, Attrs, AttrsOwned, CacheKeyFlags, Color, FamilyOwned, Metrics, Stretch, Style, Weight,
|
|
||||||
};
|
|
||||||
|
|
||||||
use jotdown::{ListKind, OrderedListNumbering, OrderedListStyle};
|
use jotdown::{ListKind, OrderedListNumbering, OrderedListStyle};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -50,9 +48,9 @@ pub struct MetricsSerde {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
#[serde(remote = "FamilyOwned")]
|
#[serde(remote = "Family")]
|
||||||
pub enum FamilySerde {
|
pub enum FamilySerde<'a> {
|
||||||
Name(String),
|
Name(&'a str),
|
||||||
Serif,
|
Serif,
|
||||||
SansSerif,
|
SansSerif,
|
||||||
Cursive,
|
Cursive,
|
||||||
|
@ -118,12 +116,13 @@ mod cache_key_flags {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
#[serde(remote = "AttrsOwned")]
|
#[serde(remote = "Attrs")]
|
||||||
pub struct AttrsSerde {
|
pub struct AttrsSerde<'a> {
|
||||||
#[serde(with = "ColorOpt")]
|
#[serde(with = "ColorOpt")]
|
||||||
pub color_opt: Option<Color>,
|
pub color_opt: Option<Color>,
|
||||||
#[serde(with = "FamilySerde")]
|
#[serde(with = "FamilySerde")]
|
||||||
pub family_owned: FamilyOwned,
|
#[serde(borrow)]
|
||||||
|
pub family: Family<'a>,
|
||||||
#[serde(with = "StretchSerde")]
|
#[serde(with = "StretchSerde")]
|
||||||
pub stretch: Stretch,
|
pub stretch: Stretch,
|
||||||
#[serde(with = "StyleSerde")]
|
#[serde(with = "StyleSerde")]
|
||||||
|
@ -135,20 +134,6 @@ pub struct AttrsSerde {
|
||||||
pub cache_key_flags: CacheKeyFlags,
|
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)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
#[serde(remote = "Option<Align>")]
|
#[serde(remote = "Option<Align>")]
|
||||||
pub enum AlignSerde {
|
pub enum AlignSerde {
|
||||||
|
@ -165,31 +150,3 @@ pub enum AlignRef {
|
||||||
Justified,
|
Justified,
|
||||||
End,
|
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue