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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#![cfg(feature = "pulldown-cmark")]
use std::borrow::Cow;
#[cfg(all(feature = "syn", feature = "thiserror"))]
use std::path::Path;
use std::sync::Arc;
use pulldown_cmark::Event;
#[cfg(feature = "thiserror")]
use thiserror::Error;
#[cfg(feature = "thiserror")]
use crate::DisallowUrlsWithPrefixError;
#[cfg(all(feature = "syn", feature = "thiserror"))]
use crate::FileDocsFromFileError;
use crate::{CMarkData, CMarkDataIter, FileDocs, Manifest, Package};
#[cfg(all(feature = "syn", feature = "thiserror"))]
use crate::{Config, File, FileFromPathError};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CMarkDocs<P, M> {
data: CMarkData,
package_path: P,
manifest: M,
}
#[cfg(all(feature = "syn", feature = "thiserror"))]
impl<'a> CMarkDocs<&'a Path, &'a Manifest> {
pub fn from_package_with_default_config(
package: &'a Package,
) -> Result<Self, CMarkDocsFromPackageError> {
Self::from_package_and_config(package, &Config::default())
}
pub fn from_package_and_config(
package: &'a Package,
config: &Config<'_>,
) -> Result<Self, CMarkDocsFromPackageError> {
let path = package.manifest().default_relative_target_path();
let file = Arc::new(File::from_path(path.to_path_buf(), Some(package.path()))?);
let package_path = package.path();
let manifest = package.manifest();
Ok(Self::from_file_and_config_and_package_path_and_manifest(
file,
config,
package_path,
manifest,
)?)
}
}
#[cfg(all(feature = "syn", feature = "thiserror"))]
impl<'a> CMarkDocs<(), ()> {
pub fn from_file_and_config(
file: Arc<File>,
config: &Config<'_>,
) -> Result<Self, FileDocsFromFileError> {
Self::from_file_and_config_and_package_path_and_manifest(file, config, (), ())
}
}
impl<'a, P, M> CMarkDocs<P, M> {
pub fn with_package_path(self, package_path: &'a Package) -> CMarkDocs<&'a Package, M> {
CMarkDocs {
data: self.data,
package_path,
manifest: self.manifest,
}
}
pub fn with_manifest(self, manifest: &'a Manifest) -> CMarkDocs<P, &'a Manifest> {
CMarkDocs {
data: self.data,
package_path: self.package_path,
manifest,
}
}
#[cfg(all(feature = "syn", feature = "thiserror"))]
pub fn from_file_and_config_and_package_path_and_manifest(
file: Arc<File>,
config: &Config<'_>,
package_path: P,
manifest: M,
) -> Result<Self, FileDocsFromFileError> {
let file_docs = Arc::new(FileDocs::from_file(file, config)?);
Ok(Self::from_file_docs_and_package_path_and_manifest(
file_docs,
package_path,
manifest,
))
}
pub fn from_file_docs_and_package_path_and_manifest(
file_docs: Arc<FileDocs>,
package_path: P,
manifest: M,
) -> Self {
let data = CMarkData::from_file_docs(file_docs);
Self::from_data_chunks_package_pach_and_manifest(data, package_path, manifest)
}
pub fn from_data_chunks_package_pach_and_manifest(
data: CMarkData,
package_path: P,
manifest: M,
) -> Self {
Self {
data,
package_path,
manifest,
}
}
pub fn data(&self) -> &CMarkData {
&self.data
}
pub fn into_data(self) -> CMarkData {
self.data
}
pub fn package_path(&self) -> &P {
&self.package_path
}
pub fn manifest(&self) -> &M {
&self.manifest
}
pub fn iter(&self) -> CMarkDataIter<'_> {
self.data.iter()
}
pub fn iter_events(&self) -> impl Iterator<Item = &Event<'_>> {
self.data.iter().filter_map(|item| item.event())
}
fn map<F>(mut self, func: F) -> CMarkDocs<P, M>
where
F: FnOnce(CMarkData) -> CMarkData,
{
self.data = func(self.data);
self
}
#[cfg(feature = "thiserror")]
fn map_result<F, E>(mut self, func: F) -> Result<CMarkDocs<P, M>, E>
where
F: FnOnce(CMarkData) -> Result<CMarkData, E>,
{
self.data = func(self.data)?;
Ok(self)
}
pub fn concat_texts(self) -> CMarkDocs<P, M> {
self.map(|data| data.concat_texts())
}
pub fn increment_heading_levels(self) -> CMarkDocs<P, M> {
self.map(|data| data.increment_heading_levels())
}
pub fn add_title(self, text: &str) -> CMarkDocs<P, M> {
self.map(|data| data.add_title(text))
}
pub fn remove_section(self, heading: &str, level: u32) -> Self {
self.map(|data| data.remove_section(heading, level))
}
pub fn remove_codeblock_tag(self, tag: &str) -> CMarkDocs<P, M> {
self.map(|data| data.remove_codeblock_tag(tag))
}
pub fn remove_codeblock_tags(self, tags: &[&str]) -> CMarkDocs<P, M> {
self.map(|data| data.remove_codeblock_tags(tags))
}
pub fn remove_codeblock_rust_test_tags(self) -> CMarkDocs<P, M> {
self.map(|data| data.remove_codeblock_rust_test_tags())
}
pub fn use_default_codeblock_tag(self, tag: &str) -> CMarkDocs<P, M> {
self.map(|data| data.use_default_codeblock_tag(tag))
}
pub fn use_default_codeblock_rust_tag(self) -> CMarkDocs<P, M> {
self.map(|data| data.use_default_codeblock_rust_tag())
}
pub fn remove_hidden_rust_code(self) -> CMarkDocs<P, M> {
self.map(|data| data.remove_hidden_rust_code())
}
#[cfg(feature = "thiserror")]
pub fn disallow_absolute_docs_links(
self,
package_name: &str,
documentation_url: &str,
) -> Result<CMarkDocs<P, M>, DisallowUrlsWithPrefixError> {
self.map_result(|data| data.disallow_absolute_docs_links(package_name, documentation_url))
}
pub fn use_absolute_docs_urls(
self,
package_name: &str,
documentation_url: &str,
) -> CMarkDocs<P, M> {
self.map(|data| data.use_absolute_docs_urls(package_name, documentation_url))
}
}
impl<'a, P> CMarkDocs<P, &'a Manifest> {
pub fn add_package_title(self) -> CMarkDocs<P, &'a Manifest> {
let name = self.manifest.package.name.clone();
self.add_title(&name)
}
#[cfg(feature = "thiserror")]
pub fn disallow_absolute_package_docs_links(
self,
) -> Result<CMarkDocs<P, &'a Manifest>, DisallowAbsolutePackageDocsLinksError> {
let name = self.manifest.package.name.clone();
let documentation = self
.manifest
.package
.documentation
.clone()
.ok_or(DisallowAbsolutePackageDocsLinksError::DocsUrlNotFound)?;
Ok(self.disallow_absolute_docs_links(&name, &documentation)?)
}
#[cfg(feature = "thiserror")]
pub fn use_absolute_package_docs_urls(
self,
) -> Result<CMarkDocs<P, &'a Manifest>, UseAbsolutePackageDocsUrlsError> {
let name = self.manifest.package.name.clone();
let documentation = self
.manifest
.package
.documentation
.clone()
.ok_or(UseAbsolutePackageDocsUrlsError::DocsUrlNotFound)?;
Ok(self.use_absolute_docs_urls(&name, &documentation))
}
pub fn map_links<F>(self, func: F, note: impl Into<Cow<'static, str>>) -> Self
where
for<'b> F: FnMut(&'b str) -> Cow<'b, str>,
{
self.map(|data| data.map_links(func, note))
}
}
#[cfg(all(feature = "syn", feature = "thiserror"))]
#[derive(Debug, Error)]
pub enum CMarkDocsFromPackageError {
#[error(transparent)]
FileError(#[from] FileFromPathError),
#[error("Documentation parsing failed: {0}")]
ParseError(#[from] FileDocsFromFileError),
}
#[cfg(feature = "thiserror")]
#[derive(Clone, Debug, Error)]
pub enum DisallowAbsolutePackageDocsLinksError {
#[error(transparent)]
DisallowUrlsWithPrefixError(#[from] DisallowUrlsWithPrefixError),
#[error("Manifest does not contain package.documentation field")]
DocsUrlNotFound,
}
#[cfg(feature = "thiserror")]
#[derive(Clone, Copy, Debug, Error)]
pub enum UseAbsolutePackageDocsUrlsError {
#[error("Manifest does not contain package.documentation field")]
DocsUrlNotFound,
}