From 8f11f1064d778b5e44ff231e934c4eca7e2ad7a9 Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Wed, 1 Jun 2022 17:01:16 +0100 Subject: [PATCH] Dedicated config module --- Cargo.toml | 2 +- src/bin/blog_server/config.rs | 111 ++++++++++++++++++++++++++++++++++ src/bin/blog_server/main.rs | 56 +---------------- src/lib/lib.rs | 3 +- src/lib/uuid.rs | 60 ------------------ 5 files changed, 115 insertions(+), 117 deletions(-) delete mode 100644 src/lib/uuid.rs diff --git a/Cargo.toml b/Cargo.toml index 12a4fc0..e258d68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ maud = "0.23" atom_syndication = "0.11" rss = "2" kdl = "4" -knuffel = "2" +knuffel = "2" # TODO: replace with kdl crate pulldown-cmark = "0.9" syntect = "4" notify = "4" diff --git a/src/bin/blog_server/config.rs b/src/bin/blog_server/config.rs index 8b13789..c819ad2 100644 --- a/src/bin/blog_server/config.rs +++ b/src/bin/blog_server/config.rs @@ -1 +1,112 @@ +use std::{path::PathBuf, ops}; +use knuffel::{ + ast::{Literal, TypeName}, + decode::{Context, Kind}, + errors::{DecodeError, ExpectedType}, + span::Spanned, + traits::ErrorSpan, + DecodeScalar, +}; + +#[derive(knuffel::Decode, Clone, Debug)] +pub struct Config { + #[knuffel(child, unwrap(argument))] + pub bind: String, + #[knuffel(child, unwrap(argument))] + pub concurrency_limit: usize, + #[knuffel(child, unwrap(argument))] + pub static_dir: PathBuf, + #[knuffel(child, unwrap(argument))] + pub favicon_dir: PathBuf, + #[knuffel(child, unwrap(argument))] + pub robots_path: PathBuf, + #[knuffel(child, unwrap(argument))] + pub posts_dir: PathBuf, + #[knuffel(child, unwrap(argument))] + pub post_media_dir: PathBuf, + #[knuffel(child, unwrap(argument))] + pub namespace_uuid: Uuid, + #[knuffel(child)] + pub self_ref: SelfRefConfig, + #[knuffel(child)] + pub rss: RssConfig, + #[knuffel(child)] + pub atom: AtomConfig, +} + +#[derive(knuffel::Decode, Clone, Debug)] +pub struct SelfRefConfig { + #[knuffel(child, unwrap(argument))] + pub protocol: String, + #[knuffel(child, unwrap(argument))] + pub domain: String, +} + +#[derive(knuffel::Decode, Clone, Debug)] +pub struct RssConfig { + #[knuffel(child, unwrap(argument))] + pub num_posts: usize, + #[knuffel(child, unwrap(argument))] + pub title: String, + #[knuffel(child, unwrap(argument))] + pub ttl: u32, +} + +#[derive(knuffel::Decode, Clone, Debug)] +pub struct AtomConfig { + #[knuffel(child, unwrap(argument))] + pub num_posts: usize, + #[knuffel(child, unwrap(argument))] + pub title: String, +} + +#[derive(Clone, Copy, Default, Debug)] +#[repr(transparent)] +pub struct Uuid(pub libshire::uuid::Uuid); + +impl Uuid { + pub fn as_inner(&self) -> &libshire::uuid::Uuid { + &self.0 + } +} + +impl ops::Deref for Uuid { + type Target = libshire::uuid::Uuid; + + fn deref(&self) -> &Self::Target { + self.as_inner() + } +} + +impl DecodeScalar for Uuid { + fn type_check(type_name: &Option>, ctx: &mut Context) { + if let Some(type_name) = type_name { + ctx.emit_error(DecodeError::TypeName { + span: type_name.span().clone(), + found: Some((&**type_name).clone()), + expected: ExpectedType::no_type(), + rust_type: "Uuid", + }); + } + } + + fn raw_decode( + value: &Spanned, + ctx: &mut Context, + ) -> Result> { + match &**value { + Literal::String(s) => match s.parse() { + Ok(uuid) => Ok(Self(uuid)), + Err(err) => { + ctx.emit_error(DecodeError::conversion(value, err)); + Ok(Default::default()) + } + }, + _ => { + ctx.emit_error(DecodeError::scalar_kind(Kind::String, value)); + Ok(Default::default()) + } + } + } +} diff --git a/src/bin/blog_server/main.rs b/src/bin/blog_server/main.rs index c04f1b7..30566ad 100644 --- a/src/bin/blog_server/main.rs +++ b/src/bin/blog_server/main.rs @@ -4,7 +4,7 @@ mod render; mod service; mod template; -use std::{env, fs, path::PathBuf, sync::Arc, thread}; +use std::{env, fs, sync::Arc, thread}; use axum::Server; use miette::{IntoDiagnostic, Context}; @@ -13,63 +13,11 @@ use tracing::info; use blog::{ codeblock::CodeBlockRenderer, db::ConcurrentPostsStore, - uuid, }; +use config::Config; use render::Renderer; -#[derive(knuffel::Decode, Clone, Debug)] -pub struct Config { - #[knuffel(child, unwrap(argument))] - bind: String, - #[knuffel(child, unwrap(argument))] - concurrency_limit: usize, - #[knuffel(child, unwrap(argument))] - static_dir: PathBuf, - #[knuffel(child, unwrap(argument))] - favicon_dir: PathBuf, - #[knuffel(child, unwrap(argument))] - robots_path: PathBuf, - #[knuffel(child, unwrap(argument))] - posts_dir: PathBuf, - #[knuffel(child, unwrap(argument))] - post_media_dir: PathBuf, - #[knuffel(child, unwrap(argument))] - namespace_uuid: uuid::Uuid, - #[knuffel(child)] - self_ref: SelfRefConfig, - #[knuffel(child)] - rss: RssConfig, - #[knuffel(child)] - atom: AtomConfig, -} - -#[derive(knuffel::Decode, Clone, Debug)] -pub struct SelfRefConfig { - #[knuffel(child, unwrap(argument))] - protocol: String, - #[knuffel(child, unwrap(argument))] - domain: String, -} - -#[derive(knuffel::Decode, Clone, Debug)] -pub struct RssConfig { - #[knuffel(child, unwrap(argument))] - num_posts: usize, - #[knuffel(child, unwrap(argument))] - title: String, - #[knuffel(child, unwrap(argument))] - ttl: u32, -} - -#[derive(knuffel::Decode, Clone, Debug)] -pub struct AtomConfig { - #[knuffel(child, unwrap(argument))] - num_posts: usize, - #[knuffel(child, unwrap(argument))] - title: String, -} - fn main() -> miette::Result<()> { tracing_subscriber::fmt::init(); diff --git a/src/lib/lib.rs b/src/lib/lib.rs index b3bf79b..f042d03 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -1,5 +1,4 @@ pub mod codeblock; -pub mod post; pub mod db; +pub mod post; pub mod time; -pub mod uuid; diff --git a/src/lib/uuid.rs b/src/lib/uuid.rs deleted file mode 100644 index aa455ce..0000000 --- a/src/lib/uuid.rs +++ /dev/null @@ -1,60 +0,0 @@ -use std::ops; - -use knuffel::{ - ast::{Literal, TypeName}, - decode::{Context, Kind}, - errors::{DecodeError, ExpectedType}, - span::Spanned, - traits::ErrorSpan, - DecodeScalar, -}; - -#[derive(Clone, Copy, Default, Debug)] -#[repr(transparent)] -pub struct Uuid(pub libshire::uuid::Uuid); - -impl Uuid { - pub fn as_inner(&self) -> &libshire::uuid::Uuid { - &self.0 - } -} - -impl ops::Deref for Uuid { - type Target = libshire::uuid::Uuid; - - fn deref(&self) -> &Self::Target { - self.as_inner() - } -} - -impl DecodeScalar for Uuid { - fn type_check(type_name: &Option>, ctx: &mut Context) { - if let Some(type_name) = type_name { - ctx.emit_error(DecodeError::TypeName { - span: type_name.span().clone(), - found: Some((&**type_name).clone()), - expected: ExpectedType::no_type(), - rust_type: "Uuid", - }); - } - } - - fn raw_decode( - value: &Spanned, - ctx: &mut Context, - ) -> Result> { - match &**value { - Literal::String(s) => match s.parse() { - Ok(uuid) => Ok(Self(uuid)), - Err(err) => { - ctx.emit_error(DecodeError::conversion(value, err)); - Ok(Default::default()) - } - }, - _ => { - ctx.emit_error(DecodeError::scalar_kind(Kind::String, value)); - Ok(Default::default()) - } - } - } -}