tiptap_rusty_parser/
document.rs1use 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#[derive(Debug, Clone, PartialEq)]
15pub struct Document {
16 root: Node,
17}
18
19impl Document {
20 #[inline]
22 pub fn new(root: Node) -> Self {
23 Document { root }
24 }
25
26 pub fn from_json_str(s: &str) -> Result<Self> {
34 Ok(Document {
35 root: serde_json::from_str(s)?,
36 })
37 }
38
39 pub fn from_value(value: Value) -> Result<Self> {
41 Ok(Document {
42 root: serde_json::from_value(value)?,
43 })
44 }
45
46 pub fn from_reader(reader: impl Read) -> Result<Self> {
48 Ok(Document {
49 root: serde_json::from_reader(reader)?,
50 })
51 }
52
53 pub fn to_json_str(&self) -> Result<String> {
55 Ok(serde_json::to_string(&self.root)?)
56 }
57
58 pub fn to_string_pretty(&self) -> Result<String> {
60 Ok(serde_json::to_string_pretty(&self.root)?)
61 }
62
63 pub fn to_value(&self) -> Result<Value> {
65 Ok(serde_json::to_value(&self.root)?)
66 }
67
68 #[inline]
70 pub fn root(&self) -> &Node {
71 &self.root
72 }
73
74 #[inline]
76 pub fn root_mut(&mut self) -> &mut Node {
77 &mut self.root
78 }
79
80 #[inline]
82 pub fn into_root(self) -> Node {
83 self.root
84 }
85
86 pub fn diff(&self, other: &Document) -> Vec<Change> {
88 self.root.diff(&other.root)
89 }
90
91 pub fn apply(&mut self, changes: &[Change]) -> std::result::Result<(), ApplyError> {
93 crate::diff::apply(&mut self.root, changes)
94 }
95
96 pub fn invert(&self, changes: &[Change]) -> std::result::Result<Vec<Change>, ApplyError> {
98 crate::diff::invert(&self.root, changes)
99 }
100
101 pub fn normalize(&mut self) {
103 self.root.normalize();
104 }
105
106 pub fn normalize_with(&mut self, opts: &crate::NormalizeOptions) {
108 self.root.normalize_with(opts);
109 }
110
111 pub fn to_html(&self) -> String {
113 self.root.to_html()
114 }
115
116 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}