Skip to main content

tiptap_rusty_parser/
document.rs

1//! [`Document`] — owning wrapper around a root [`Node`] with (de)serialization.
2
3use crate::diff::{ApplyError, Change};
4use crate::error::Result;
5use crate::node::Node;
6use serde_json::Value;
7use std::io::Read;
8use std::ops::{Deref, DerefMut};
9
10/// An owned Tiptap document (its root node, usually `type: "doc"`).
11///
12/// Derefs to the root [`Node`], so all query/mutation methods are available
13/// directly on a `Document`.
14#[derive(Debug, Clone, PartialEq)]
15pub struct Document {
16    root: Node,
17}
18
19impl Document {
20    /// Wrap an existing root node.
21    #[inline]
22    pub fn new(root: Node) -> Self {
23        Document { root }
24    }
25
26    /// Parse from a JSON string.
27    ///
28    /// ```
29    /// use tiptap_rusty_parser::Document;
30    /// let d = Document::from_json_str(r#"{"type":"doc","content":[]}"#).unwrap();
31    /// assert_eq!(d.node_type.as_deref(), Some("doc"));
32    /// ```
33    pub fn from_json_str(s: &str) -> Result<Self> {
34        Ok(Document {
35            root: serde_json::from_str(s)?,
36        })
37    }
38
39    /// Parse from a `serde_json::Value`.
40    pub fn from_value(value: Value) -> Result<Self> {
41        Ok(Document {
42            root: serde_json::from_value(value)?,
43        })
44    }
45
46    /// Parse from any reader.
47    pub fn from_reader(reader: impl Read) -> Result<Self> {
48        Ok(Document {
49            root: serde_json::from_reader(reader)?,
50        })
51    }
52
53    /// Serialize to a compact JSON string.
54    pub fn to_json_str(&self) -> Result<String> {
55        Ok(serde_json::to_string(&self.root)?)
56    }
57
58    /// Serialize to a pretty JSON string.
59    pub fn to_string_pretty(&self) -> Result<String> {
60        Ok(serde_json::to_string_pretty(&self.root)?)
61    }
62
63    /// Serialize to a `serde_json::Value`.
64    pub fn to_value(&self) -> Result<Value> {
65        Ok(serde_json::to_value(&self.root)?)
66    }
67
68    /// Borrow the root node.
69    #[inline]
70    pub fn root(&self) -> &Node {
71        &self.root
72    }
73
74    /// Mutably borrow the root node.
75    #[inline]
76    pub fn root_mut(&mut self) -> &mut Node {
77        &mut self.root
78    }
79
80    /// Consume into the root node.
81    #[inline]
82    pub fn into_root(self) -> Node {
83        self.root
84    }
85
86    /// Structural diff from this document to `other`. See [`Node::diff`].
87    pub fn diff(&self, other: &Document) -> Vec<Change> {
88        self.root.diff(&other.root)
89    }
90
91    /// Apply a [`Change`] list in place. See [`crate::apply`].
92    pub fn apply(&mut self, changes: &[Change]) -> std::result::Result<(), ApplyError> {
93        crate::diff::apply(&mut self.root, changes)
94    }
95
96    /// Invert a [`Change`] list relative to this document. See [`crate::invert`].
97    pub fn invert(&self, changes: &[Change]) -> std::result::Result<Vec<Change>, ApplyError> {
98        crate::diff::invert(&self.root, changes)
99    }
100
101    /// Normalize the document tree in place. See [`Node::normalize`](crate::Node::normalize).
102    pub fn normalize(&mut self) {
103        self.root.normalize();
104    }
105
106    /// Normalize with custom options. See [`Node::normalize_with`](crate::Node::normalize_with).
107    pub fn normalize_with(&mut self, opts: &crate::NormalizeOptions) {
108        self.root.normalize_with(opts);
109    }
110
111    /// Render to an HTML string. See [`Node::to_html`](crate::Node::to_html).
112    pub fn to_html(&self) -> String {
113        self.root.to_html()
114    }
115
116    /// Render to HTML with custom options. See [`Node::to_html_with`](crate::Node::to_html_with).
117    pub fn to_html_with(&self, opts: &crate::HtmlOptions) -> String {
118        self.root.to_html_with(opts)
119    }
120}
121
122impl Deref for Document {
123    type Target = Node;
124    #[inline]
125    fn deref(&self) -> &Node {
126        &self.root
127    }
128}
129
130impl DerefMut for Document {
131    #[inline]
132    fn deref_mut(&mut self) -> &mut Node {
133        &mut self.root
134    }
135}
136
137impl From<Node> for Document {
138    #[inline]
139    fn from(root: Node) -> Self {
140        Document { root }
141    }
142}