return structured errors from commands (wip)
This commit is contained in:
@ -9,40 +9,24 @@ use crate::errors::*;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct AppConfig {
|
||||
pub db_path: PathBuf,
|
||||
#[serde(default = "default_listen_addr")]
|
||||
pub listen_addr: Ipv4Addr,
|
||||
#[serde(default = "default_listen_port")]
|
||||
pub listen_port: u16,
|
||||
#[serde(default = "default_rehide_ms")]
|
||||
pub rehide_ms: u64,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct DbAppConfig {
|
||||
listen_addr: Option<Ipv4Addr>,
|
||||
listen_port: Option<u16>,
|
||||
rehide_ms: Option<u64>,
|
||||
#[serde(default = "default_start_minimized")]
|
||||
pub start_minimized: bool,
|
||||
}
|
||||
|
||||
|
||||
impl Default for AppConfig {
|
||||
fn default() -> Self {
|
||||
AppConfig {
|
||||
db_path: get_or_create_db_path(),
|
||||
listen_addr: Ipv4Addr::LOCALHOST,
|
||||
listen_port: listen_port(),
|
||||
rehide_ms: 1000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<DbAppConfig> for AppConfig {
|
||||
fn from(db_config: DbAppConfig) -> Self {
|
||||
AppConfig {
|
||||
db_path: get_or_create_db_path(),
|
||||
listen_addr: db_config.listen_addr.unwrap_or(Ipv4Addr::LOCALHOST),
|
||||
listen_port: db_config.listen_port.unwrap_or_else(|| listen_port()),
|
||||
rehide_ms: db_config.rehide_ms.unwrap_or(1000),
|
||||
listen_addr: default_listen_addr(),
|
||||
listen_port: default_listen_port(),
|
||||
rehide_ms: default_rehide_ms(),
|
||||
start_minimized: default_start_minimized(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -58,18 +42,7 @@ pub async fn load(pool: &SqlitePool) -> Result<AppConfig, SetupError> {
|
||||
None => return Ok(AppConfig::default()),
|
||||
};
|
||||
|
||||
let db_config: DbAppConfig = serde_json::from_str(&row.data)?;
|
||||
Ok(AppConfig::from(db_config))
|
||||
}
|
||||
|
||||
|
||||
fn listen_port() -> u16 {
|
||||
if cfg!(debug_assertions) {
|
||||
12_345
|
||||
}
|
||||
else {
|
||||
19_923
|
||||
}
|
||||
Ok(serde_json::from_str(&row.data)?)
|
||||
}
|
||||
|
||||
|
||||
@ -89,3 +62,19 @@ pub fn get_or_create_db_path() -> PathBuf {
|
||||
parent.push("creddy.db");
|
||||
parent
|
||||
}
|
||||
|
||||
|
||||
fn default_listen_port() -> u16 {
|
||||
if cfg!(debug_assertions) {
|
||||
12_345
|
||||
}
|
||||
else {
|
||||
19_923
|
||||
}
|
||||
}
|
||||
|
||||
fn default_listen_addr() -> Ipv4Addr { Ipv4Addr::LOCALHOST }
|
||||
|
||||
fn default_rehide_ms() -> u64 { 1000 }
|
||||
|
||||
fn default_start_minimized() -> bool { true }
|
||||
|
Reference in New Issue
Block a user