1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#![cfg(feature = "pulldown-cmark")]
use core::ops::Range;
use std::borrow::Cow;
use std::boxed::Box;
use std::string::String;
use std::sync::Arc;
use std::vec::Vec;
use pulldown_cmark::Event;
use crate::TextSource;
#[derive(Clone, Debug, PartialEq)]
pub enum CMarkItem {
Parsed {
event: Event<'static>,
range: Range<usize>,
text_source: TextSource,
},
Created {
event: Event<'static>,
note: Cow<'static, str>,
},
Modified {
event: Event<'static>,
nodes: Box<[Arc<CMarkItem>]>,
note: Cow<'static, str>,
},
Removed {
nodes: Box<[Arc<CMarkItem>]>,
note: Cow<'static, str>,
},
Noted {
node: Arc<CMarkItem>,
note: Cow<'static, str>,
},
}
#[allow(single_use_lifetimes)]
#[derive(Clone, Debug, PartialEq)]
pub struct CMarkSpan<'a> {
pub range: &'a Range<usize>,
pub text_source: &'a TextSource,
pub note: String,
}
impl<'a> CMarkItem {
pub fn new(event: Event<'static>, note: Cow<'static, str>) -> Arc<Self> {
Arc::new(Self::Created { event, note })
}
pub fn from(event: Event<'static>, range: Range<usize>, text_source: TextSource) -> Arc<Self> {
Arc::new(Self::Parsed {
event,
range,
text_source,
})
}
pub fn event(&self) -> Option<&Event<'static>> {
match self {
Self::Parsed { event, .. } => Some(event),
Self::Created { event, .. } => Some(event),
Self::Modified { event, .. } => Some(event),
Self::Removed { .. } => None,
Self::Noted { node, .. } => node.event(),
}
}
pub fn spans(&self) -> Vec<CMarkSpan<'_>> {
match self {
Self::Parsed {
event,
range,
text_source,
} => std::vec![CMarkSpan {
range,
text_source,
note: std::format!("{:?}", event),
}],
Self::Created { .. } => Vec::new(),
Self::Modified { event, nodes, note } => nodes
.iter()
.flat_map(|node| node.spans())
.map(|span| CMarkSpan {
note: span.note + " : " + note + " -> " + &std::format!("{:?}", event),
..span
})
.collect(),
Self::Removed { nodes, note } => nodes
.iter()
.flat_map(|node| node.spans())
.map(|span| CMarkSpan {
note: span.note + " : " + note,
..span
})
.collect(),
Self::Noted { node, note } => node
.spans()
.into_iter()
.map(|span| CMarkSpan {
note: span.note + " : " + note,
..span
})
.collect(),
}
}
}
pub trait CMarkItemAsModified {
fn into_modified(self, event: Event<'static>, note: Cow<'static, str>) -> Arc<CMarkItem>;
}
pub trait CMarkItemAsRemoved {
fn into_removed(self, note: Cow<'static, str>) -> Arc<CMarkItem>;
}
pub trait CMarkItemWithNote {
fn with_note(self, note: Cow<'static, str>) -> Arc<CMarkItem>;
}
impl CMarkItemAsModified for Arc<CMarkItem> {
fn into_modified(self, event: Event<'static>, note: Cow<'static, str>) -> Arc<CMarkItem> {
Arc::new(CMarkItem::Modified {
nodes: Box::new([self]),
event,
note,
})
}
}
impl CMarkItemAsModified for Box<[Arc<CMarkItem>]> {
fn into_modified(self, event: Event<'static>, note: Cow<'static, str>) -> Arc<CMarkItem> {
assert!(!self.is_empty());
Arc::new(CMarkItem::Modified {
nodes: self,
event,
note,
})
}
}
impl CMarkItemAsModified for Vec<Arc<CMarkItem>> {
fn into_modified(self, event: Event<'static>, note: Cow<'static, str>) -> Arc<CMarkItem> {
assert!(!self.is_empty());
Arc::new(CMarkItem::Modified {
nodes: self.into_boxed_slice(),
event,
note,
})
}
}
impl CMarkItemAsRemoved for Arc<CMarkItem> {
fn into_removed(self, note: Cow<'static, str>) -> Arc<CMarkItem> {
Arc::new(CMarkItem::Removed {
nodes: Box::new([self]),
note,
})
}
}
impl CMarkItemAsRemoved for Box<[Arc<CMarkItem>]> {
fn into_removed(self, note: Cow<'static, str>) -> Arc<CMarkItem> {
assert!(!self.is_empty());
Arc::new(CMarkItem::Removed { nodes: self, note })
}
}
impl CMarkItemAsRemoved for Vec<Arc<CMarkItem>> {
fn into_removed(self, note: Cow<'static, str>) -> Arc<CMarkItem> {
assert!(!self.is_empty());
Arc::new(CMarkItem::Removed {
nodes: self.into_boxed_slice(),
note,
})
}
}
impl CMarkItemWithNote for Arc<CMarkItem> {
fn with_note(self, note: Cow<'static, str>) -> Arc<CMarkItem> {
Arc::new(CMarkItem::Noted { node: self, note })
}
}