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
use failure::{Error, Fail};
use rayon::iter::{FromParallelIterator, IntoParallelIterator};
use reqwest::StatusCode;
use std::fmt::{self, Display, Formatter};
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use url::Url;

/// The error which were generated while checking links.
#[derive(Debug, Fail)]
#[fail(display = "there are broken links")]
pub struct BrokenLinks(Vec<Box<BrokenLink>>);

impl BrokenLinks {
    pub fn links(&self) -> &[Box<BrokenLink>] {
        &self.0
    }
}

impl FromParallelIterator<Box<BrokenLink>> for BrokenLinks {
    fn from_par_iter<I>(par_iter: I) -> Self
    where
        I: IntoParallelIterator<Item = Box<BrokenLink>>,
    {
        BrokenLinks(Vec::from_par_iter(par_iter))
    }
}

impl FromIterator<Box<BrokenLink>> for BrokenLinks {
    fn from_iter<I: IntoIterator<Item = Box<BrokenLink>>>(it: I) -> BrokenLinks {
        BrokenLinks(it.into_iter().collect())
    }
}

impl IntoIterator for BrokenLinks {
    type Item = Box<BrokenLink>;
    type IntoIter = <Vec<Self::Item> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

/// An iterator over all the links in [`BrokenLinks`].
///
/// [`BrokenLinks`]: struct.BrokenLinks.html
pub struct Links<'a> {
    parent: &'a BrokenLinks,
    cursor: usize,
}

impl<'a> Iterator for Links<'a> {
    type Item = &'a BrokenLink;

    fn next(&mut self) -> Option<Self::Item> {
        let item = self.parent.0.get(self.cursor).map(|b| &**b);
        self.cursor += 1;
        item
    }
}

impl<'a> IntoIterator for &'a BrokenLinks {
    type Item = &'a BrokenLink;
    type IntoIter = Links<'a>;

    fn into_iter(self) -> Self::IntoIter {
        Links {
            parent: self,
            cursor: 0,
        }
    }
}

/// Some arbitrary broken link which occurs at a specific line in a chapter. The
/// `Display` impl should state why the link is "broken".
pub trait BrokenLink: Fail {
    /// Which chapter it was in.
    fn chapter(&self) -> &Path;
    /// The line this error occurred on.
    fn line(&self) -> usize;
    fn as_fail(&self) -> &Fail;
}

macro_rules! impl_broken_link {
    ($name:ty) => {
        impl BrokenLink for $name {
            fn line(&self) -> usize {
                self.line
            }

            fn chapter(&self) -> &Path {
                &self.chapter
            }

            fn as_fail(&self) -> &Fail {
                self
            }
        }
    };
}

impl_broken_link!(EmptyLink);
impl_broken_link!(FileNotFound);
impl_broken_link!(HttpError);
impl_broken_link!(UnsuccessfulStatus);
impl_broken_link!(ForbiddenPath);

/// The user specified a file which doesn't exist.
#[derive(Debug, Clone, PartialEq, Fail)]
pub struct EmptyLink {
    pub chapter: PathBuf,
    pub line: usize,
}

impl EmptyLink {
    pub(crate) fn new<P>(chapter: P, line: usize) -> EmptyLink
    where
        P: Into<PathBuf>,
    {
        let chapter = chapter.into();

        EmptyLink { chapter, line }
    }
}

impl Display for EmptyLink {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "The link is empty")
    }
}

/// Received an unsuccessful status code when fetching a resource from the
/// internet.
#[derive(Debug, Clone, PartialEq, Fail)]
pub struct UnsuccessfulStatus {
    pub url: Url,
    pub code: StatusCode,
    pub chapter: PathBuf,
    pub line: usize,
}

impl UnsuccessfulStatus {
    pub(crate) fn new<P>(url: Url, code: StatusCode, chapter: P, line: usize) -> UnsuccessfulStatus
    where
        P: Into<PathBuf>,
    {
        let chapter = chapter.into();

        UnsuccessfulStatus {
            url,
            code,
            chapter,
            line,
        }
    }
}

impl Display for UnsuccessfulStatus {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "\"{}\" returned {}", self.url, self.code,)
    }
}

/// The user specified a file which doesn't exist.
#[derive(Debug, Clone, PartialEq, Fail)]
pub struct FileNotFound {
    pub path: PathBuf,
    pub chapter: PathBuf,
    pub line: usize,
}

impl FileNotFound {
    pub(crate) fn new<P, Q>(path: P, chapter: Q, line: usize) -> FileNotFound
    where
        P: Into<PathBuf>,
        Q: Into<PathBuf>,
    {
        let path = path.into();
        let chapter = chapter.into();

        FileNotFound {
            path,
            chapter,
            line,
        }
    }
}

impl Display for FileNotFound {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "\"{}\" doesn't exist", self.path.display(),)
    }
}

/// An error occurred while trying to fetch the link from the internet.
#[derive(Debug, Fail)]
pub struct HttpError {
    pub url: Url,
    pub chapter: PathBuf,
    pub line: usize,
    pub error: Error,
}

impl HttpError {
    pub(crate) fn new<P, E>(url: Url, chapter: P, line: usize, error: E) -> HttpError
    where
        P: Into<PathBuf>,
        E: Into<Error>,
    {
        let chapter = chapter.into();
        let error = error.into();

        HttpError {
            url,
            chapter,
            line,
            error,
        }
    }
}

impl Display for HttpError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "There was an error while fetching \"{}\", {}",
            self.url, self.error,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Fail)]
pub struct ForbiddenPath {
    pub path: PathBuf,
    pub chapter: PathBuf,
    pub line: usize,
}

impl ForbiddenPath {
    pub(crate) fn new<P, Q>(path: P, chapter: Q, line: usize) -> ForbiddenPath
    where
        P: Into<PathBuf>,
        Q: Into<PathBuf>,
    {
        let path = path.into();
        let chapter = chapter.into();

        ForbiddenPath {
            path,
            chapter,
            line,
        }
    }
}

impl Display for ForbiddenPath {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "\"{}\" goes outside the book's source directory",
            self.path.display(),
        )
    }
}