settings: split in multiple modules
This commit is contained in:
parent
33cc5b03e9
commit
a5eafc49f5
3 changed files with 66 additions and 58 deletions
51
crates/settings/src/database.rs
Normal file
51
crates/settings/src/database.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
use crate::error::Error;
|
||||||
|
use crate::Settings;
|
||||||
|
use database::sqlx::SqliteExecutor;
|
||||||
|
use database::Settings as DatabaseSettings;
|
||||||
|
|
||||||
|
const DEFAULT_BUSINESS_NAME: &str = "ezidam";
|
||||||
|
pub const DEFAULT_BUSINESS_LOGO: &[u8] = include_bytes!("../../../logo/ezidam.png");
|
||||||
|
|
||||||
|
impl From<DatabaseSettings> for Settings {
|
||||||
|
fn from(db: DatabaseSettings) -> Self {
|
||||||
|
Self {
|
||||||
|
created_at: db.created_at,
|
||||||
|
updated_at: db.updated_at,
|
||||||
|
business_name: db
|
||||||
|
.business_name
|
||||||
|
.unwrap_or_else(|| DEFAULT_BUSINESS_NAME.into()),
|
||||||
|
business_logo: db
|
||||||
|
.business_logo
|
||||||
|
.unwrap_or_else(|| DEFAULT_BUSINESS_LOGO.to_vec()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Settings {
|
||||||
|
pub async fn init(conn: impl SqliteExecutor<'_>) -> Result<Option<()>, Error> {
|
||||||
|
Ok(DatabaseSettings::init(conn).await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get(conn: impl SqliteExecutor<'_>) -> Result<Self, Error> {
|
||||||
|
Ok(DatabaseSettings::get(conn).await.map(Self::from)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_business_name(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
business_name: &str,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
DatabaseSettings::set_business_name(conn, Some(business_name)).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// **Warning**: No checks are performed, make sure the bytes are safe!
|
||||||
|
pub async fn set_business_logo(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
business_logo: &[u8],
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
DatabaseSettings::set_business_logo(conn, Some(business_logo)).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
8
crates/settings/src/error.rs
Normal file
8
crates/settings/src/error.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
// error
|
||||||
|
#[derive(thiserror::Error)]
|
||||||
|
// the rest
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Database: {0}")]
|
||||||
|
Database(#[from] database::Error),
|
||||||
|
}
|
||||||
|
|
@ -1,15 +1,11 @@
|
||||||
use chrono::{DateTime, Utc};
|
mod database;
|
||||||
use database::sqlx::SqliteExecutor;
|
mod error;
|
||||||
use database::Settings as DatabaseSettings;
|
|
||||||
|
|
||||||
// error
|
use chrono::{DateTime, Utc};
|
||||||
#[derive(thiserror::Error)]
|
|
||||||
// the rest
|
// Exports
|
||||||
#[derive(Debug)]
|
pub use crate::database::DEFAULT_BUSINESS_LOGO;
|
||||||
pub enum Error {
|
pub use crate::error::Error;
|
||||||
#[error("Database: {0}")]
|
|
||||||
Database(#[from] database::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
// the rest
|
// the rest
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -20,55 +16,8 @@ pub struct Settings {
|
||||||
business_logo: Vec<u8>,
|
business_logo: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_BUSINESS_NAME: &str = "ezidam";
|
|
||||||
pub const DEFAULT_BUSINESS_LOGO: &[u8] = include_bytes!("../../../logo/ezidam.png");
|
|
||||||
|
|
||||||
impl From<DatabaseSettings> for Settings {
|
|
||||||
fn from(db: DatabaseSettings) -> Self {
|
|
||||||
Self {
|
|
||||||
created_at: db.created_at,
|
|
||||||
updated_at: db.updated_at,
|
|
||||||
business_name: db
|
|
||||||
.business_name
|
|
||||||
.unwrap_or_else(|| DEFAULT_BUSINESS_NAME.into()),
|
|
||||||
business_logo: db
|
|
||||||
.business_logo
|
|
||||||
.unwrap_or_else(|| DEFAULT_BUSINESS_LOGO.to_vec()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Settings {
|
impl Settings {
|
||||||
pub fn business_logo(&self) -> &[u8] {
|
pub fn business_logo(&self) -> &[u8] {
|
||||||
self.business_logo.as_slice()
|
self.business_logo.as_slice()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
|
||||||
pub async fn init(conn: impl SqliteExecutor<'_>) -> Result<Option<()>, Error> {
|
|
||||||
Ok(DatabaseSettings::init(conn).await?)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get(conn: impl SqliteExecutor<'_>) -> Result<Self, Error> {
|
|
||||||
Ok(DatabaseSettings::get(conn).await.map(Self::from)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn set_business_name(
|
|
||||||
conn: impl SqliteExecutor<'_>,
|
|
||||||
business_name: &str,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
DatabaseSettings::set_business_name(conn, Some(business_name)).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// **Warning**: No checks are performed, make sure the bytes are safe!
|
|
||||||
pub async fn set_business_logo(
|
|
||||||
conn: impl SqliteExecutor<'_>,
|
|
||||||
business_logo: &[u8],
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
DatabaseSettings::set_business_logo(conn, Some(business_logo)).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue