]> git.r.bdr.sh - rbdr/blog/blobdiff - src/archiver/mod.rs
Make this created_on more readable
[rbdr/blog] / src / archiver / mod.rs
index 6f0c28483c7cbe9446bd84e4a9699addea0d58ad..ba8dd691ba422ba110ff0a5792919f0a5a4cb7a1 100644 (file)
@@ -1,62 +1,53 @@
-mod raw;
 mod gemini;
 mod gopher;
 mod gemini;
 mod gopher;
+mod raw;
 
 
+use crate::template::{Context, Value};
 use std::collections::HashMap;
 use std::fs::read_dir;
 use std::io::Result;
 use std::collections::HashMap;
 use std::fs::read_dir;
 use std::io::Result;
-use std::path::PathBuf;
-use time::{OffsetDateTime, format_description::FormatItem, macros::format_description};
-use crate::template::{TemplateContext, TemplateValue};
+use std::path::{Path, PathBuf};
+use time::{format_description::FormatItem, macros::format_description, OffsetDateTime};
 
 const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
 
 struct ArchiveEntry {
     id: String,
 
 const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
 
 struct ArchiveEntry {
     id: String,
-    slug: String
+    slug: String,
 }
 
 }
 
+type Archiver = fn(&Path, &Path, &Path, &Context) -> Result<()>;
+
 impl ArchiveEntry {
 impl ArchiveEntry {
-    pub fn to_template_context(archive_entries: &Vec<ArchiveEntry>) -> TemplateContext {
+    pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Context {
         let mut context = HashMap::new();
 
         let archive_entries_collection = archive_entries
             .iter()
         let mut context = HashMap::new();
 
         let archive_entries_collection = archive_entries
             .iter()
-            .map(|archive_entry| archive_entry.to_template_value())
+            .map(ArchiveEntry::to_template_value)
             .collect();
 
         context.insert(
             "archive_length".to_string(),
             .collect();
 
         context.insert(
             "archive_length".to_string(),
-            TemplateValue::Unsigned(
-                archive_entries.len().try_into().unwrap()
-            )
+            Value::Unsigned(archive_entries.len().try_into().unwrap()),
         );
         context.insert(
             "posts".to_string(),
         );
         context.insert(
             "posts".to_string(),
-            TemplateValue::Collection(archive_entries_collection)
+            Value::Collection(archive_entries_collection),
         );
 
         context
     }
 
         );
 
         context
     }
 
-    pub fn to_template_value(&self) -> TemplateContext {
+    pub fn to_template_value(&self) -> Context {
         let mut context = HashMap::new();
 
         let mut context = HashMap::new();
 
-        context.insert(
-            "id".to_string(),
-            TemplateValue::String(self.id.clone())
-        );
+        context.insert("id".to_string(), Value::String(self.id.clone()));
 
 
-        context.insert(
-            "slug".to_string(),
-            TemplateValue::String(self.slug.clone())
-        );
+        context.insert("slug".to_string(), Value::String(self.slug.clone()));
 
         if let Some(title) = self.title() {
 
         if let Some(title) = self.title() {
-            context.insert(
-                "title".to_string(),
-                TemplateValue::String(title)
-            );
+            context.insert("title".to_string(), Value::String(title));
         }
 
         context
         }
 
         context
@@ -64,17 +55,18 @@ impl ArchiveEntry {
 
     fn title(&self) -> Option<String> {
         let date = OffsetDateTime::from_unix_timestamp_nanos(
 
     fn title(&self) -> Option<String> {
         let date = OffsetDateTime::from_unix_timestamp_nanos(
-            (self.id.parse::<u64>().ok()? * 1_000_000).into()
-        ).ok()?;
+            (self.id.parse::<u64>().ok()? * 1_000_000).into(),
+        )
+        .ok()?;
         let short_date = date.format(&DATE_FORMAT).ok()?;
         let short_date = date.format(&DATE_FORMAT).ok()?;
-        let title = self.slug.replace("-", " ");
-        Some(format!("{} {}", short_date, title))
+        let title = self.slug.replace('-', " ");
+        Some(format!("{short_date} {title}"))
     }
 }
 
 fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
     let mut archive_entries = Vec::new();
     }
 }
 
 fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
     let mut archive_entries = Vec::new();
-    if let Ok(entries) = read_dir(&archive_directory) {
+    if let Ok(entries) = read_dir(archive_directory) {
         for entry in entries.filter_map(Result::ok) {
             let entry_path = entry.path();
             let post_id = entry.file_name();
         for entry in entries.filter_map(Result::ok) {
             let entry_path = entry.path();
             let post_id = entry.file_name();
@@ -87,16 +79,18 @@ fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
                                 Some(extension) => {
                                     if extension == "gmi" {
                                         if let Some(slug) = candidate_path.file_stem() {
                                 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()) {
+                                            if let (Some(post_id), Some(slug)) =
+                                                (post_id.to_str(), slug.to_str())
+                                            {
                                                 archive_entries.push(ArchiveEntry {
                                                     id: post_id.to_string(),
                                                 archive_entries.push(ArchiveEntry {
                                                     id: post_id.to_string(),
-                                                    slug: slug.to_string()
-                                                })
+                                                    slug: slug.to_string(),
+                                                });
                                             }
                                         }
                                     }
                                             }
                                         }
                                     }
-                                },
-                                _ => continue
+                                }
+                                _ => continue,
                             }
                         }
                     }
                             }
                         }
                     }
@@ -105,26 +99,29 @@ fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
         }
     }
 
         }
     }
 
-    archive_entries
-        .sort_by(|a, b| b.id.cmp(&a.id));
+    archive_entries.sort_by(|a, b| b.id.cmp(&a.id));
     archive_entries
 }
 
     archive_entries
 }
 
-
-pub fn archive(archive_directory: &PathBuf, template_directory: &PathBuf, output_directory: &PathBuf) -> Result<()> {
+pub fn archive(
+    archive_directory: &PathBuf,
+    template_directory: &Path,
+    output_directory: &Path,
+) -> Result<()> {
     let archivers = available_archivers();
     let archive_entries = read_archive(archive_directory);
     let context = ArchiveEntry::to_template_context(&archive_entries);
     for archiver in archivers {
     let archivers = available_archivers();
     let archive_entries = read_archive(archive_directory);
     let context = ArchiveEntry::to_template_context(&archive_entries);
     for archiver in archivers {
-        archiver(archive_directory, template_directory, output_directory, &context)?;
+        archiver(
+            archive_directory,
+            template_directory,
+            output_directory,
+            &context,
+        )?;
     }
     }
-    return Ok(())
+    Ok(())
 }
 
 }
 
-fn available_archivers() -> Vec<fn(&PathBuf, &PathBuf, &PathBuf, &TemplateContext) -> Result<()>> {
-    vec![
-        raw::archive,
-        gemini::archive,
-        gopher::archive
-    ]
+fn available_archivers() -> Vec<Archiver> {
+    vec![raw::archive, gemini::archive, gopher::archive]
 }
 }