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
97impl Deref for Document {
98    type Target = Node;
99    #[inline]
100    fn deref(&self) -> &Node {
101        &self.root
102    }
103}
104
105impl DerefMut for Document {
106    #[inline]
107    fn deref_mut(&mut self) -> &mut Node {
108        &mut self.root
109    }
110}
111
112impl From<Node> for Document {
113    #[inline]
114    fn from(root: Node) -> Self {
115        Document { root }
116    }
117}