52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
use kordophone_db::settings::Settings as DbSettings;
|
|
use anyhow::Result;
|
|
|
|
mod keys {
|
|
pub static SERVER_URL: &str = "ServerURL";
|
|
pub static USERNAME: &str = "Username";
|
|
pub static CREDENTIAL_ITEM: &str = "CredentialItem";
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Settings {
|
|
pub server_url: Option<String>,
|
|
pub username: Option<String>,
|
|
pub credential_item: Option<String>,
|
|
}
|
|
|
|
impl Settings {
|
|
pub fn from_db(db_settings: &mut DbSettings) -> Result<Self> {
|
|
let server_url: Option<String> = db_settings.get(keys::SERVER_URL)?;
|
|
let username: Option<String> = db_settings.get(keys::USERNAME)?;
|
|
let credential_item: Option<String> = db_settings.get(keys::CREDENTIAL_ITEM)?;
|
|
|
|
Ok(Self {
|
|
server_url,
|
|
username,
|
|
credential_item,
|
|
})
|
|
}
|
|
|
|
pub fn save(&self, db_settings: &mut DbSettings) -> Result<()> {
|
|
if let Some(server_url) = &self.server_url {
|
|
db_settings.put(keys::SERVER_URL, &server_url)?;
|
|
}
|
|
if let Some(username) = &self.username {
|
|
db_settings.put(keys::USERNAME, &username)?;
|
|
}
|
|
if let Some(credential_item) = &self.credential_item {
|
|
db_settings.put(keys::CREDENTIAL_ITEM, &credential_item)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Default for Settings {
|
|
fn default() -> Self {
|
|
Self {
|
|
server_url: None,
|
|
username: None,
|
|
credential_item: None,
|
|
}
|
|
}
|
|
} |