aboutsummaryrefslogtreecommitdiff
path: root/src/archiver/mod.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-04-06 00:57:56 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-04-06 00:57:56 +0200
commitd0f582b98712d967b2f95d0405886d063bd89468 (patch)
treecc644c21278d336772557366bcdd3e46b22065db /src/archiver/mod.rs
parent8b3b94a38b443c50afc5b42cca45db7c18ce280d (diff)
Get stricter clippy
Diffstat (limited to 'src/archiver/mod.rs')
-rw-r--r--src/archiver/mod.rs98
1 files changed, 54 insertions, 44 deletions
diff --git a/src/archiver/mod.rs b/src/archiver/mod.rs
index 25c45a8..db3a841 100644
--- a/src/archiver/mod.rs
+++ b/src/archiver/mod.rs
@@ -5,7 +5,7 @@ mod raw;
use crate::template::{Context, Value};
use std::collections::HashMap;
use std::fs::read_dir;
-use std::io::Result;
+use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
use time::{OffsetDateTime, format_description::FormatItem, macros::format_description};
@@ -19,7 +19,7 @@ struct ArchiveEntry {
type Archiver = fn(&Path, &Path, &Path, &Context) -> Result<()>;
impl ArchiveEntry {
- pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Context {
+ pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Result<Context> {
let mut context = HashMap::new();
let archive_entries_collection = archive_entries
@@ -27,16 +27,20 @@ impl ArchiveEntry {
.map(ArchiveEntry::to_template_value)
.collect();
+ let archive_length: u64 = archive_entries
+ .len()
+ .try_into()
+ .map_err(|_| Error::new(ErrorKind::Other, "Too many items in the archive."))?;
context.insert(
"archive_length".to_string(),
- Value::Unsigned(archive_entries.len().try_into().unwrap()),
+ Value::Unsigned(archive_length),
);
context.insert(
"posts".to_string(),
Value::Collection(archive_entries_collection),
);
- context
+ Ok(context)
}
pub fn to_template_value(&self) -> Context {
@@ -75,22 +79,19 @@ fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
if let Ok(candidates) = read_dir(&entry_path) {
for candidate in candidates.filter_map(Result::ok) {
let candidate_path = candidate.path();
- match candidate_path.extension() {
- Some(extension) => {
- if extension == "gmi" {
- if let Some(slug) = candidate_path.file_stem() {
- if let (Some(post_id), Some(slug)) =
- (post_id.to_str(), slug.to_str())
- {
- archive_entries.push(ArchiveEntry {
- id: post_id.to_string(),
- slug: slug.to_string(),
- });
- }
+ if let Some(extension) = candidate_path.extension() {
+ if extension == "gmi" {
+ if let Some(slug) = candidate_path.file_stem() {
+ if let (Some(post_id), Some(slug)) =
+ (post_id.to_str(), slug.to_str())
+ {
+ archive_entries.push(ArchiveEntry {
+ id: post_id.to_string(),
+ slug: slug.to_string(),
+ });
}
}
}
- _ => continue,
}
}
}
@@ -110,7 +111,7 @@ pub fn archive(
) -> Result<()> {
let archivers = available_archivers();
let archive_entries = read_archive(archive_directory);
- let context = ArchiveEntry::to_template_context(&archive_entries);
+ let context = ArchiveEntry::to_template_context(&archive_entries)?;
for archiver in archivers {
archiver(
archive_directory,
@@ -139,13 +140,15 @@ mod tests {
#[test]
fn test_creates_context_with_empty_archive_entries() {
- let context = ArchiveEntry::to_template_context(&[]);
-
- assert_eq!(context["archive_length"], Value::Unsigned(0));
- if let Value::Collection(posts) = &context["posts"] {
- assert!(posts.is_empty());
+ if let Ok(context) = ArchiveEntry::to_template_context(&[]) {
+ assert_eq!(context.get("archive_length"), Some(&Value::Unsigned(0)));
+ if let Some(Value::Collection(posts)) = &context.get("posts") {
+ assert!(posts.is_empty());
+ } else {
+ panic!("The posts context was not the right type.");
+ }
} else {
- panic!("The posts context was not the right type.");
+ panic!("Could not generate template context.");
}
}
@@ -156,20 +159,22 @@ mod tests {
slug: "the-archiving-shelves-with-a-spinning-lever-to-move-them".to_string(),
};
- let context = ArchiveEntry::to_template_context(&[archive_entry]);
-
- assert_eq!(context["archive_length"], Value::Unsigned(1));
- if let Value::Collection(posts) = &context["posts"] {
- if let Some(post) = posts.first() {
- assert_eq!(
- post["id"],
- Value::String("you-know-what-is-cool".to_string())
- );
+ if let Ok(context) = ArchiveEntry::to_template_context(&[archive_entry]) {
+ assert_eq!(context.get("archive_length"), Some(&Value::Unsigned(1)));
+ if let Some(Value::Collection(posts)) = &context.get("posts") {
+ if let Some(post) = posts.first() {
+ assert_eq!(
+ post["id"],
+ Value::String("you-know-what-is-cool".to_string())
+ );
+ } else {
+ panic!("The template context had no posts");
+ }
} else {
- panic!("The template context had no posts");
+ panic!("The posts context was not the right type.");
}
} else {
- panic!("The posts context was not the right type.");
+ panic!("Could not generate context");
}
}
@@ -183,12 +188,14 @@ mod tests {
let context = archive_entry.to_template_value();
assert_eq!(
- context["id"],
- Value::String("they-always-show-them-in-spy-films".to_string())
+ context.get("id"),
+ Some(&Value::String(
+ "they-always-show-them-in-spy-films".to_string()
+ ))
);
assert_eq!(
- context["slug"],
- Value::String("or-like-universities".to_string())
+ context.get("slug"),
+ Some(&Value::String("or-like-universities".to_string()))
);
assert!(!context.contains_key("title"));
}
@@ -202,14 +209,17 @@ mod tests {
let context = archive_entry.to_template_value();
- assert_eq!(context["id"], Value::String("1736035200000".to_string()));
assert_eq!(
- context["slug"],
- Value::String("need-a-warehouse".to_string())
+ context.get("id"),
+ Some(&Value::String("1736035200000".to_string()))
+ );
+ assert_eq!(
+ context.get("slug"),
+ Some(&Value::String("need-a-warehouse".to_string()))
);
assert_eq!(
- context["title"],
- Value::String("2025-01-05 need a warehouse".to_string())
+ context.get("title"),
+ Some(&Value::String("2025-01-05 need a warehouse".to_string()))
);
}