aboutsummaryrefslogtreecommitdiff
path: root/src/generator
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-01-05 15:48:16 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-01-05 15:48:16 +0100
commitb17907faf8d9693cef94a6048d802bd4ced9102f (patch)
tree1bd87c40e85af675a96b63672163b4caaa174364 /src/generator
parentd7fef30ac3f539975ef9edbba8e0af4a4e9ff3de (diff)
Deal with all lints
Diffstat (limited to 'src/generator')
-rw-r--r--src/generator/html.rs27
-rw-r--r--src/generator/mod.rs18
-rw-r--r--src/generator/rss.rs27
-rw-r--r--src/generator/static_files.rs11
-rw-r--r--src/generator/txt.rs27
5 files changed, 49 insertions, 61 deletions
diff --git a/src/generator/html.rs b/src/generator/html.rs
index 7b36944..70cc320 100644
--- a/src/generator/html.rs
+++ b/src/generator/html.rs
@@ -1,25 +1,22 @@
-use crate::template::{find, parse, TemplateContext};
+use crate::template::{find, parse, Context};
use std::fs::write;
use std::io::{Error, ErrorKind::Other, Result};
-use std::path::PathBuf;
+use std::path::Path;
const FILENAME: &str = "index.html";
pub fn generate(
- _: &PathBuf,
- template_directory: &PathBuf,
- target: &PathBuf,
- context: &TemplateContext,
+ _: &Path,
+ template_directory: &Path,
+ target: &Path,
+ context: &Context,
) -> Result<()> {
- match find(template_directory, FILENAME) {
- Some(template) => {
- let parsed_template = parse(&template)
- .ok_or_else(|| Error::new(Other, "Unable to parse HTML template"))?;
- let rendered_template = parsed_template.render(context)?;
- let location = target.join(FILENAME);
- write(location, rendered_template)?;
- }
- None => {}
+ if let Some(template) = find(template_directory, FILENAME) {
+ let parsed_template =
+ parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse HTML template"))?;
+ let rendered_template = parsed_template.render(context)?;
+ let location = target.join(FILENAME);
+ write(location, rendered_template)?;
}
Ok(())
}
diff --git a/src/generator/mod.rs b/src/generator/mod.rs
index 293ad7d..dbe1c2e 100644
--- a/src/generator/mod.rs
+++ b/src/generator/mod.rs
@@ -4,18 +4,20 @@ mod static_files;
mod txt;
use crate::post::Post;
-use crate::template::TemplateContext;
+use crate::template::Context;
use std::io::Result;
-use std::path::PathBuf;
+use std::path::Path;
+
+type Generator = fn(&Path, &Path, &Path, &Context) -> Result<()>;
pub fn generate(
- static_directory: &PathBuf,
- template_directory: &PathBuf,
- output_directory: &PathBuf,
- posts: &Vec<Post>,
+ static_directory: &Path,
+ template_directory: &Path,
+ output_directory: &Path,
+ posts: &[Post],
) -> Result<()> {
let generators = available_generators();
- let context = Post::to_template_context(&posts);
+ let context = Post::to_template_context(posts);
for generator in generators {
generator(
static_directory,
@@ -27,7 +29,7 @@ pub fn generate(
Ok(())
}
-fn available_generators() -> Vec<fn(&PathBuf, &PathBuf, &PathBuf, &TemplateContext) -> Result<()>> {
+fn available_generators() -> Vec<Generator> {
vec![
static_files::generate,
// These three are actually the same. Can generalize, don't know how in rust yet.
diff --git a/src/generator/rss.rs b/src/generator/rss.rs
index 9705f8e..e79694e 100644
--- a/src/generator/rss.rs
+++ b/src/generator/rss.rs
@@ -1,25 +1,22 @@
-use crate::template::{find, parse, TemplateContext};
+use crate::template::{find, parse, Context};
use std::fs::write;
use std::io::{Error, ErrorKind::Other, Result};
-use std::path::PathBuf;
+use std::path::Path;
const FILENAME: &str = "feed.xml";
pub fn generate(
- _: &PathBuf,
- template_directory: &PathBuf,
- target: &PathBuf,
- context: &TemplateContext,
+ _: &Path,
+ template_directory: &Path,
+ target: &Path,
+ context: &Context,
) -> Result<()> {
- match find(template_directory, FILENAME) {
- Some(template) => {
- let parsed_template = parse(&template)
- .ok_or_else(|| Error::new(Other, "Unable to parse RSS template"))?;
- let rendered_template = parsed_template.render(context)?;
- let location = target.join(FILENAME);
- write(location, rendered_template)?;
- }
- None => {}
+ if let Some(template) = find(template_directory, FILENAME) {
+ let parsed_template =
+ parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse RSS template"))?;
+ let rendered_template = parsed_template.render(context)?;
+ let location = target.join(FILENAME);
+ write(location, rendered_template)?;
}
Ok(())
}
diff --git a/src/generator/static_files.rs b/src/generator/static_files.rs
index 8c55de3..b4ec9fd 100644
--- a/src/generator/static_files.rs
+++ b/src/generator/static_files.rs
@@ -1,14 +1,9 @@
-use crate::template::TemplateContext;
+use crate::template::Context;
use crate::utils::recursively_copy;
use std::io::Result;
-use std::path::PathBuf;
+use std::path::Path;
-pub fn generate(
- source: &PathBuf,
- _: &PathBuf,
- target: &PathBuf,
- _: &TemplateContext,
-) -> Result<()> {
+pub fn generate(source: &Path, _: &Path, target: &Path, _: &Context) -> Result<()> {
if source.exists() {
return recursively_copy(source, target);
}
diff --git a/src/generator/txt.rs b/src/generator/txt.rs
index e146c15..0a53655 100644
--- a/src/generator/txt.rs
+++ b/src/generator/txt.rs
@@ -1,25 +1,22 @@
-use crate::template::{find, parse, TemplateContext};
+use crate::template::{find, parse, Context};
use std::fs::write;
use std::io::{Error, ErrorKind::Other, Result};
-use std::path::PathBuf;
+use std::path::Path;
const FILENAME: &str = "index.txt";
pub fn generate(
- _: &PathBuf,
- template_directory: &PathBuf,
- target: &PathBuf,
- context: &TemplateContext,
+ _: &Path,
+ template_directory: &Path,
+ target: &Path,
+ context: &Context,
) -> Result<()> {
- match find(template_directory, FILENAME) {
- Some(template) => {
- let parsed_template = parse(&template)
- .ok_or_else(|| Error::new(Other, "Unable to parse TXT template"))?;
- let rendered_template = parsed_template.render(context)?;
- let location = target.join(FILENAME);
- write(location, rendered_template)?;
- }
- None => {}
+ if let Some(template) = find(template_directory, FILENAME) {
+ let parsed_template =
+ parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse TXT template"))?;
+ let rendered_template = parsed_template.render(context)?;
+ let location = target.join(FILENAME);
+ write(location, rendered_template)?;
}
Ok(())
}