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
#![cfg(feature = "pulldown-cmark")]
#[cfg(feature = "thiserror")]
use std::path::Path;
use std::sync::Arc;
#[cfg(feature = "thiserror")]
use thiserror::Error;
use crate::{CMarkData, CMarkDataIter, File, Manifest, Package};
#[cfg(feature = "thiserror")]
use crate::{DisallowUrlsWithPrefixError, FileFromPathError};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CMarkReadme<P, M> {
data: CMarkData,
package_path: P,
manifest: M,
}
#[cfg(feature = "thiserror")]
impl<'a> CMarkReadme<&'a Path, &'a Manifest> {
pub fn from_package(package: &'a Package) -> Result<Self, CMarkReadmeFromPackageError> {
let path = package
.relative_readme_path()
.ok_or(CMarkReadmeFromPackageError::NotFound)?;
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_package_path_and_manifest(
file,
package_path,
manifest,
))
}
}
impl<'a> CMarkReadme<(), ()> {
pub fn from_file(file: Arc<File>) -> Self {
Self::from_file_and_package_path_and_manifest(file, (), ())
}
}
impl<'a, P, M> CMarkReadme<P, M> {
pub fn with_package_path(self, package_path: &'a Package) -> CMarkReadme<&'a Package, M> {
CMarkReadme {
data: self.data,
package_path,
manifest: self.manifest,
}
}
pub fn with_manifest(self, manifest: &'a Manifest) -> CMarkReadme<P, &'a Manifest> {
CMarkReadme {
data: self.data,
package_path: self.package_path,
manifest,
}
}
pub fn from_file_and_package_path_and_manifest(
file: Arc<File>,
package_path: P,
manifest: M,
) -> Self {
let data = CMarkData::from_file(file);
Self::from_data_and_package_path_and_manifest(data, package_path, manifest)
}
pub fn from_data_and_package_path_and_manifest(
data: CMarkData,
package_path: P,
manifest: M,
) -> Self {
Self {
data,
package_path,
manifest,
}
}
pub fn 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()
}
fn map<F>(mut self, func: F) -> CMarkReadme<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<CMarkReadme<P, M>, E>
where
F: FnOnce(CMarkData) -> Result<CMarkData, E>,
{
self.data = func(self.data)?;
Ok(self)
}
pub fn concat_texts(self) -> CMarkReadme<P, M> {
self.map(|data| data.concat_texts())
}
pub fn remove_images_only_paragraph<F>(self, predicate: F) -> CMarkReadme<P, M>
where
F: FnMut(&[&str]) -> bool,
{
self.map(|data| data.remove_images_only_paragraph(predicate))
}
#[cfg(feature = "glob")]
pub fn remove_badges_paragraph(self) -> CMarkReadme<P, M> {
self.map(|data| data.remove_badges_paragraph())
}
pub fn remove_section(self, heading: &str, level: u32) -> Self {
self.map(|data| data.remove_section(heading, level))
}
pub fn remove_documentation_section(self) -> Self {
self.map(|data| data.remove_documentation_section())
}
#[cfg(feature = "thiserror")]
pub fn disallow_absolute_blob_links(
self,
repository_url: &str,
) -> Result<CMarkReadme<P, M>, DisallowUrlsWithPrefixError> {
self.map_result(|data| data.disallow_absolute_blob_links(repository_url))
}
pub fn use_absolute_blob_urls(self, repository_url: &str) -> CMarkReadme<P, M> {
self.map(|data| data.use_absolute_blob_urls(repository_url))
}
}
#[cfg(feature = "thiserror")]
impl<'a, P> CMarkReadme<P, &'a Manifest> {
pub fn disallow_absolute_repository_blob_links(
self,
) -> Result<CMarkReadme<P, &'a Manifest>, DisallowAbsoluteRepositoryBlobLinksError> {
let repository = self
.manifest
.package
.repository
.clone()
.ok_or(DisallowAbsoluteRepositoryBlobLinksError::DocsUrlNotFound)?;
Ok(self.disallow_absolute_blob_links(&repository)?)
}
pub fn use_absolute_repository_blob_urls(
self,
) -> Result<CMarkReadme<P, &'a Manifest>, UseAbsoluteRepositoryBlobUrlsError> {
let repository = self
.manifest
.package
.repository
.clone()
.ok_or(UseAbsoluteRepositoryBlobUrlsError::DocsUrlNotFound)?;
Ok(self.use_absolute_blob_urls(&repository))
}
}
#[cfg(feature = "thiserror")]
#[derive(Debug, Error)]
pub enum CMarkReadmeFromPackageError {
#[error(transparent)]
FileError(#[from] FileFromPathError),
#[error("CMarkReadme not found.")]
NotFound,
}
#[cfg(feature = "thiserror")]
#[derive(Clone, Debug, Error)]
pub enum DisallowAbsoluteRepositoryBlobLinksError {
#[error(transparent)]
DisallowUrlsWithPrefixError(#[from] DisallowUrlsWithPrefixError),
#[error("Manifest does not contain package.documentation field")]
DocsUrlNotFound,
}
#[cfg(feature = "thiserror")]
#[derive(Clone, Copy, Debug, Error)]
pub enum UseAbsoluteRepositoryBlobUrlsError {
#[error("Manifest does not contain package.documentation field")]
DocsUrlNotFound,
}