From dacf9f0b53cb2e8ece0a1f6e5b86424f174679ce Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Sun, 5 Jun 2022 09:46:32 +0100 Subject: [PATCH] Prepublish only sets the publish time if one was not already specified --- src/bin/prepublish/main.rs | 4 +++- src/lib/post/header.rs | 7 +++---- src/lib/post/rendered_post.rs | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bin/prepublish/main.rs b/src/bin/prepublish/main.rs index 33158da..0d83da1 100644 --- a/src/bin/prepublish/main.rs +++ b/src/bin/prepublish/main.rs @@ -29,7 +29,9 @@ fn main() { }, }; - *source.header_mut().published_mut() = Utc::now(); + if source.header().published().is_none() { + *source.header_mut().published_mut() = Some(Utc::now()); + } if let Err(err) = fs::write(&path, source.to_string()) { eprintln!("failed to write {}: {}", path.to_string_lossy(), err); diff --git a/src/lib/post/header.rs b/src/lib/post/header.rs index 09e846a..08d153c 100644 --- a/src/lib/post/header.rs +++ b/src/lib/post/header.rs @@ -13,8 +13,7 @@ pub struct Header { pub(super) author: ShString22, #[serde(default)] pub(super) tags: Vec, - #[serde(default = "crate::time::unix_epoch")] - pub(super) published: DateTime, + pub(super) published: Option>, } impl Header { @@ -68,13 +67,13 @@ impl Header { #[inline] #[must_use] - pub fn published(&self) -> DateTime { + pub fn published(&self) -> Option> { self.published } #[inline] #[must_use] - pub fn published_mut(&mut self) -> &mut DateTime { + pub fn published_mut(&mut self) -> &mut Option> { &mut self.published } } diff --git a/src/lib/post/rendered_post.rs b/src/lib/post/rendered_post.rs index a89f50d..89e9f16 100644 --- a/src/lib/post/rendered_post.rs +++ b/src/lib/post/rendered_post.rs @@ -16,7 +16,7 @@ pub struct RenderedPost { uuid: Uuid, id: Id, header: Header, - updated: DateTime, + updated: Option>, html: Markup, } @@ -50,7 +50,7 @@ impl RenderedPost { uuid, id, header: source.header, - updated: updated.unwrap_or_else(unix_epoch), + updated, html: render_markdown(code_renderer, &source.markdown), }) } @@ -101,12 +101,14 @@ impl RenderedPost { #[must_use] pub fn published(&self) -> DateTime { self.header.published() + .unwrap_or_else(unix_epoch) } #[inline] #[must_use] pub fn updated(&self) -> DateTime { self.updated + .unwrap_or_else(unix_epoch) } #[inline]