summaryrefslogtreecommitdiff
path: root/src/works/stories.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/works/stories.rs')
-rw-r--r--src/works/stories.rs60
1 files changed, 28 insertions, 32 deletions
diff --git a/src/works/stories.rs b/src/works/stories.rs
index 80ab8de..f86d24a 100644
--- a/src/works/stories.rs
+++ b/src/works/stories.rs
@@ -1,6 +1,4 @@
-use std::format;
-
-use anyhow::Context;
+use anyhow::{Result, Ok, Context};
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
@@ -29,50 +27,48 @@ impl super::Works for Stories {
&self,
tera_instance: &tera::Tera,
template_name: &str,
- ) -> anyhow::Result<()> {
+ ) -> Result<()> {
let mut result = Vec::new();
for story in self.0.iter() {
- if story.update_available()? {
+ if story.update_available().with_context(|| format!("Failed to check for update for {}", story.title))? {
let mut context = tera::Context::new();
context.insert("story", story);
- context.insert("text", &story.get_text()?);
+ context.insert("text", &story.get_text().with_context(|| format!("Failed to get text from {}", story.title))?);
result.push(std::fs::write(
story.get_html_path().as_str(),
- tera_instance.render(format!("{}.html", template_name).as_str(), &context)?,
+ tera_instance.render(format!("{}.html", template_name).as_str(), &context).with_context(|| format!("Failed to render single story page for {}", story.title))?,
));
}
}
- anyhow::Ok(()).with_context(|| "Failed to render single story pages")
+ Ok(())
}
// create stories from stories.json and add last modification date
- fn new_from_file(file: &std::fs::File) -> anyhow::Result<Self> {
- let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(file))?;
+ fn new_from_file(file: &std::fs::File) -> Result<Self> {
+ let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(file)).with_context(|| format!("Failed to read JSON from {:#?}", file))?;
for story in stories.0.iter_mut() {
- story.get_last_modified()?;
- story.get_pages_num()?;
+ story.get_last_modified().with_context(|| format!("Failed to get last modified date for {}", story.title))?;
+ story.get_pages_num().with_context(|| format!("Failed to get number of pages from {}'s PDF", story.title))?;
}
- // add error context
- anyhow::Ok(stories).with_context(|| format!("Failed to create new story {:?}", file))
+ Ok(stories)
}
}
impl Story {
// check if it's necessary to re-compile the html single page
- fn update_available(&self) -> anyhow::Result<bool> {
- anyhow::Ok(
+ fn update_available(&self) -> Result<bool> {
+ Ok(
// HTML file doesn't exist yet
!std::path::Path::new(&self.get_html_path()).exists()
// pdf document changes
- || std::fs::metadata(self.get_pdf_path())?.modified()?
- > std::fs::metadata(self.get_html_path())?.modified()?
+ || std::fs::metadata(self.get_pdf_path()).with_context(|| format!("Failed to get PDF metadata for {}", self.title))?.modified().with_context(|| format!("Failed to get last modified date for {}'s PDF", self.title))?
+ > std::fs::metadata(self.get_html_path()).with_context(|| format!("Failed to get HTML metadata for {}", self.title))?.modified().with_context(|| format!("Failed to get last modified date for {}'s HTML", self.title))?
// base template changes
- || std::fs::metadata("templates/base.html")?.modified()?
- > std::fs::metadata(self.get_html_path())?.modified()?
+ || std::fs::metadata("templates/base.html").context("Failed to get metadata for base HTML template")?.modified().context("Failed to get last modified date for base HTML template")?
+ > std::fs::metadata(self.get_html_path()).with_context(|| format!("Failed to get HTML metadata for {}", self.title))?.modified().with_context(|| format!("Failed to get last modified date for {}'s HTML", self.title))?
// story template changes
- ||std::fs::metadata("templates/story.html")?.modified()? > std::fs::metadata(self.get_html_path())?.modified()?, // add error context
+ ||std::fs::metadata("templates/story.html").context("Failed to get metadata for story HTML template")?.modified().context("Failed to get last modified date for story HTML template")? > std::fs::metadata(self.get_html_path()).with_context(|| format!("Failed to get HTML metadata for {}", self.title))?.modified().with_context(|| format!("Failed to get last modified date for {}'s HTML", self.title))?, // add error context
)
- .with_context(|| format!("Failed to check for update for {}", self.title))
}
// use story document name for the single page
@@ -98,31 +94,31 @@ impl Story {
}
// extract text from pdf document
- fn get_text(&self) -> anyhow::Result<String> {
- let text = std::fs::read_to_string(self.get_content_html_path())?;
- anyhow::Ok(text).with_context(|| format!("Failed to extract text for {}", self.title))
+ fn get_text(&self) -> Result<String> {
+ let text = std::fs::read_to_string(self.get_content_html_path()).with_context(|| format!("Failed to read {}'s content HTML file", self.title))?;
+ Ok(text)
}
// get date of last modification
- fn get_last_modified(&mut self) -> anyhow::Result<chrono::NaiveDate> {
+ fn get_last_modified(&mut self) -> Result<chrono::NaiveDate> {
use std::os::linux::fs::MetadataExt;
- let pdf_last_modification = std::fs::metadata(self.get_pdf_path())?.st_mtime();
+ let pdf_last_modification = std::fs::metadata(self.get_pdf_path()).with_context(|| format!("Failed to get {}'s PDF", self.title))?.st_mtime();
let html_path = std::fs::metadata(self.get_html_path());
- let html_last_modification = if let Ok(file) = html_path {
+ let html_last_modification = if let core::result::Result::Ok(file) = html_path {
file.st_mtime()
} else {
0
};
let date = chrono::NaiveDateTime::from_timestamp(std::cmp::max(pdf_last_modification, html_last_modification), 0).date();
self.last_update = date;
- anyhow::Ok(date).with_context(|| format!("Failed to get last modified date for {}", self.title))
+ Ok(date)
}
// get number of pages of pdf file
- fn get_pages_num(&mut self) -> anyhow::Result<u32> {
- let file = pdf::file::File::open(self.get_pdf_path())?;
+ fn get_pages_num(&mut self) -> Result<u32> {
+ let file = pdf::file::File::open(self.get_pdf_path()).with_context(|| format!("Failed to get {}'s PDF", self.title))?;
self.number_of_pages = file.num_pages();
- anyhow::Ok(file.num_pages()).with_context(|| format!("Failed to number of pages for {}", self.title))
+ Ok(file.num_pages())
}
}