earth 0.0.2

The alternative library of libearth, the shared common library for various Earth Reader apps.
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::borrow::{Borrow, ToOwned};
use std::default::Default;
use std::fmt;
use std::io;
use std::iter::{FromIterator, IntoIterator};
use std::mem::swap;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

use regex::Regex;

use html::ForHtml;
use parser::base::{DecodeResult, XmlElement};
use schema::{FromSchemaReader, Mergeable};
use util::merge_vec;

/// Link element defined in RFC 4287 (section 4.2.7).
///
/// RFC: <https://tools.ietf.org/html/rfc4287#section-4.2.7>.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Link {
    /// The link's required URI.  It corresponds to `href` attribute of
    /// [RFC 4287 (section 4.2.7.1)][rfc-link-1].
    ///
    /// [rfc-link-1]: https://tools.ietf.org/html/rfc4287#section-4.2.7.1
    pub uri: String,

    /// The relation type of the link.  It corresponds to `rel` attribute
    /// of [RFC 4287 (section 4.2.7.2)][rfc-link-2].
    ///
    /// ### See also
    ///
    /// * [Existing rel values][rel-values] --- Microformats Wiki
    ///
    ///   This page contains tables of known HTML ``rel`` values from
    ///   specifications, formats, proposals, brainstorms, and non-trivial
    ///   [POSH][] usage in the wild.  In addition, dropped and rejected
    ///   values are listed at the end for comprehensiveness.
    ///
    /// [rfc-link-2]: https://tools.ietf.org/html/rfc4287#section-4.2.7.2
    /// [rel-values]: http://microformats.org/wiki/existing-rel-values
    /// [POSH]: http://microformats.org/wiki/POSH
    pub relation: String,

    /// The optional hint for the MIME media type of the linked content.
    /// It corresponds to `type` attribute of
    /// [RFC 4287 (section 4.2.7.3)][rfc-link-3].
    ///
    /// [rfc-link-3]: https://tools.ietf.org/html/rfc4287#section-4.2.7.3
    pub mimetype: Option<String>,

    /// The language of the linked content.  It corresponds to `hreflang`
    /// attribute of [RFC 4287 (section 4.2.7.4)][rfc-link-4].
    ///
    /// [rfc-link-4]: https://tools.ietf.org/html/rfc4287#section-4.2.7.4
    pub language: Option<String>,

    /// The title of the linked resource.  It corresponds to `title`
    /// attribute of [RFC 4287 (section 4.2.7.5)][rfc-link-5].
    ///
    /// [rfc-link-5]: https://tools.ietf.org/html/rfc4287#section-4.2.7.5
    pub title: Option<String>,

    /// The optional hint for the length of the linked content in octets.
    /// It corresponds to `length` attribute of [RFC 4287 (section 4.2.7.6)
    /// ][rfc-link-6].
    ///
    /// [rfc-link-6]: https://tools.ietf.org/html/rfc4287#section-4.2.7.6
    pub byte_size: Option<u64>,
}

impl Link {
    pub fn new<T>(uri: T) -> Link
        where T: Into<String>
    {
        Link {
            uri: uri.into(), relation: "alternate".to_owned(),
            mimetype: None, language: None, title: None, byte_size: None
        }   
    }

    /// Whether its `mimetype` is HTML (or XHTML).
    pub fn is_html(&self) -> bool {
        if let Some(ref mimetype) = self.mimetype {
            let pat = Regex::new(r#"^\s*([^;/\s]+/[^;/\s]+)\s*(?:;\s*.*)?$"#).unwrap();
            if let Some(c) = pat.captures(&mimetype[..]) {
                if let Some(mimetype) = c.at(1) {
                    return ["text/html", "application/xhtml+xml"]
                        .contains(&mimetype);
                }
            }
        }
        false
    }
}

impl Default for Link {
    fn default() -> Link { Link::new("") }
}

impl fmt::Display for Link {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.uri)
    }
}

impl<'a> fmt::Display for ForHtml<'a, Link> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "<link rel=\"{}\"", self.relation));
        if let Some(ref mimetype) = self.mimetype {
            try!(write!(f, " type=\"{}\"", mimetype));
        }
        if let Some(ref language) = self.language {
            try!(write!(f, " hreflang=\"{}\"", language));
        }
        try!(write!(f, " href=\"{}\"", self.uri));
        if let Some(ref title) = self.title {
            try!(write!(f, " title=\"{}\"", title));
        }
        write!(f, ">")
    }
}

