From ce20464337c413e375b531ef07cc29481f5eeaef Mon Sep 17 00:00:00 2001 From: trivernis <trivernis@protonmail.com> Date: Sun, 13 Aug 2023 00:00:40 +0200 Subject: [PATCH] (wip) add content loader --- src/content_loader.rs | 56 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 2 files changed, 58 insertions(+) create mode 100644 src/content_loader.rs diff --git a/src/content_loader.rs b/src/content_loader.rs new file mode 100644 index 0000000..f9f0818 --- /dev/null +++ b/src/content_loader.rs @@ -0,0 +1,56 @@ +use std::{collections::HashMap, path::PathBuf}; + +use async_walkdir::{Filtering, WalkDir}; +use futures::{future, StreamExt}; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct TemplateName(String); + +impl AsRef<str> for TemplateName { + fn as_ref(&self) -> &str { + &self.0 + } +} + +#[derive(Deserialize)] +pub struct Page { + template: TemplateName, + #[serde(flatten)] + data: HashMap<String, toml::Value>, +} + +pub struct ContentLoader { + path: PathBuf, +} + +impl ContentLoader { + pub fn new(path: PathBuf) -> Self { + Self { path } + } + + async fn load_pages(&self) -> Vec<Page> { + todo!() + } + + async fn find_files(&self) -> Vec<PathBuf> { + WalkDir::new(&self.path) + .filter(|e| async move { + e.path() + .extension() + .map(|e| { + if e == "toml" { + Filtering::Continue + } else { + Filtering::Ignore + } + }) + .unwrap_or(Filtering::Ignore) + }) + .map(|e| e.expect("failed to read dir").path()) + .collect::<Vec<_>>() + .await + } +} + +fn parse_page(path: PathBuf) {} diff --git a/src/lib.rs b/src/lib.rs index f5d7310..836661a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,9 @@ use std::path::PathBuf; use config::ConfigLoader; use miette::Result; + mod config; +mod content_loader; #[derive(Debug)] pub struct Paths {