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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
use chrono::{DateTime, Utc}; use mime::Mime; #[cfg(test)] use crate::parser::util::timestamp_rfc2822_lenient; #[cfg(test)] use crate::parser::util::timestamp_rfc3339_lenient; /// Combined model for a syndication feed (i.e. RSS1, RSS 2, Atom, JSON Feed) /// /// The model is based on the Atom standard as a start with RSS1+2 mapped on to it e.g. /// * Atom /// * Feed -> Feed /// * Entry -> Entry /// * RSS 1 + 2 /// * Channel -> Feed /// * Item -> Entry /// /// [Atom spec]: http://www.atomenabled.org/developers/syndication/ /// [RSS 2 spec]: https://validator.w3.org/feed/docs/rss2.html /// [RSS 1 spec]: https://validator.w3.org/feed/docs/rss1.html /// /// Certain elements are not mapped given their limited utility: /// * RSS 2: /// * channel - docs (pointer to the spec), cloud (for callbacks), textInput (text box e.g. for search) /// * item - comments (link to comments on the article), source (pointer to the channel, but our data model links items to a channel) /// * RSS 1: /// * channel - rdf:about attribute (pointer to feed), textinput (text box e.g. for search) #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Feed { /// A unique identifier for this feed /// * Atom (required): Identifies the feed using a universally unique and permanent URI. /// * RSS doesn't require an ID so it is initialised to the hash of the first link or a UUID if not found pub id: String, /// The title of the feed /// * Atom (required): Contains a human readable title for the feed. Often the same as the title of the associated website. This value should not be blank. /// * RSS 1 + 2 (required) "title": The name of the channel. It's how people refer to your service. /// * JSON Feed: is the name of the feed pub title: Option<Text>, /// The time at which the feed was last modified. If not provided in the source, or invalid, it is `None`. /// * Atom (required): Indicates the last time the feed was modified in a significant way. /// * RSS 2 (optional) "lastBuildDate": The last time the content of the channel changed. pub updated: Option<DateTime<Utc>>, /// Atom (recommended): Collection of authors defined at the feed level. /// JSON Feed: specifies the feed author. pub authors: Vec<Person>, /// Description of the feed /// * Atom (optional): Contains a human-readable description or subtitle for the feed (from <subtitle>). /// * RSS 1 + 2 (required): Phrase or sentence describing the channel. /// * JSON Feed: description of the feed pub description: Option<Text>, /// Links to related pages /// * Atom (recommended): Identifies a related Web page. /// * RSS 1 + 2 (required): The URL to the HTML website corresponding to the channel. /// * JSON Feed: the homepage and feed URLs pub links: Vec<Link>, /// Structured classification of the feed /// * Atom (optional): Specifies a category that the feed belongs to. A feed may have multiple category elements. /// * RSS 2 (optional) "category": Specify one or more categories that the channel belongs to. pub categories: Vec<Category>, /// People who have contributed to the feed /// * Atom (optional): Names one contributor to the feed. A feed may have multiple contributor elements. /// * RSS 2 (optional) "managingEditor": Email address for person responsible for editorial content. /// * RSS 2 (optional) "webMaster": Email address for person responsible for technical issues relating to channel. pub contributors: Vec<Person>, /// Information on the software used to build the feed /// * Atom (optional): Identifies the software used to generate the feed, for debugging and other purposes. /// * RSS 2 (optional): A string indicating the program used to generate the channel. pub generator: Option<Generator>, /// A small icon /// * Atom (optional): Identifies a small image which provides iconic visual identification for the feed. /// * JSON Feed: is the URL of an image for the feed suitable to be used in a source list. pub icon: Option<Image>, /// RSS 2 (optional): The language the channel is written in. pub language: Option<String>, /// An image used to visually identify the feed /// * Atom (optional): Identifies a larger image which provides visual identification for the feed. /// * RSS 1 + 2 (optional) "image": Specifies a GIF, JPEG or PNG image that can be displayed with the channel. /// * JSON Feed: is the URL of an image for the feed suitable to be used in a timeline pub logo: Option<Image>, /// RSS 2 (optional): The publication date for the content in the channel. pub published: Option<DateTime<Utc>>, /// Rights restricting content within the feed /// * Atom (optional): Conveys information about rights, e.g. copyrights, held in and over the feed. /// * RSS 2 (optional) "copyright": Copyright notice for content in the channel. pub rights: Option<Text>, /// RSS 2 (optional): It's a number of minutes that indicates how long a channel can be cached before refreshing from the source. pub ttl: Option<u32>, /// The individual items within the feed /// * Atom (optional): Individual entries within the feed (e.g. a blog post) /// * RSS 1+2 (optional): Individual items within the channel. pub entries: Vec<Entry>, } impl Default for Feed { fn default() -> Self { Feed { id: "".into(), title: None, updated: None, authors: Vec::new(), description: None, links: Vec::new(), categories: Vec::new(), contributors: Vec::new(), generator: None, icon: None, language: None, logo: None, published: None, rights: None, ttl: None, entries: Vec::new(), } } } #[cfg(test)] impl Feed { pub fn author(mut self, person: Person) -> Self { self.authors.push(person); self } pub fn category(mut self, category: Category) -> Self { self.categories.push(category); self } pub fn contributor(mut self, person: Person) -> Self { self.contributors.push(person); self } pub fn description(mut self, description: Text) -> Self { self.description = Some(description); self } pub fn entry(mut self, entry: Entry) -> Self { self.entries.push(entry); self } pub fn generator(mut self, generator: Generator) -> Self { self.generator = Some(generator); self } pub fn icon(mut self, image: Image) -> Self { self.icon = Some(image); self } pub fn id(mut self, id: &str) -> Self { self.id = id.to_string(); self } pub fn language(mut self, language: &str) -> Self { self.language = Some(language.to_owned()); self } pub fn link(mut self, link: Link) -> Self { self.links.push(link); self } pub fn logo(mut self, image: Image) -> Self { self.logo = Some(image); self } pub fn published_rfc2822(mut self, pub_date: &str) -> Self { self.published = timestamp_rfc2822_lenient(pub_date); self } pub fn rights(mut self, rights: Text) -> Self { self.rights = Some(rights); self } pub fn title(mut self, title: Text) -> Self { self.title = Some(title); self } pub fn ttl(mut self, ttl: u32) -> Self { self.ttl = Some(ttl); self } pub fn updated(mut self, updated: Option<DateTime<Utc>>) -> Self { self.updated = updated; self } pub fn updated_rfc2822(mut self, updated: &str) -> Self { self.updated = timestamp_rfc2822_lenient(updated); self } pub fn updated_rfc3339(mut self, updated: &str) -> Self { self.updated = timestamp_rfc3339_lenient(updated); self } } /// An item within a feed #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Entry { /// A unique identifier for this item with a feed. If not supplied it is initialised to a hash of the first link or a UUID if not available. /// * Atom (required): Identifies the entry using a universally unique and permanent URI. /// * RSS 2 (optional) "guid": A string that uniquely identifies the item. /// * RSS 1: does not specify a unique ID as a separate item, but does suggest the URI should be "the same as the link" so we use a hash of the link if found /// * JSON Feed: is unique for that item for that feed over time. pub id: String, /// Title of this item within the feed /// * Atom, RSS 1(required): Contains a human readable title for the entry. /// * RSS 2 (optional): The title of the item. /// * JSON Feed: The title of the item. pub title: Option<Text>, /// Time at which this item was last modified. If not provided in the source, or invalid, it is `None`. /// * Atom (required): Indicates the last time the entry was modified in a significant way. /// * RSS doesn't specify this field. /// * JSON Feed: the last modification date of this item pub updated: Option<DateTime<Utc>>, /// Authors of this item /// * Atom (recommended): Collection of authors defined at the entry level. /// * RSS 2 (optional): Email address of the author of the item. /// * JSON Feed: the author of the item pub authors: Vec<Person>, /// The content of the item /// * Atom (recommended): Contains or links to the complete content of the entry. /// * RSS 2 (optional) "enclosure": Describes a media object that is attached to the item. /// * JSON Feed: the html content of the item, or the text content if no html is specified pub content: Option<Content>, /// Links associated with this item /// * Atom (recommended): Identifies a related Web page. /// * RSS 2 (optional): The URL of the item. /// * RSS 1 (required): The item's URL. /// * JSON Feed: the url and external URL for the item is the first items, then each subsequent attachment pub links: Vec<Link>, /// A short summary of the item /// * Atom (recommended): Conveys a short summary, abstract, or excerpt of the entry. /// * RSS 1+2 (optional): The item synopsis. /// * JSON Feed: the summary for the item, or the text content if no summary is provided and both text and html content are specified pub summary: Option<Text>, /// Structured classification of the item /// * Atom (optional): Specifies a category that the entry belongs to. A feed may have multiple category elements. /// * RSS 2 (optional): Includes the item in one or more categories. /// * JSON Feed: the supplied item tags pub categories: Vec<Category>, /// Atom (optional): Names one contributor to the entry. A feed may have multiple contributor elements. pub contributors: Vec<Person>, /// Time at which this item was first published /// * Atom (optional): Contains the time of the initial creation or first availability of the entry. /// * RSS 2 (optional) "pubDate": Indicates when the item was published. /// * JSON Feed: the date at which the item was published pub published: Option<DateTime<Utc>>, /// Atom (optional): If an entry is copied from one feed into another feed, then this contains the source feed metadata. pub source: Option<String>, /// Atom (optional): Conveys information about rights, e.g. copyrights, held in and over the feed. pub rights: Option<Text>, } impl Default for Entry { fn default() -> Self { Entry { id: "".into(), title: None, updated: None, authors: Vec::new(), content: None, links: Vec::new(), summary: None, categories: Vec::new(), contributors: Vec::new(), published: None, source: None, rights: None, } } } #[cfg(test)] impl Entry { pub fn author(mut self, person: Person) -> Self { self.authors.push(person); self } pub fn category(mut self, category: Category) -> Self { self.categories.push(category); self } pub fn content(mut self, content: Content) -> Self { self.content = Some(content); self } pub fn contributor(mut self, person: Person) -> Self { self.contributors.push(person); self } pub fn id(mut self, id: &str) -> Self { self.id = id.to_string(); self } pub fn link(mut self, link: Link) -> Self { self.links.push(link); self } pub fn published_rfc2822(mut self, published: &str) -> Self { self.published = timestamp_rfc2822_lenient(published); self } pub fn published_rfc3339(mut self, published: &str) -> Self { self.published = timestamp_rfc3339_lenient(published); self } pub fn summary(mut self, summary: Text) -> Self { self.summary = Some(summary); self } pub fn title(mut self, title: Text) -> Self { self.title = Some(title); self } pub fn updated(mut self, updated: Option<DateTime<Utc>>) -> Self { self.updated = updated; self } pub fn updated_rfc2822(mut self, updated: &str) -> Self { self.updated = timestamp_rfc2822_lenient(updated); self } pub fn updated_rfc3339(mut self, updated: &str) -> Self { self.updated = timestamp_rfc3339_lenient(updated); self } } /// Represents the category of a feed or entry /// /// [Atom spec]: http://www.atomenabled.org/developers/syndication/#category /// [RSS 2 spec]: https://validator.w3.org/feed/docs/rss2.html#ltcategorygtSubelementOfLtitemgt #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Category { /// The category as a human readable string /// * Atom (required): Identifies the category. /// * RSS 2: The value of the element is a forward-slash-separated string that identifies a hierarchic location in the indicated taxonomy. Processors may establish conventions for the interpretation of categories. /// * JSON Feed: the value of the tag pub term: String, /// Atom (optional): Identifies the categorization scheme via a URI. pub scheme: Option<String>, /// Atom (optional): Provides a human-readable label for display. pub label: Option<String>, } impl Category { pub fn new(term: String) -> Category { Category { term, scheme: None, label: None } } } #[cfg(test)] impl Category { pub fn label(mut self, label: &str) -> Self { self.label = Some(label.to_owned()); self } pub fn scheme(mut self, scheme: &str) -> Self { self.scheme = Some(scheme.to_owned()); self } } /// Content, or link to the content, for a given entry. /// /// [Atom spec]: http://www.atomenabled.org/developers/syndication/#contentElement /// [RSS 2.0]: https://validator.w3.org/feed/docs/rss2.html#ltenclosuregtSubelementOfLtitemgt #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Content { /// Atom /// * If the type attribute ends in +xml or /xml, then an xml document of this type is contained inline. /// * If the type attribute starts with text, then an escaped document of this type is contained inline. /// * Otherwise a base64 encoded document of the indicated media type is contained inline. pub body: Option<String>, /// Type of content /// * Atom: The type attribute is either text, html, xhtml, in which case the content element is defined identically to other text constructs. /// * RSS 2: Type says what its type is, a standard MIME type pub content_type: Mime, /// RSS 2.0: Length of the content in bytes pub length: Option<u64>, /// Source of the content /// * Atom: If the src attribute is present, it represents the URI of where the content can be found. The type attribute, if present, is the media type of the content. /// * RSS 2.0: where the enclosure is located pub src: Option<Link>, } impl Default for Content { fn default() -> Content { Content { body: None, content_type: mime::TEXT_PLAIN, length: None, src: None } } } #[cfg(test)] impl Content { pub fn body(mut self, body: &str) -> Self { self.body = Some(body.to_owned()); self } pub fn content_type(mut self, content_type: &str) -> Self { self.content_type = content_type.parse::<Mime>().unwrap(); self } pub fn length(mut self, length: u64) -> Self { self.length = Some(length); self } pub fn src(mut self, url: &str) -> Self { self.src = Some(Link::new(url.to_owned())); self } } /// Information on the tools used to generate the feed /// /// Atom: Identifies the software used to generate the feed, for debugging and other purposes. #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Generator { /// Atom: Additional data /// RSS 2: A string indicating the program used to generate the channel. pub content: String, /// Atom: Link to the tool pub uri: Option<String>, /// Atom: Tool version pub version: Option<String>, } impl Generator { pub fn new(content: String) -> Generator { Generator { uri: None, version: None, content } } } #[cfg(test)] impl Generator { pub fn uri(mut self, uri: &str) -> Self { self.uri = Some(uri.to_owned()); self } pub fn version(mut self, version: &str) -> Self { self.version = Some(version.to_owned()); self } } /// Represents a a link to an image. /// /// [Atom spec]: http://www.atomenabled.org/developers/syndication/#optionalFeedElements /// [RSS 2 spec]: https://validator.w3.org/feed/docs/rss2.html#ltimagegtSubelementOfLtchannelgt /// [RSS 1 spec]: https://validator.w3.org/feed/docs/rss1.html#s5.4 #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Image { /// Link to the image /// * Atom: The URL to an image or logo /// * RSS 1 + 2: the URL of a GIF, JPEG or PNG image that represents the channel. pub uri: String, /// RSS 1 + 2: describes the image, it's used in the ALT attribute of the HTML <img> tag when the channel is rendered in HTML. pub title: Option<String>, /// RSS 1 + 2: the URL of the site, when the channel is rendered, the image is a link to the site. pub link: Option<Link>, /// RSS 2 (optional): width of the image pub width: Option<u32>, /// RSS 2 (optional): height of the image pub height: Option<u32>, /// RSS 2 (optional): contains text that is included in the TITLE attribute of the link formed around the image in the HTML rendering. pub description: Option<String>, } impl Image { pub fn new(uri: String) -> Image { Image { uri, title: None, link: None, width: None, height: None, description: None } } } #[cfg(test)] impl Image { pub fn description(mut self, description: &str) -> Self { self.description = Some(description.to_owned()); self } pub fn height(mut self, height: u32) -> Self { self.height = Some(height); self } pub fn link(mut self, link: &str) -> Self { self.link = Some(Link::new(link.to_owned())); self } pub fn title(mut self, title: &str) -> Self { self.title = Some(title.to_owned()); self } pub fn width(mut self, width: u32) -> Self { self.width = Some(width); self } } /// Represents a link to an associated resource for the feed or entry. /// /// [Atom spec]: http://www.atomenabled.org/developers/syndication/#link #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Link { /// Link to additional content /// * Atom: The URI of the referenced resource (typically a Web page). /// * RSS 2: The URL to the HTML website corresponding to the channel or item. /// * JSON Feed: the URI to the attachment, feed etc pub href: String, /// A single link relationship type. pub rel: Option<String>, /// Indicates the media type of the resource. pub media_type: Option<String>, /// Indicates the language of the referenced resource. pub href_lang: Option<String>, /// Human readable information about the link, typically for display purposes. pub title: Option<String>, /// The length of the resource, in bytes. pub length: Option<u64>, } impl Link { pub fn new(href: String) -> Link { Link { href, rel: None, media_type: None, href_lang: None, title: None, length: None, } } } #[cfg(test)] impl Link { pub fn href_lang(mut self, lang: &str) -> Self { self.href_lang = Some(lang.to_owned()); self } pub fn length(mut self, length: u64) -> Self { self.length = Some(length); self } pub fn media_type(mut self, media_type: &str) -> Self { self.media_type = Some(media_type.to_owned()); self } pub fn rel(mut self, rel: &str) -> Self { self.rel = Some(rel.to_owned()); self } pub fn title(mut self, title: &str) -> Self { self.title = Some(title.to_owned()); self } } /// Represents an author, contributor etc. /// /// [Atom spec]: http://www.atomenabled.org/developers/syndication/#person #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Person { /// Atom: human-readable name for the person. /// JSON Feed: human-readable name for the person. pub name: String, /// Atom: home page for the person. /// JSON Feed: link to media (Twitter etc) for the person pub uri: Option<String>, /// Atom: An email address for the person. pub email: Option<String>, } impl Person { pub fn new(name: String) -> Person { Person { name, uri: None, email: None } } } #[cfg(test)] impl Person { pub fn email(mut self, email: &str) -> Self { self.email = Some(email.to_owned()); self } pub fn uri(mut self, uri: &str) -> Self { self.uri = Some(uri.to_owned()); self } } /// Textual content, or link to the content, for a given entry. #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Text { pub content_type: Mime, pub src: Option<String>, pub content: String, } impl Text { pub fn new(content: String) -> Text { Text { content_type: mime::TEXT_PLAIN, src: None, content } } } #[cfg(test)] impl Text { pub fn content_type(mut self, content_type: &str) -> Self { self.content_type = content_type.parse::<Mime>().unwrap(); self } }