diff --git a/Cargo.lock b/Cargo.lock index cfb8dba..7508960 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,13 +4,13 @@ version = 3 [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bato" -version = "0.1.5" +version = "0.1.6" dependencies = [ "cmake", "serde", @@ -19,79 +19,79 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.70" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26a6ce4b6a484fa3edb70f7efa6fc430fd2b87285fe8b84304fd0936faa0dc0" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cmake" -version = "0.1.45" +version = "0.1.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb6210b637171dfba4cda12e579ac6dc73f5165ad56133e5d72ef3131f320855" +checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" dependencies = [ "cc", ] -[[package]] -name = "dtoa" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" - [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "indexmap" -version = "1.7.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ "autocfg", "hashbrown", ] [[package]] -name = "linked-hash-map" -version = "0.5.4" +name = "itoa" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" [[package]] name = "proc-macro2" -version = "1.0.29" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.9" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" dependencies = [ "proc-macro2", ] +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + [[package]] name = "serde" -version = "1.0.130" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.130" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391" dependencies = [ "proc-macro2", "quote", @@ -100,38 +100,36 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.8.21" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8c608a35705a5d3cdc9fbe403147647ff34b921f8e833e49306df898f9b20af" +checksum = "79b7c9017c64a49806c6e8df8ef99b92446d09c92457f85f91835b01a8064ae0" dependencies = [ - "dtoa", "indexmap", + "itoa", + "ryu", "serde", - "yaml-rust", + "unsafe-libyaml", ] [[package]] name = "syn" -version = "1.0.76" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "unicode-ident" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" [[package]] -name = "yaml-rust" -version = "0.4.5" +name = "unsafe-libyaml" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] +checksum = "931179334a56395bcf64ba5e0ff56781381c1a5832178280c7d7f91d1679aeb0" diff --git a/Cargo.toml b/Cargo.toml index e0a5490..8df91a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,14 @@ [package] name = "bato" -version = "0.1.5" +version = "0.1.6" authors = ["pierre "] -edition = "2018" +edition = "2021" links = "notilus" build = "build.rs" [dependencies] serde = { version = "1.0", features = ["derive"] } -serde_yaml = "0.8" +serde_yaml = "0.9" [build-dependencies] cmake = "0.1" diff --git a/src/lib.rs b/src/lib.rs index c341080..197ce40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,27 @@ pub enum Urgency { } #[derive(Debug, Serialize, Deserialize)] +pub struct NotificationConfig { + summary: String, + body: Option, + icon: Option, + urgency: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct UserConfig { + pub tick_rate: Option, + bat_name: Option, + low_level: u32, + critical_level: u32, + full_design: Option, + critical: Option, + low: Option, + full: Option, + charging: Option, + discharging: Option, +} + pub struct Notification { summary: CString, body: Option, @@ -46,7 +67,28 @@ pub struct Notification { urgency: Option, } -#[derive(Debug, Serialize, Deserialize)] +impl TryFrom for Notification { + type Error = Error; + + fn try_from(config: NotificationConfig) -> Result { + let summary = CString::new(config.summary).map_err(|_| "NulError")?; + let body = config + .body + .map(|s| CString::new(s).map_err(|_| "NulError")) + .transpose()?; + let icon = config + .icon + .map(|s| CString::new(s).map_err(|_| "NulError")) + .transpose()?; + Ok(Notification { + summary, + body, + icon, + urgency: config.urgency, + }) + } +} + pub struct Config { pub tick_rate: Option, bat_name: Option, @@ -67,9 +109,31 @@ impl Config { env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| format!("{}/.config", home)); let content = fs::read_to_string(format!("{}/bato/bato.yaml", config_path)) .map_err(|err| format!("while reading the config file, {}", err))?; - let config: Config = serde_yaml::from_str(&content) + let user_config: UserConfig = serde_yaml::from_str(&content) .map_err(|err| format!("while deserializing the config file, {}", err))?; - Ok(config) + let critical = user_config + .critical + .map(Notification::try_from) + .transpose()?; + + Ok(Config { + tick_rate: user_config.tick_rate, + bat_name: user_config.bat_name, + low_level: user_config.low_level, + critical_level: user_config.critical_level, + full_design: user_config.full_design, + critical, + low: user_config.low.map(Notification::try_from).transpose()?, + full: user_config.full.map(Notification::try_from).transpose()?, + charging: user_config + .charging + .map(Notification::try_from) + .transpose()?, + discharging: user_config + .discharging + .map(Notification::try_from) + .transpose()?, + }) } pub fn normalize(&mut self) -> &Self {