impl FromSchemaReader for Link {
    fn read_from<B: io::BufRead>(&mut self, element: XmlElement<B>)
                                 -> DecodeResult<()>
    {
        self.uri = try!(element.get_attr("href")).to_owned();
        self.relation = element.get_attr("rel")
                               .unwrap_or("alternate").to_owned();
        self.mimetype = element.get_attr("type").ok()
                               .map(ToOwned::to_owned);
        self.language = element.get_attr("hreflang").ok()
                               .map(ToOwned::to_owned);
        self.title = element.get_attr("title").ok()
                            .map(ToOwned::to_owned);
        self.byte_size = element.get_attr("length").ok()
                                .and_then(|v| FromStr::from_str(v).ok());
        Ok(())
    }
}

pub struct FilterByMimeType<'a, I, T> where I: Iterator<Item=T>, T: Borrow<Link> {
    inner: I,
    condition: Condition<'a>,
}

pub enum Condition<'a> {
    Regex(Regex),
    Simple(&'a str),
}

impl<'a, I, T> Iterator for FilterByMimeType<'a, I, T>
    where I: Iterator<Item=T>, T: Borrow<Link>
{
    type Item = T;

    fn next(&mut self) -> Option<T> {
        loop {
            if let Some(link) = self.inner.next() {
                let matched = if let Some(t) = link.borrow().mimetype.as_ref() {
                    match self.condition {
                        Condition::Regex(ref r) => r.is_match(t),
                        Condition::Simple(ref s) => s == t,
                    }
                } else {
                    false
                };
                if matched {
                    return Some(link);
                }
            } else {
                return None;
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, self.inner.size_hint().1)
    }
}


pub trait LinkIteratorExt<'a>: Iterator<Item=&'a Link> + Sized {
    /// Filter links by their `mimetype` e.g.:
    ///
    /// ```
    /// # use earth::feed::{LinkList, LinkIteratorExt};
    /// # let links = LinkList(Vec::new());
    /// links.iter().filter_by_mimetype("text/html")
    /// # ;
    /// ```
    ///
    /// `pattern` can include wildcards (`*`) as well e.g.:
    ///
    /// ```
    /// # use earth::feed::{LinkList, LinkIteratorExt};
    /// # let links = LinkList(Vec::new());
    /// links.iter().filter_by_mimetype("application/xml+*")
    /// # ;
    /// ```
    fn filter_by_mimetype<'b>(self, pattern: &'b str) ->
        FilterByMimeType<Self, &'a Link>
    {
        use regex;
        let cond = if pattern.contains('*') {
            let mut regex_str = "^".to_string();
            let mut first = true;
            for part in pattern.split('*') {
                if first {
                    first = false
                } else {
                    regex_str.push_str(".+?")
                }
                regex_str.push_str(&regex::quote(part));
            }
            regex_str.push('$');
            let regex = Regex::new(&regex_str);
            let regex = regex.unwrap();
            Condition::Regex(regex)
        } else {
            Condition::Simple(pattern)
        };
        FilterByMimeType { inner: self, condition: cond }
    }

    fn permalink(self) -> Option<&'a Link> {
        let mut result = None;
        let mut score = (false, false);
        for link in self {
            let rel_is_alternate = link.relation == "alternate";
            let new_score = (link.is_html(), rel_is_alternate);
            if score < new_score {
                result = Some(link);
                score = new_score;
            }
        }
        result
    }

    fn favicon(self) -> Option<&'a Link> {
        for link in self {
            if link.relation.split(' ').any(|i| i == "icon") {
                return Some(link);
            }
        }
        None
    }
}

impl<'a, I: Iterator<Item=&'a Link>> LinkIteratorExt<'a> for I { }


#[derive(Default, Debug)]
pub struct LinkList(pub Vec<Link>);

impl LinkList {
    pub fn new() -> LinkList { LinkList(Vec::new()) }
}

impl Deref for LinkList {
    type Target = Vec<Link>;
    fn deref(&self) -> &Vec<Link> { &self.0 }
}

impl DerefMut for LinkList {
    fn deref_mut(&mut self) -> &mut Vec<Link> { &mut self.0 }
}

impl FromIterator<Link> for LinkList {
    fn from_iter<T: IntoIterator<Item=Link>>(iterator: T) -> Self {
        LinkList(FromIterator::from_iter(iterator))
    }
}

impl Mergeable for Vec<Link> {
    fn merge_with(&mut self, mut other: Vec<Link>) {
        swap(self, &mut other);
        merge_vec(self, other.into_iter());
    }
}


#[cfg(test)]
mod test {
    use super::{Link, LinkIteratorExt};

    use std::default::Default;

    use html::ToHtml;

