From 669d4ead8163547daead5afe576af9282d03f7c6 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Tue, 14 Dec 2021 08:52:40 +0100 Subject: [PATCH] remove unused struct and put its functions as top level --- Cargo.toml | 2 +- benches/benchmark.rs | 6 +- examples/gen_sitemap.rs | 4 +- src/lib.rs | 142 ++++++++++++++++++++-------------------- 4 files changed, 76 insertions(+), 78 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c432664..a80c36b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sitewriter" -version = "0.4.2" +version = "0.5.0" authors = ["Edgar "] edition = "2021" description = "A sitemap writing library." diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 1132d23..4f3c919 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -1,5 +1,5 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use sitewriter::*; +use sitewriter::{ChangeFreq, UrlEntry, UrlEntryBuilder}; fn benchmark(c: &mut Criterion) { use chrono::Utc; @@ -52,11 +52,11 @@ fn benchmark(c: &mut Criterion) { ]; c.bench_function("generate_str", |b| { - b.iter(|| Sitemap::generate_str(black_box(&urls))) + b.iter(|| sitewriter::generate_str(black_box(&urls))) }); c.bench_function("generate_bytes", |b| { - b.iter(|| Sitemap::generate_bytes(black_box(&urls))) + b.iter(|| sitewriter::generate_bytes(black_box(&urls))) }); } diff --git a/examples/gen_sitemap.rs b/examples/gen_sitemap.rs index da6b14f..2729546 100644 --- a/examples/gen_sitemap.rs +++ b/examples/gen_sitemap.rs @@ -1,5 +1,5 @@ use chrono::prelude::*; -use sitewriter::*; +use sitewriter::{ChangeFreq, UrlEntry, UrlEntryBuilder}; fn main() { let urls = vec![ @@ -48,6 +48,6 @@ fn main() { }, ]; - let result = Sitemap::generate_str(&urls).unwrap(); + let result = sitewriter::generate_str(&urls); println!("{}", result); } diff --git a/src/lib.rs b/src/lib.rs index 59c3f6e..aad9774 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ //! //! ```rust //! use chrono::prelude::*; -//! use sitewriter::*; +//! use sitewriter::{ChangeFreq, UrlEntry, UrlEntryBuilder}; //! //! let urls = vec![ //! UrlEntryBuilder::default() @@ -61,12 +61,12 @@ //! }, //! ]; //! -//! let result = Sitemap::generate_str(&urls).unwrap(); +//! let result = sitewriter::generate_str(&urls); //! println!("{}", result); //! ``` -//! +//! //! ## CREV - Rust code reviews - Raise awareness -//! +//! //! Please, spread this info !\ //! Open source code needs a community effort to express trustworthiness.\ //! Start with reading the reviews of the crates you use on [web.crev.dev/rust-reviews/crates/](https://web.crev.dev/rust-reviews/crates/) \ @@ -77,25 +77,20 @@ //! Help other developers, inform them and share your opinion.\ //! Use [cargo_crev_reviews](https://crates.io/crates/cargo_crev_reviews) to write reviews easily. - use chrono::{DateTime, SecondsFormat, Utc}; use derive_builder::Builder; -pub use url; -use url::Url; - use quick_xml::{ events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event}, Writer, }; +use std::{fmt::Display, io::Cursor}; -use quick_xml::Result; -use std::fmt::Display; -use std::io::Cursor; +pub use quick_xml::Result; +pub use url::Url; -pub use quick_xml; - -/// How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page. -#[derive(Debug, Eq, PartialEq, Copy, Clone)] +/// How frequently the page is likely to change. This value provides general +/// information to search engines and may not correlate exactly to how often they crawl the page. +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum ChangeFreq { /// Changes each time it's accessed. Always, @@ -129,7 +124,7 @@ impl Display for ChangeFreq { } /// A sitemap url entry. -#[derive(Debug, Clone, Builder)] +#[derive(Debug, Clone, Builder, PartialEq, PartialOrd)] #[builder(setter(strip_option))] pub struct UrlEntry { /// URL of the page. @@ -150,7 +145,8 @@ pub struct UrlEntry { } impl UrlEntry { - pub fn new( + #[must_use] + pub const fn new( loc: Url, lastmod: Option>, changefreq: Option, @@ -165,10 +161,6 @@ impl UrlEntry { } } -/// Struct that implements the sitemap generation function. -#[derive(Debug)] -pub struct Sitemap; - fn write_tag(writer: &mut Writer, tag: &str, text: &str) -> Result<()> where T: std::io::Write, @@ -180,65 +172,71 @@ where Ok(()) } -impl Sitemap { - /// Generates the sitemap and saves it using the provided writer. - /// - /// It's recommended to use [`Sitemap::into_bytes`] or [`Sitemap::into_str`] if you need a - /// String or a Vec. - pub fn generate(inner_writer: T, urls: &[UrlEntry]) -> Result - where - T: std::io::Write, - { - let mut writer = Writer::new_with_indent(inner_writer, b' ', 4); - writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"UTF-8"), None)))?; +/// Generates the sitemap and saves it using the provided writer. +/// +/// It's recommended to use [`Sitemap::generate_bytes`] or [`Sitemap::generate_str`] if you need a +/// String or a Vec. +/// +/// # Errors +/// +/// Will return `Err` if it fails to write to the writer. +pub fn generate(inner_writer: T, urls: &[UrlEntry]) -> Result +where + T: std::io::Write, +{ + let mut writer = Writer::new_with_indent(inner_writer, b' ', 4); + writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"UTF-8"), None)))?; - let urlset_name = b"urlset"; - let mut urlset = BytesStart::borrowed_name(urlset_name); - urlset.push_attribute(("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")); - writer.write_event(Event::Start(urlset))?; + let urlset_name = b"urlset"; + let mut urlset = BytesStart::borrowed_name(urlset_name); + urlset.push_attribute(("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")); + writer.write_event(Event::Start(urlset))?; - for entry in urls { - writer - .write_event(Event::Start(BytesStart::borrowed_name(b"url"))) - .expect("error opening url"); + for entry in urls { + writer + .write_event(Event::Start(BytesStart::borrowed_name(b"url"))) + .expect("error opening url"); - write_tag(&mut writer, "loc", entry.loc.as_str())?; + write_tag(&mut writer, "loc", entry.loc.as_str())?; - if let Some(lastmod) = &entry.lastmod { - write_tag( - &mut writer, - "lastmod", - &lastmod.to_rfc3339_opts(SecondsFormat::Secs, true), - )?; - } - if let Some(priority) = &entry.priority { - write_tag(&mut writer, "priority", &format!("{:.1}", priority))?; - } - if let Some(changefreq) = &entry.changefreq { - write_tag(&mut writer, "changefreq", &changefreq.to_string())?; - } - - writer.write_event(Event::End(BytesEnd::borrowed(b"url")))?; + if let Some(lastmod) = &entry.lastmod { + write_tag( + &mut writer, + "lastmod", + &lastmod.to_rfc3339_opts(SecondsFormat::Secs, true), + )?; + } + if let Some(priority) = &entry.priority { + write_tag(&mut writer, "priority", &format!("{:.1}", priority))?; + } + if let Some(changefreq) = &entry.changefreq { + write_tag(&mut writer, "changefreq", &changefreq.to_string())?; } - writer.write_event(Event::End(BytesEnd::borrowed(urlset_name)))?; - - Ok(writer.into_inner()) + writer.write_event(Event::End(BytesEnd::borrowed(b"url")))?; } - /// Generates the sitemap. - pub fn generate_bytes(urls: &[UrlEntry]) -> Result> { - let inner = Cursor::new(Vec::new()); - let result = Sitemap::generate(inner, urls)?; - Ok(result.into_inner()) - } + writer.write_event(Event::End(BytesEnd::borrowed(urlset_name)))?; - /// Generates the sitemap returning a string. - pub fn generate_str(urls: &[UrlEntry]) -> Result { - let bytes = Sitemap::generate_bytes(urls)?; - let res = std::str::from_utf8(&bytes).expect("to be valid utf8"); - Ok(res.to_owned()) - } + Ok(writer.into_inner()) +} + +/// Generates the sitemap. +#[must_use] +pub fn generate_bytes(urls: &[UrlEntry]) -> Vec { + let inner = Cursor::new(Vec::new()); + let result = generate(inner, urls).expect( + "it should never error, please report this bug to https://github.com/edg-l/sitewriter/issues", + ); + result.into_inner() +} + +/// Generates the sitemap returning a string. +#[must_use] +pub fn generate_str(urls: &[UrlEntry]) -> String { + let bytes = generate_bytes(urls); + let res = std::str::from_utf8(&bytes).expect("to be valid utf8"); + res.to_owned() } #[cfg(test)] @@ -296,7 +294,7 @@ mod tests { }, ]; - Sitemap::generate_str(&urls).unwrap(); + let _result = generate_str(&urls); } #[test]