diff --git a/src/lib.rs b/src/lib.rs index dd4822a..a356e92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,7 +169,6 @@ pub struct Generator { lookup_tree: BTreeMap>, article_pages: Vec<(String, Page)>, downloadables: Downloadables, - today: Date, head: Markup, header: Markup, footer: Markup, @@ -181,8 +180,18 @@ impl Generator { pub async fn new>(dir: P, pages: Vec>) -> Result { let length = pages.len(); + let today = time::OffsetDateTime::now_utc().date(); + let (link_map, lookup_tree, article_pages) = pages .into_iter() + .filter(|page| { + page.properties + .published + .date + .as_ref() + .map(|date| date.start <= today) + .unwrap_or(false) + }) .map(|page| { let date = page .properties @@ -201,10 +210,9 @@ impl Generator { ), (Some(Either::Left(date)), Some(url)) => bail!("Diary currently doesn't support rendering a page with both a date and a URL but page {} has date {} and URL {}", page.id, date, url), (None, None) => bail!("Diary pages must have either a date or a URL"), - (Some(Either::Left(date)), None) => ( - format_day(date, true), - Either::Left(date), - ), + (Some(Either::Left(date)), None) => { + (format_day(date, true), Either::Left(date)) + } (None, Some(url)) => (format!("/{}", url), Either::Right(url)), }; @@ -230,8 +238,6 @@ impl Generator { }, )?; - let today = time::OffsetDateTime::now_utc().date(); - let read_config_file = async { tokio::fs::File::open("config.json") .await @@ -265,7 +271,6 @@ impl Generator { link_map, lookup_tree, article_pages, - today, head, header, footer, @@ -293,15 +298,6 @@ impl Generator { } } - fn filter_unpublished(&self, page: &Page) -> bool { - page.properties - .published - .date - .as_ref() - .map(|date| date.start <= self.today) - .unwrap_or(false) - } - fn render_article( &self, renderer: &HtmlRenderer, @@ -359,7 +355,6 @@ impl Generator { let (current_pages, pages) = range .map(|(_, page)| page) - .filter(|page| self.filter_unpublished(page)) .map(|page| (page.id, page)) .unzip::<_, _, HashSet<_>, Vec<_>>(); @@ -458,7 +453,6 @@ impl Generator { let (current_pages, pages) = range .map(|(_, page)| page) - .filter(|page| self.filter_unpublished(page)) .map(|page| (page.id, page)) .unzip::<_, _, HashSet<_>, Vec<_>>(); @@ -540,7 +534,6 @@ impl Generator { let days = self .lookup_tree .iter() - .filter(|(_, page)| self.filter_unpublished(page)) .map(|(date, page)| { let renderer = HtmlRenderer { heading_anchors: HeadingAnchors::After("#"), @@ -567,11 +560,11 @@ impl Generator { .lookup_tree .range((Bound::Unbounded, Bound::Excluded(date))) .rev() - .find(|(_, page)| self.filter_unpublished(page)); + .next(); let next_page = self .lookup_tree .range((Bound::Excluded(date), Bound::Unbounded)) - .find(|(_, page)| self.filter_unpublished(page)); + .next(); let cover = self.download_cover(page)?; let path = format_day(*date, false); @@ -659,7 +652,6 @@ impl Generator { let years = self .lookup_tree .iter() - .filter(|(_, page)| self.filter_unpublished(page)) .rev() .map(|(&date, page)| IndexMonth { month: (date.year(), date.month()), @@ -791,7 +783,6 @@ impl Generator { let articles = self .article_pages .iter() - .filter(|(_, page)| self.filter_unpublished(page)) .map(|(url, page)| { let renderer = HtmlRenderer { heading_anchors: HeadingAnchors::After("#"), @@ -889,7 +880,7 @@ impl Generator { let published_date = page.properties.published.date.as_ref().map(get_date); let published_date = match published_date { - Some(published_date) if self.filter_unpublished(page) => published_date, + Some(published_date) => published_date, _ => return None, }; diff --git a/tests/bugs.rs b/tests/bugs.rs new file mode 100644 index 0000000..8931ea4 --- /dev/null +++ b/tests/bugs.rs @@ -0,0 +1,37 @@ +mod utils; + +use diary_generator::{Generator, Properties}; +use notion_generator::response::{properties::DateProperty, Page}; +use utils::{function, new_page, TestDir}; + +#[tokio::test] +async fn unpublished_pages_dont_cause_crashes() { + let cwd = TestDir::new(function!()); + + let page = new_page( + "ac3fb543-001f-4be5-a25e-4978abd05b1d", + "unpublished page with no date", + "just a page without a publish date yet", + None, + ); + + Generator::new( + &cwd, + vec![Page { + properties: Properties { + date: DateProperty { + date: None, + ..page.properties.date + }, + published: DateProperty { + date: None, + ..page.properties.published + }, + ..page.properties + }, + ..page + }], + ) + .await + .unwrap(); +} diff --git a/tests/index_page.rs b/tests/index_page.rs index 0dd3363..b08f2cd 100644 --- a/tests/index_page.rs +++ b/tests/index_page.rs @@ -65,32 +65,32 @@ async fn simple_index() { new_page( "cf2bacc9d75c4226aab53601c336f295", "Day 0: Nannou, helping L, and lots of noise", - Time { - original: "2021-11-07".to_string(), - parsed: Either::Left(date!(2021 - 11 - 07)), - }, "Every journey starts with 1 O'clock: assistance. \ I just didn't know mine will also start with noise.", + Some(Time { + original: "2021-11-07".to_string(), + parsed: Either::Left(date!(2021 - 11 - 07)), + }), ), new_page( "ac3fb543001f4be5a25e4978abd05b1d", "Day 1: Down the rabbit hole we go", - Time { + "Alice starts making games by watching trains with the loveliest coding conductor.", + Some(Time { original: "2021-11-08".to_string(), parsed: Either::Left(date!(2021 - 11 - 08)), - }, - "Alice starts making games by watching trains with the loveliest coding conductor.", + }), ), new_page( "ac3fb543001f4be5a25e4978abd05b1d", "Day 2: Enter Bevy & Shaders are hard", - Time { - original: "2021-11-09".to_string(), - parsed: Either::Left(date!(2021 - 11 - 09)), - }, "3 O’clock: departure. \ We are not entering the world of Bevy where we will actually make things happen. \ There’s no turning back now", + Some(Time { + original: "2021-11-09".to_string(), + parsed: Either::Left(date!(2021 - 11 - 09)), + }), ), ], ) diff --git a/tests/utils/page.rs b/tests/utils/page.rs index d321d30..dfa8ebe 100644 --- a/tests/utils/page.rs +++ b/tests/utils/page.rs @@ -6,7 +6,7 @@ use notion_generator::response::{ }; use time::macros::date; -pub fn new(id: &str, title: &str, date: Time, description: &str) -> Page { +pub fn new(id: &str, title: &str, description: &str, date: Option