Module restructure

This commit is contained in:
Olivia Brooks
2025-10-15 12:40:52 -04:00
parent 3937777255
commit 90ceb2124e
5 changed files with 70 additions and 46 deletions

View File

@@ -1,17 +1,28 @@
use crate::error; use std::{
use std; fs::File,
use std::fs::File; path::{Path, PathBuf},
use std::path::{Path, PathBuf}; };
use chrono::NaiveDate;
use ron::error::SpannedResult; use ron::error::SpannedResult;
use serde::{Deserialize, Deserializer, de::DeserializeOwned}; use serde::{Deserialize, de::DeserializeOwned};
const SANE_DATE_FORMAT: &str = "%Y-%m-%d"; use super::{
og,
res::{Date, Image},
};
use crate::error;
pub static FALLBACK_META_PATH: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new(); pub static FALLBACK_META_PATH: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
static FALLBACK_META: std::sync::OnceLock<MetaFallback> = std::sync::OnceLock::new(); static FALLBACK_META: std::sync::OnceLock<MetaFallback> = std::sync::OnceLock::new();
/// Fallback Meta Setup
pub fn load_fb_meta<P: AsRef<Path>>(path: P) {
FALLBACK_META_PATH.get_or_init(|| path.as_ref().to_path_buf());
// Trigger OnceLock loading of fallback_meta path.
Meta::default();
}
/// Make sure to populate `FALLBACK_META_PATH` before constructing a `Meta`. /// Make sure to populate `FALLBACK_META_PATH` before constructing a `Meta`.
/// ///
/// `MetaFallback` is the same as `Meta`, but without a `Default` impl. /// `MetaFallback` is the same as `Meta`, but without a `Default` impl.
@@ -24,10 +35,15 @@ pub struct Meta {
site_name: String, site_name: String,
locale: String, locale: String,
// `type` is a keyword, this gets around that limitation.
//
// Trailing underscore is apparently a semi-common practice for this, as
// prefixed underscore is reserved by the compiler to indicate
// intentionally unused assignments.
#[serde(rename = "type")] #[serde(rename = "type")]
type_: String, // `type` is a keyword, this gets around that limitation. type_: String,
authors: Vec<crate::og::Author>, authors: Vec<og::Author>,
date: Date, date: Date,
image: Option<Image>, image: Option<Image>,
} }
@@ -60,7 +76,7 @@ struct MetaFallback {
#[serde(rename = "type")] #[serde(rename = "type")]
type_: String, // `type` is a keyword, this gets around that limitation. type_: String, // `type` is a keyword, this gets around that limitation.
authors: Vec<crate::og::Author>, authors: Vec<og::Author>,
date: Date, date: Date,
image: Option<Image>, image: Option<Image>,
} }
@@ -105,6 +121,7 @@ fn meta_try_from_file<T: DeserializeOwned>(file: File) -> SpannedResult<T> {
.from_reader(file) .from_reader(file)
} }
// TryFrom<File>
impl TryFrom<File> for Meta { impl TryFrom<File> for Meta {
type Error = error::MetaError; type Error = error::MetaError;
@@ -121,6 +138,7 @@ impl TryFrom<File> for MetaFallback {
} }
} }
// TryFrom<&Path>
impl TryFrom<&Path> for Meta { impl TryFrom<&Path> for Meta {
type Error = error::MetaError; type Error = error::MetaError;
@@ -144,23 +162,3 @@ impl TryFrom<&Path> for MetaFallback {
) )
} }
} }
#[derive(Clone, Debug, Deserialize)]
pub struct Image(String);
#[derive(Clone, Debug, Deserialize, Default)]
#[serde(default)]
pub struct Date {
#[serde(deserialize_with = "naive_date_from_str")]
posted: NaiveDate,
#[serde(deserialize_with = "naive_date_from_str")]
modified: NaiveDate,
}
fn naive_date_from_str<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error>
where
D: Deserializer<'de>,
{
NaiveDate::parse_from_str(Deserialize::deserialize(deserializer)?, SANE_DATE_FORMAT)
.map_err(serde::de::Error::custom)
}

6
src/blog/mod.rs Normal file
View File

@@ -0,0 +1,6 @@
mod meta;
pub mod og;
mod res;
#[allow(unused_imports)]
pub use meta::{FALLBACK_META_PATH, Meta, load_fb_meta};

View File

@@ -20,12 +20,14 @@ impl Into<String> for Gender {
} }
} }
#[allow(dead_code)]
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
pub struct Author { pub struct Author {
name: Name, name: Name,
gender: Gender, gender: Gender,
} }
#[allow(dead_code)]
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
pub enum Name { pub enum Name {
FirstOnly { FirstOnly {

25
src/blog/res.rs Normal file
View File

@@ -0,0 +1,25 @@
use chrono::NaiveDate;
use serde::{Deserialize, Deserializer};
const SANE_DATE_FORMAT: &str = "%Y-%m-%d";
#[allow(dead_code)]
#[derive(Clone, Debug, Deserialize)]
pub struct Image(String);
#[derive(Clone, Debug, Deserialize, Default)]
#[serde(default)]
pub struct Date {
#[serde(deserialize_with = "naive_date_from_str")]
posted: NaiveDate,
#[serde(deserialize_with = "naive_date_from_str")]
modified: NaiveDate,
}
fn naive_date_from_str<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error>
where
D: Deserializer<'de>,
{
NaiveDate::parse_from_str(Deserialize::deserialize(deserializer)?, SANE_DATE_FORMAT)
.map_err(serde::de::Error::custom)
}

View File

@@ -2,17 +2,15 @@ mod blog;
mod cli; mod cli;
mod convert; mod convert;
mod error; mod error;
mod og; mod lock;
use std::path::PathBuf; use crate::blog::load_fb_meta;
use crate::cli::{Cli, Commands};
use crate::blog::Meta;
use crate::cli::Commands;
use clap::Parser; use clap::Parser;
fn main() { fn main() {
let config = cli::Cli::parse(); let config = Cli::parse();
dbg!(&config); dbg!(&config);
@@ -23,12 +21,16 @@ fn main() {
meta, meta,
post_md, post_md,
template_html, template_html,
} => (), } => {
load_fb_meta(fallback_meta);
}
Commands::Update { lockfile } => (), Commands::Update { lockfile } => (),
Commands::Refresh { lockfile, scope } => (), Commands::Refresh { lockfile, scope } => (),
Commands::Clean => (), Commands::Clean => (),
} }
//println!("Loaded FallbackMeta");
/* /*
load_fb_meta(&config); load_fb_meta(&config);
@@ -52,12 +54,3 @@ fn main() {
.expect("Failed to deserialize single_meta file"); .expect("Failed to deserialize single_meta file");
*/ */
} }
/// Fallback Meta Setup
fn load_fb_meta(path: PathBuf) {
blog::FALLBACK_META_PATH.get_or_init(|| path);
// Trigger OnceLock loading of fallback_meta path.
Meta::default();
println!("Loaded FallbackMeta");
}