From eca7687439c2b6f804603de75501b6737a82e5a2 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 15 Jun 2023 17:02:39 +0300 Subject: Database: use newtypes to represent settings This allows much for a cleaner and idiomatic settings interface. --- kittybox-rs/src/frontend/mod.rs | 36 +++++++++++++++++++--------------- kittybox-rs/src/frontend/onboarding.rs | 4 ++-- 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'kittybox-rs/src/frontend') diff --git a/kittybox-rs/src/frontend/mod.rs b/kittybox-rs/src/frontend/mod.rs index f0f4e5a..9d5c164 100644 --- a/kittybox-rs/src/frontend/mod.rs +++ b/kittybox-rs/src/frontend/mod.rs @@ -132,8 +132,9 @@ pub async fn homepage( // // btw is it more efficient to fetch these in parallel? let (blogname, channels) = tokio::join!( - db.get_setting(crate::database::Settings::SiteName, &path) - .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())), + db.get_setting::(&path) + .map(Result::unwrap_or_default), + db.get_channels(&path).map(|i| i.unwrap_or_default()) ); // Render the homepage @@ -144,8 +145,8 @@ pub async fn homepage( r#"text/html; charset="utf-8""#, )], Template { - title: &blogname, - blog_name: &blogname, + title: blogname.as_ref(), + blog_name: blogname.as_ref(), feeds: channels, user, content: MainPage { @@ -170,8 +171,9 @@ pub async fn homepage( error!("Error while fetching h-card and/or h-feed: {}", err); // Return the error let (blogname, channels) = tokio::join!( - db.get_setting(crate::database::Settings::SiteName, &path) - .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())), + db.get_setting::(&path) + .map(Result::unwrap_or_default), + db.get_channels(&path).map(|i| i.unwrap_or_default()) ); @@ -182,8 +184,8 @@ pub async fn homepage( r#"text/html; charset="utf-8""#, )], Template { - title: &blogname, - blog_name: &blogname, + title: blogname.as_ref(), + blog_name: blogname.as_ref(), feeds: channels, user, content: ErrorPage { @@ -215,8 +217,9 @@ pub async fn catchall( match get_post_from_database(&db, path.as_str(), query.after, &user).await { Ok(post) => { let (blogname, channels) = tokio::join!( - db.get_setting(crate::database::Settings::SiteName, &host) - .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())), + db.get_setting::(&host) + .map(Result::unwrap_or_default), + db.get_channels(&host).map(|i| i.unwrap_or_default()) ); // Render the homepage @@ -227,8 +230,8 @@ pub async fn catchall( r#"text/html; charset="utf-8""#, )], Template { - title: &blogname, - blog_name: &blogname, + title: blogname.as_ref(), + blog_name: blogname.as_ref(), feeds: channels, user, content: match post.pointer("/type/0").and_then(|i| i.as_str()) { @@ -245,8 +248,9 @@ pub async fn catchall( } Err(err) => { let (blogname, channels) = tokio::join!( - db.get_setting(crate::database::Settings::SiteName, &host) - .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())), + db.get_setting::(&host) + .map(Result::unwrap_or_default), + db.get_channels(&host).map(|i| i.unwrap_or_default()) ); ( @@ -256,8 +260,8 @@ pub async fn catchall( r#"text/html; charset="utf-8""#, )], Template { - title: &blogname, - blog_name: &blogname, + title: blogname.as_ref(), + blog_name: blogname.as_ref(), feeds: channels, user, content: ErrorPage { diff --git a/kittybox-rs/src/frontend/onboarding.rs b/kittybox-rs/src/frontend/onboarding.rs index b4bae8e..88b533b 100644 --- a/kittybox-rs/src/frontend/onboarding.rs +++ b/kittybox-rs/src/frontend/onboarding.rs @@ -1,4 +1,4 @@ -use crate::database::{Settings, Storage}; +use crate::database::{settings, Storage}; use axum::{ extract::{Extension, Host}, http::StatusCode, @@ -68,7 +68,7 @@ async fn onboard( )); } - db.set_setting(Settings::SiteName, user.me.as_str(), &data.blog_name) + db.set_setting::(user.me.as_str(), data.blog_name.to_owned()) .await .map_err(FrontendError::from)?; -- cgit 1.4.1