use super::{ContentMatch, Fragment, Mark, MarkSet, Node, NodeType, Text};
use derivative::Derivative;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
pub trait Schema: Sized + 'static {
type Mark: Mark<Self>;
type MarkType: MarkType;
type Node: Node<Self>;
type NodeType: NodeType<Self>;
type ContentMatch: ContentMatch<Self>;
fn find_linebreak_replacement_type(&self) -> Option<Self::NodeType> {
None
}
}
#[derive(Derivative, Deserialize, Serialize)]
#[derivative(
Debug(bound = ""),
Clone(bound = ""),
Default(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
#[serde(bound = "")]
pub struct Block<S: Schema> {
#[serde(default)]
#[derivative(Debug(bound = ""))]
pub content: Fragment<S>,
}
impl<S: Schema> Block<S> {
pub fn copy<F>(&self, map: F) -> Self
where
F: FnOnce(&Fragment<S>) -> Fragment<S>,
{
Block {
content: map(&self.content),
}
}
}
#[derive(Derivative, Deserialize, Serialize)]
#[derivative(
Debug(bound = "A: Debug"),
Clone(bound = "A: Clone"),
Default(bound = "A: Default"),
PartialEq(bound = "A: PartialEq"),
Eq(bound = "A: Eq")
)]
#[serde(bound = "A: for<'d> Deserialize<'d> + Serialize")]
pub struct AttrNode<S: Schema, A> {
pub attrs: A,
#[serde(default)]
#[derivative(Debug(bound = ""))]
pub content: Fragment<S>,
}
impl<S: Schema, A: Clone> AttrNode<S, A> {
pub fn copy<F>(&self, map: F) -> Self
where
F: FnOnce(&Fragment<S>) -> Fragment<S>,
{
AttrNode {
content: map(&self.content),
attrs: self.attrs.clone(),
}
}
}
#[derive(Derivative, Deserialize, Serialize)]
#[derivative(
Debug(bound = ""),
Clone(bound = ""),
Default(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
#[serde(bound = "")]
pub struct TextNode<S: Schema> {
#[serde(default)]
pub marks: MarkSet<S>,
pub text: Text,
}
impl<S: Schema> TextNode<S> {
pub fn same_markup<'o>(&self, other: &'o S::Node) -> Option<&'o TextNode<S>> {
other.text_node().filter(|x| x.marks == self.marks)
}
pub fn with_text(&self, text: Text) -> Self {
TextNode {
marks: self.marks.clone(),
text,
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct Leaf<A> {
pub attrs: A,
}
pub trait MarkType: Copy + Clone + Debug + PartialEq + Eq + PartialOrd + Ord {
fn rank(self) -> usize {
0
}
fn excludes(self, _other: Self) -> bool {
false
}
fn inclusive(self) -> bool {
true
}
fn check_attrs(self, _attrs: &serde_json::Value) -> Result<(), String> {
Ok(())
}
}