    #[test]
    fn test_link_html_property() {
        let mut link = Link::new("http://dahlia.kr/");
        link.mimetype = Some("text/html".to_string());
        assert!(link.is_html());
        link.mimetype = Some("application/xhtml+xml".to_string());
        assert!(link.is_html());
        link.mimetype = Some("application/xml".to_string());
        assert!(!link.is_html());
    }

    #[test]
    fn test_link_str() {
        let link = Link {
            uri: "http://dahlia.kr/".to_string(),
            relation: "alternate".to_string(),
            mimetype: Some("text/html".to_string()),
            title: Some("Hong Minhee's website".to_string()),
            language: None, byte_size: None,
        };
        assert_eq!(link.to_string(), "http://dahlia.kr/");
    }

    #[test]
    fn test_link_html_method() {
        let link = Link::new("http://dahlia.kr/");
        assert_html!(link,
                     "<link rel=\"alternate\" href=\"http://dahlia.kr/\">");
        let link = Link {
            uri: "http://dahlia.kr/".to_string(),
            relation: "alternate".to_string(),
            mimetype: Some("text/html".to_string()),
            title: Some("Hong Minhee's website".to_string()),
            language: Some("en".to_string()),
            byte_size: None
        };
        assert_html!(link,
                     concat!("<link rel=\"alternate\" type=\"text/html\" ",
                             "hreflang=\"en\" href=\"http://dahlia.kr/\" ",
                             "title=\"Hong Minhee\'s website\">"));
    }

    fn fx_feed_links() -> Vec<Link> {
        vec![
            Link::new("http://example.org/"),
            Link {
                relation: "alternate".to_string(),
                mimetype: Some("text/html".to_string()),
                uri: "http://example.com/index.html".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "alternate".to_string(),
                mimetype: Some("text/html".to_string()),
                uri: "http://example.com/index2.html".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "alternate".to_string(),
                mimetype: Some("text/xml".to_string()),
                uri: "http://example.com/index.xml".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "alternate".to_string(),
                mimetype: Some("application/json".to_string()),
                uri: "http://example.com/index.json".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "alternate".to_string(),
                mimetype: Some("text/javascript".to_string()),
                uri: "http://example.com/index.js".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "alternate".to_string(),
                mimetype: Some("application/xml+atom".to_string()),
                uri: "http://example.com/index.atom".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "alternate".to_string(),  // remove it if available
                mimetype: Some("application/xml+rss".to_string()),
                uri: "http://example.com/index.atom".to_string(),
                title: None, language: None, byte_size: None,
            },
            Link {
                relation: "icon".to_string(),
                mimetype: Some("image/png".to_string()),
                uri: "http://example.com/favicon.png".to_string(),
                title: None, language: None, byte_size: None,
            },
        ]
    }

    #[test]
    fn test_link_list_filter_by_mimetype() {
        let links = fx_feed_links();
        let result: Vec<_> = links.iter()
            .filter_by_mimetype("text/html")
            .collect();
        assert_eq!(result.len(), 2);
        assert_eq!(result.iter()
                   .map(|link| &link.mimetype.as_ref().unwrap()[..])
                   .collect::<Vec<_>>(),
                   ["text/html", "text/html"]);
        let result: Vec<_> = links.iter()
            .filter_by_mimetype("application/*")
            .collect();
        assert_eq!(result.len(), 3);
        assert_eq!(result.iter()
                   .map(|link| &link.mimetype.as_ref().unwrap()[..])
                   .collect::<Vec<_>>(),
                   ["application/json",
                    "application/xml+atom",
                    "application/xml+rss"]);
    }

    #[test]
    fn test_link_list_permalink() {
        let mut links = fx_feed_links();
        let mut other_link = Link::new("http://example.com/");
        other_link.relation = "other".to_string();
        let mut html_link = Link::new("http://example.com/");
        html_link.relation = "other".to_string();
        html_link.mimetype = Some("text/html".to_string());
        links.extend(vec![other_link, html_link.clone()].into_iter());
        assert_eq!(links.iter().permalink(), Some(&links[1]));
        links.remove(1);
        links.remove(1);
        assert_eq!(links.iter().permalink(), Some(&html_link));
        links.pop();
        assert_eq!(links.iter().permalink(), Some(&links[0]));
        assert_eq!(links[links.len() - 1..].iter().permalink(), None);
    }

    #[test]
    fn test_link_list_favicon() {
        let mut links = fx_feed_links();
        assert_eq!(links.iter().favicon(), links.last());
        links[0] = Link {
            relation: "shortcut icon".to_string(),
            uri: "http://example.com/favicon.ico".to_string(),
            ..Default::default()
        };
        assert_eq!(links.iter().favicon(), links.first());
    }
}