Skip to content

Commit

Permalink
Merge pull request #1 from goddtriffin/builders
Browse files Browse the repository at this point in the history
Builders
  • Loading branch information
goddtriffin authored Sep 10, 2022
2 parents f652590 + c59527e commit afdc7ec
Show file tree
Hide file tree
Showing 16 changed files with 719 additions and 368 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sitemap-rs"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
rust-version = "1.63"
authors = ["Todd Everett Griffin <[email protected]>"]
Expand Down
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,6 @@ It is by far fast enough for my use-cases, so I didn't have to reach for anythin
If you like what this library provides, but simply need the ability to parse sitemaps and could also use a speed boost -
please consider pushing a pull request! (Preferably one that replaces `xml-builder` with `quick-xml` lol)

#### Builder pattern

Currently, the only way to create instances of each struct is by using `::new()`.
By implementing the [Builder pattern](https://en.wikipedia.org/wiki/Builder_pattern) on each struct, less work has to be done
on the library users' side as they don't have to throw in many `None` values for each optional field they don't want to use.
Not only would this make the library more ergonomic to use, but it would vastly improve readability (specifically at
each struct initialization point).

This hasn't been prioritized yet as I am currently satisfied with `::new()` for my use cases.
Pull requests are welcome!

#### Codified country codes

In video sitemaps, there is a tag called `<video: restriction>` where the text is a space-delimited list of country codes
Expand Down
32 changes: 10 additions & 22 deletions examples/generate_image_sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,19 @@ use std::path::PathBuf;

fn main() {
let urls: Vec<Url> = vec![
Url::new(
String::from("http://example.com/sample1.html"),
None,
None,
None,
Some(vec![
Url::builder(String::from("http://example.com/sample1.html"))
.images(vec![
Image::new(String::from("http://example.com/image.jpg")),
Image::new(String::from("http://example.com/photo.jpg")),
]),
None,
None,
)
.expect("failed a <url> validation"),
Url::new(
String::from("http://example.com/sample2.html"),
None,
None,
None,
Some(vec![Image::new(String::from(
])
.build()
.expect("failed a <url> validation"),
Url::builder(String::from("http://example.com/sample2.html"))
.images(vec![Image::new(String::from(
"http://example.com/picture.jpg",
))]),
None,
None,
)
.expect("failed a <url> validation"),
))])
.build()
.expect("failed a <url> validation"),
];

let url_set: UrlSet = UrlSet::new(urls).expect("failed a <urlset> validation");
Expand Down
28 changes: 12 additions & 16 deletions examples/generate_news_sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ use sitemap_rs::url_set::UrlSet;
use std::path::PathBuf;

fn main() {
let urls: Vec<Url> = vec![Url::new(
String::from("http://www.example.org/business/article55.html"),
None,
None,
None,
None,
None,
Some(News::new(
Publication::new(String::from("The Example Times"), String::from("en")),
DateTime::from_utc(
NaiveDate::from_ymd(2008, 12, 23).and_hms(0, 0, 0),
FixedOffset::east(0),
),
String::from("Companies A, B in Merger Talks"),
)),
)
let urls: Vec<Url> = vec![Url::builder(String::from(
"http://www.example.org/business/article55.html",
))
.news(News::new(
Publication::new(String::from("The Example Times"), String::from("en")),
DateTime::from_utc(
NaiveDate::from_ymd(2008, 12, 23).and_hms(0, 0, 0),
FixedOffset::east(0),
),
String::from("Companies A, B in Merger Talks"),
))
.build()
.expect("failed a <url> validation")];

let url_set: UrlSet = UrlSet::new(urls).expect("failed a <urlset> validation");
Expand Down
18 changes: 7 additions & 11 deletions examples/generate_url_sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ use sitemap_rs::url_set::UrlSet;
use std::path::PathBuf;

fn main() {
let urls: Vec<Url> = vec![Url::new(
String::from("http://www.example.com/"),
Some(DateTime::from_utc(
let urls: Vec<Url> = vec![Url::builder(String::from("http://www.example.com/"))
.last_modified(DateTime::from_utc(
NaiveDate::from_ymd(2005, 1, 1).and_hms(0, 0, 0),
FixedOffset::east(0),
)),
Some(ChangeFrequency::Monthly),
Some(0.8),
None,
None,
None,
)
.expect("failed a <url> validation")];
))
.change_frequency(ChangeFrequency::Monthly)
.priority(0.8)
.build()
.expect("failed a <url> validation")];

let url_set: UrlSet = UrlSet::new(urls).expect("failed a <urlset> validation");
url_set
Expand Down
107 changes: 53 additions & 54 deletions examples/generate_video_sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,60 @@ use std::collections::HashSet;
use std::path::PathBuf;

fn main() {
let urls: Vec<Url> = vec![Url::new(
String::from("http://www.example.com/videos/some_video_landing_page.html"),
None,
None,
None,
None,
Some(vec![Video::new(
String::from("http://www.example.com/thumbs/123.jpg"),
String::from("Grilling steaks for summer"),
String::from("Alkis shows you how to get perfectly done steaks every time"),
String::from("http://streamserver.example.com/video123.mp4"),
String::from("http://www.example.com/videoplayer.php?video=123"),
Some(600),
Some(DateTime::from_utc(
NaiveDate::from_ymd(2021, 11, 5).and_hms(11, 20, 30),
FixedOffset::east(8 * 3600),
)),
Some(4.2),
Some(12345),
Some(DateTime::from_utc(
NaiveDate::from_ymd(2007, 11, 5).and_hms(11, 20, 30),
FixedOffset::east(8 * 3600),
)),
Some(true),
Some(Restriction::new(
HashSet::from([
String::from("IE"),
String::from("GB"),
String::from("US"),
String::from("CA"),
]),
Relationship::Allow,
)),
Some(Platform::new(
HashSet::from([PlatformType::Web, PlatformType::Tv]),
Relationship::Allow,
)),
Some(true),
Some(Uploader::new(
String::from("GrillyMcGrillserson"),
Some(String::from(
"http://www.example.com/users/grillymcgrillerson",
)),
)),
Some(false),
Some(vec![
String::from("steak"),
String::from("meat"),
String::from("summer"),
String::from("outdoor"),
]),
)
.expect("failed a <video:video> validation")]),
None,
let video: Video = Video::builder(
String::from("http://www.example.com/thumbs/123.jpg"),
String::from("Grilling steaks for summer"),
String::from("Alkis shows you how to get perfectly done steaks every time"),
String::from("http://streamserver.example.com/video123.mp4"),
String::from("http://www.example.com/videoplayer.php?video=123"),
)
.duration(600)
.expiration_date(DateTime::from_utc(
NaiveDate::from_ymd(2021, 11, 5).and_hms(11, 20, 30),
FixedOffset::east(8 * 3600),
))
.rating(4.2)
.view_count(12345)
.publication_date(DateTime::from_utc(
NaiveDate::from_ymd(2007, 11, 5).and_hms(11, 20, 30),
FixedOffset::east(8 * 3600),
))
.family_friendly(true)
.restriction(Restriction::new(
HashSet::from([
String::from("IE"),
String::from("GB"),
String::from("US"),
String::from("CA"),
]),
Relationship::Allow,
))
.platform(Platform::new(
HashSet::from([PlatformType::Web, PlatformType::Tv]),
Relationship::Allow,
))
.requires_subscription(true)
.uploader(Uploader::new(
String::from("GrillyMcGrillserson"),
Some(String::from(
"http://www.example.com/users/grillymcgrillerson",
)),
))
.live(false)
.tags(vec![
String::from("steak"),
String::from("meat"),
String::from("summer"),
String::from("outdoor"),
])
.build()
.expect("failed a <video:video> validation");

let urls: Vec<Url> = vec![Url::builder(String::from(
"http://www.example.com/videos/some_video_landing_page.html",
))
.videos(vec![video])
.build()
.expect("failed a <url> validation")];

let url_set: UrlSet = UrlSet::new(urls).expect("failed a <urlset> validation");
Expand Down
20 changes: 9 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@
//! use sitemap_rs::url_set::UrlSet;
//! use std::path::PathBuf;
//!
//! let urls: Vec<Url> = vec![Url::new(
//! String::from("http://www.example.com/"),
//! Some(DateTime::from_utc(
//! let urls: Vec<Url> = vec![Url::builder(String::from("http://www.example.com/"))
//! .last_modified(DateTime::from_utc(
//! NaiveDate::from_ymd(2005, 1, 1).and_hms(0, 0, 0),
//! FixedOffset::east(0),
//! )),
//! Some(ChangeFrequency::Monthly),
//! Some(0.8),
//! None,
//! None,
//! None,
//! )
//! .expect("failed a <url> validation")];
//! ))
//! .change_frequency(ChangeFrequency::Monthly)
//! .priority(0.8)
//! .build()
//! .expect("failed a <url> validation")];
//!
//! let url_set: UrlSet = UrlSet::new(urls).expect("failed a <urlset> validation");
//! url_set
Expand Down Expand Up @@ -53,10 +49,12 @@ pub mod sitemap;
pub mod sitemap_index;
pub mod sitemap_index_error;
pub mod url;
pub mod url_builder;
pub mod url_error;
pub mod url_set;
pub mod url_set_error;
pub mod video;
pub mod video_builder;
pub mod video_error;

pub const NAMESPACE: &str = "http://www.sitemaps.org/schemas/sitemap/0.9";
Expand Down
14 changes: 5 additions & 9 deletions src/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@ impl Sitemap {
sitemap.add_child(loc)?;

// add <lastmod>, if it exists
match self.last_modified {
None => (),
Some(last_modified) => {
let mut last_mod: XMLElement = XMLElement::new("lastmod");
last_mod.add_text(
last_modified.to_rfc3339_opts(RFC_3339_SECONDS_FORMAT, RFC_3339_USE_Z),
)?;
sitemap.add_child(last_mod)?;
}
if let Some(last_modified) = self.last_modified {
let mut last_mod: XMLElement = XMLElement::new("lastmod");
last_mod
.add_text(last_modified.to_rfc3339_opts(RFC_3339_SECONDS_FORMAT, RFC_3339_USE_Z))?;
sitemap.add_child(last_mod)?;
}

Ok(sitemap)
Expand Down
Loading

0 comments on commit afdc7ec

Please sign in to comment.