From 54914782c7632e041919746e80d3f802f6601a63 Mon Sep 17 00:00:00 2001 From: Vika Date: Wed, 23 Mar 2022 05:01:14 +0300 Subject: Make the settings in the database a strong type --- src/database/mod.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/database/mod.rs') diff --git a/src/database/mod.rs b/src/database/mod.rs index e7baaa8..57223f8 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -39,6 +39,20 @@ pub enum ErrorKind { Other, } +/// Enum representing settings that might be stored in the site's database. +#[derive(Serialize, Debug, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum Settings { + /// The name of the website -- displayed in the header and the browser titlebar. + SiteName, +} + +impl std::string::ToString for Settings { + fn to_string(&self) -> String { + serde_variant::to_variant_name(self).unwrap().to_string() + } +} + /// Error signalled from the database. #[derive(Debug)] pub struct StorageError { @@ -239,10 +253,10 @@ pub trait Storage: std::fmt::Debug + Clone + Send + Sync { async fn delete_post(&self, url: &'_ str) -> Result<()>; /// Gets a setting from the setting store and passes the result. - async fn get_setting(&self, setting: &'_ str, user: &'_ str) -> Result; + async fn get_setting(&self, setting: Settings, user: &'_ str) -> Result; /// Commits a setting to the setting store. - async fn set_setting(&self, setting: &'_ str, user: &'_ str, value: &'_ str) -> Result<()>; + async fn set_setting(&self, setting: Settings, user: &'_ str, value: &'_ str) -> Result<()>; } #[cfg(test)] @@ -399,12 +413,12 @@ mod tests { async fn test_backend_settings(backend: Backend) { backend - .set_setting("site_name", "https://fireburn.ru/", "Vika's Hideout") + .set_setting(crate::database::Settings::SiteName, "https://fireburn.ru/", "Vika's Hideout") .await .unwrap(); assert_eq!( backend - .get_setting("site_name", "https://fireburn.ru/") + .get_setting(crate::database::Settings::SiteName, "https://fireburn.ru/") .await .unwrap(), "Vika's Hideout" -- cgit 1.4.1