use std::sync::Arc;
use crate::Attrs;
use crate::MarkTypeId;
use crate::Schema;
#[derive(Clone, Debug, PartialEq)]
pub struct Mark {
pub kind: MarkTypeId,
pub attrs: Attrs,
}
impl Mark {
#[must_use]
pub const fn new(kind: MarkTypeId) -> Self {
Self {
kind,
attrs: Attrs::new(),
}
}
#[must_use]
pub const fn with_attrs(kind: MarkTypeId, attrs: Attrs) -> Self {
Self { kind, attrs }
}
}
#[derive(Clone, Debug, Default)]
pub struct MarkSet(Option<Arc<[Mark]>>);
impl MarkSet {
#[must_use]
pub const fn empty() -> Self {
Self(None)
}
#[must_use]
pub fn from_marks(marks: impl IntoIterator<Item = Mark>) -> Self {
let mut marks: Vec<Mark> = marks.into_iter().collect();
marks.sort_by_key(|mark| mark.kind);
marks.reverse();
marks.dedup_by_key(|mark| mark.kind);
marks.reverse();
Self::from_sorted(marks)
}
#[must_use]
pub fn as_slice(&self) -> &[Mark] {
self.0.as_deref().unwrap_or_default()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_none()
}
#[must_use]
pub fn get(&self, kind: MarkTypeId) -> Option<&Mark> {
let marks = self.as_slice();
let index = marks.binary_search_by_key(&kind, |mark| mark.kind).ok()?;
Some(&marks[index])
}
#[must_use]
pub fn contains(&self, kind: MarkTypeId) -> bool {
self.get(kind).is_some()
}
#[must_use]
pub fn with(&self, mark: &Mark, schema: &Schema) -> Self {
let excludes = &schema.mark(mark.kind).excludes;
let mut marks: Vec<Mark> = self
.as_slice()
.iter()
.filter(|existing| excludes.binary_search(&existing.kind).is_err())
.cloned()
.collect();
let index = marks.partition_point(|existing| existing.kind < mark.kind);
marks.insert(index, mark.clone());
Self::from_sorted(marks)
}
#[must_use]
pub fn without(&self, kind: MarkTypeId) -> Self {
if !self.contains(kind) {
return self.clone();
}
let marks = self
.as_slice()
.iter()
.filter(|mark| mark.kind != kind)
.cloned();
Self::from_sorted(marks.collect())
}
fn from_sorted(marks: Vec<Mark>) -> Self {
if marks.is_empty() {
return Self::empty();
}
Self(Some(marks.into()))
}
}
impl PartialEq for MarkSet {
fn eq(&self, other: &Self) -> bool {
match (&self.0, &other.0) {
(Some(left), Some(right)) => Arc::ptr_eq(left, right) || left == right,
(None, None) => true,
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::MarkSpec;
use crate::schema::BlockSpec;
fn schema() -> Schema {
Schema::builder()
.block(BlockSpec::text("paragraph"))
.mark(MarkSpec::new("bold"))
.mark(MarkSpec::new("italic"))
.mark(MarkSpec::new("code").excludes(&["bold", "italic"]))
.mark(MarkSpec::new("link"))
.build()
.expect("valid schema")
}
fn kind(schema: &Schema, name: &str) -> MarkTypeId {
schema.mark_type(name).expect("mark exists")
}
#[test]
fn keeps_marks_sorted_by_type() {
let schema = schema();
let set = MarkSet::empty()
.with(&Mark::new(kind(&schema, "italic")), &schema)
.with(&Mark::new(kind(&schema, "bold")), &schema);
let kinds: Vec<_> = set.as_slice().iter().map(|mark| mark.kind).collect();
assert_eq!(kinds, [kind(&schema, "bold"), kind(&schema, "italic")]);
}
#[test]
fn adding_replaces_same_type_and_drops_excluded() {
let schema = schema();
let link = kind(&schema, "link");
let first = Mark::with_attrs(link, Attrs::new().with("href", "a"));
let second = Mark::with_attrs(link, Attrs::new().with("href", "b"));
let set =
MarkSet::from_marks([Mark::new(kind(&schema, "bold")), first]).with(&second, &schema);
assert_eq!(set.get(link), Some(&second));
let coded = set.with(&Mark::new(kind(&schema, "code")), &schema);
assert!(!coded.contains(kind(&schema, "bold")));
assert!(coded.contains(link));
}
#[test]
fn removing_the_last_mark_yields_the_empty_set() {
let schema = schema();
let bold = kind(&schema, "bold");
let set = MarkSet::from_marks([Mark::new(bold)]).without(bold);
assert!(set.is_empty());
assert_eq!(set, MarkSet::empty());
}
}