switched err-derive for snafu
This commit is contained in:
parent
dce14132ce
commit
96a5455483
3 changed files with 12 additions and 53 deletions
39
Cargo.lock
generated
39
Cargo.lock
generated
|
@ -371,20 +371,6 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
||||
|
||||
[[package]]
|
||||
name = "err-derive"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c34a887c8df3ed90498c1c437ce21f211c8e27672921a8ffa293cb8d6d4caa9e"
|
||||
dependencies = [
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn 1.0.109",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "event-listener"
|
||||
version = "5.2.0"
|
||||
|
@ -1185,7 +1171,6 @@ version = "1.1.0"
|
|||
dependencies = [
|
||||
"async-channel",
|
||||
"config",
|
||||
"err-derive",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"gilrs",
|
||||
|
@ -1690,12 +1675,6 @@ dependencies = [
|
|||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80af6f9131f277a45a3fba6ce8e2258037bb0477a67e610d3c1fe046ab31de47"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.17"
|
||||
|
@ -1858,18 +1837,6 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "synstructure"
|
||||
version = "0.12.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "system-deps"
|
||||
version = "6.2.1"
|
||||
|
@ -2196,12 +2163,6 @@ version = "1.11.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.0"
|
||||
|
|
|
@ -11,7 +11,6 @@ tracker-state-debug = []
|
|||
[dependencies]
|
||||
async-channel = "2.2.0"
|
||||
config = "0.14.0"
|
||||
err-derive = "0.3.1"
|
||||
futures-core = "0.3.30"
|
||||
futures-util = { version = "0.3.30", features = ["tokio-io"] }
|
||||
gilrs = "0.10.6"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use config::{Config, FileFormat};
|
||||
use err_derive::Error;
|
||||
use gtk::cairo::IoError;
|
||||
use snafu::prelude::*;
|
||||
use log::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
|
@ -29,27 +30,25 @@ impl Default for AppConfig {
|
|||
}
|
||||
|
||||
pub fn load_config() -> AppConfig {
|
||||
let settings = Config::builder()
|
||||
Config::builder()
|
||||
.add_source(config::File::new("./settings.toml", FileFormat::Toml))
|
||||
.build();
|
||||
|
||||
settings
|
||||
.build()
|
||||
.and_then(|val| val.try_deserialize())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum SaveConfigError {
|
||||
#[error(display = "Could not serialize app state: {:?}", _0)]
|
||||
SerdeError(#[cause] toml::ser::Error),
|
||||
#[error(display = "Could not write app state to file: {:?}", _0)]
|
||||
IoError(#[cause] std::io::Error),
|
||||
#[snafu(display("Could not serialize app state: {source}"))]
|
||||
SerdeError {source: toml::ser::Error },
|
||||
#[snafu(display("Could not write app state to file: {path}"))]
|
||||
IoError {source: std::io::Error, path: String },
|
||||
}
|
||||
|
||||
pub fn save_config(config: &AppConfig) -> Result<(), SaveConfigError> {
|
||||
let toml_str = toml::to_string(&config)?;
|
||||
let mut file = File::create("./settings.toml")?;
|
||||
file.write_all(toml_str.as_bytes())?;
|
||||
let toml_str = toml::to_string(&config).context(SerdeSnafu)?;
|
||||
let mut file = File::create("./settings.toml").context(IoSnafu {path: "./settings.toml" })?;
|
||||
file.write_all(toml_str.as_bytes()).context(IoSnafu {path: "./settings.toml" })?;
|
||||
info!("Config file saved successfully");
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue