diff --git a/Cargo.lock b/Cargo.lock index 4dacc4f822..2db752c5e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,6 +258,15 @@ version = "0.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "414dcefbc63d77c526a76b3afcf6fbb9b5e2791c19c3aa2297733208750c6e53" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -1058,6 +1067,7 @@ dependencies = [ "async-once-cell", "async-trait", "backon", + "bincode", "calibright", "chrono", "chrono-tz", @@ -1067,6 +1077,7 @@ dependencies = [ "futures", "glob", "hyper", + "i3status-rs_data", "inotify 0.10.2", "libc", "libpulse-binding", @@ -1097,6 +1108,13 @@ dependencies = [ "zbus", ] +[[package]] +name = "i3status-rs_data" +version = "0.1.0" +dependencies = [ + "serde", +] + [[package]] name = "iana-time-zone" version = "0.1.57" diff --git a/Cargo.toml b/Cargo.toml index 9c70ef94e4..bdbb6b7a17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ authors = [ edition = "2021" [workspace] -members = [".", "xtask"] +members = [".", "xtask", "data"] [features] default = ["pulseaudio"] @@ -28,6 +28,7 @@ rustdoc-args = ["--cfg", "docsrs"] async-once-cell = "0.5" async-trait = "0.1" backon = "0.4.1" +bincode = "1.3.3" calibright = { version = "0.1", features = ["watch"] } chrono = { version = "0.4", default-features = false, features = ["clock", "unstable-locales"] } chrono-tz = { version = "0.8", features = ["serde"] } @@ -64,6 +65,7 @@ unicode-segmentation = "1.10.1" wayrs-client = { version = "0.12", features = ["tokio"] } wayrs-protocols = { version = "0.12", features = ["wlr-foreign-toplevel-management-unstable-v1"] } zbus = { version = "3.14", default-features = false, features = ["tokio"] } +i3status-rs_data = {path = "data"} [dependencies.tokio] version = "1.12" @@ -82,6 +84,11 @@ features = [ "time", ] +[build-dependencies] +bincode = "1.3.3" +serde_json = "1.0" +i3status-rs_data = {path = "data"} + [profile.release] lto = "thin" diff --git a/build.rs b/build.rs index b88c673f7a..ff236a3e32 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,25 @@ -use std::process::Command; +use std::{env, fs::File, path::PathBuf, process::Command}; fn main() { + let met_no_legends_store: i3status_rs_data::weather::met_no::LegendsStore = + serde_json::from_str(include_str!("data/met_no_legends.json")).unwrap(); + + let buffer = + File::create(PathBuf::from(env::var("OUT_DIR").unwrap()).join("met_no_legends.bin")) + .unwrap(); + + bincode::serialize_into(buffer, &met_no_legends_store).unwrap(); + + let open_weather_map_cities_store: i3status_rs_data::weather::open_weather_map::CitiesStore = + serde_json::from_str(include_str!("data/open_weather_map_cities.json")).unwrap(); + + let buffer = File::create( + PathBuf::from(env::var("OUT_DIR").unwrap()).join("open_weather_map_cities.bin"), + ) + .unwrap(); + + bincode::serialize_into(buffer, &open_weather_map_cities_store).unwrap(); + let hash = Command::new("git") .args(["rev-parse", "--short", "HEAD"]) .env("GIT_CONFIG_GLOBAL", "/dev/null") diff --git a/cspell.yaml b/cspell.yaml index bcc204edf7..de747b8d5d 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -4,7 +4,7 @@ allowCompoundWords: true ignorePaths: - target/ - man/ - - src/blocks/weather/met_no_legends.json + - data/met_no_legends.json ignoreRegExpList: # Ignore usernames that are @'d - /@[\w-]*/g diff --git a/data/Cargo.toml b/data/Cargo.toml new file mode 100644 index 0000000000..f776583e30 --- /dev/null +++ b/data/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "i3status-rs_data" +publish = false +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0", features = ["derive", "rc"] } diff --git a/src/blocks/weather/met_no_legends.json b/data/met_no_legends.json similarity index 100% rename from src/blocks/weather/met_no_legends.json rename to data/met_no_legends.json diff --git a/src/blocks/weather/open_weather_map_cities.json b/data/open_weather_map_cities.json similarity index 100% rename from src/blocks/weather/open_weather_map_cities.json rename to data/open_weather_map_cities.json diff --git a/data/src/lib.rs b/data/src/lib.rs new file mode 100644 index 0000000000..81f83c3708 --- /dev/null +++ b/data/src/lib.rs @@ -0,0 +1,4 @@ +extern crate serde; + +mod prelude; +pub mod weather; diff --git a/data/src/prelude.rs b/data/src/prelude.rs new file mode 100644 index 0000000000..444712a842 --- /dev/null +++ b/data/src/prelude.rs @@ -0,0 +1,3 @@ +pub use std::collections::HashMap; + +pub use serde::{Deserialize, Serialize}; diff --git a/data/src/weather/met_no.rs b/data/src/weather/met_no.rs new file mode 100644 index 0000000000..495f1eacd0 --- /dev/null +++ b/data/src/weather/met_no.rs @@ -0,0 +1,10 @@ +use crate::prelude::*; + +pub type LegendsStore = HashMap; + +#[derive(Deserialize, Serialize)] +pub struct LegendsResult { + pub desc_en: String, + pub desc_nb: String, + pub desc_nn: String, +} diff --git a/data/src/weather/mod.rs b/data/src/weather/mod.rs new file mode 100644 index 0000000000..605c1c1a9f --- /dev/null +++ b/data/src/weather/mod.rs @@ -0,0 +1,2 @@ +pub mod met_no; +pub mod open_weather_map; diff --git a/data/src/weather/open_weather_map.rs b/data/src/weather/open_weather_map.rs new file mode 100644 index 0000000000..ae55d220a3 --- /dev/null +++ b/data/src/weather/open_weather_map.rs @@ -0,0 +1,9 @@ +use crate::prelude::*; + +pub type CitiesStore = HashMap; + +#[derive(Deserialize, Serialize)] +pub struct City { + pub lat: f64, + pub lon: f64, +} diff --git a/src/blocks/weather/update_data.sh b/data/update.sh similarity index 100% rename from src/blocks/weather/update_data.sh rename to data/update.sh diff --git a/src/blocks/weather/met_no.rs b/src/blocks/weather/met_no.rs index 4543dc2e92..cc40039a52 100644 --- a/src/blocks/weather/met_no.rs +++ b/src/blocks/weather/met_no.rs @@ -1,6 +1,6 @@ use super::*; -type LegendsStore = HashMap; +use i3status_rs_data::weather::met_no::LegendsStore; #[derive(Deserialize, Debug)] #[serde(tag = "name", rename_all = "lowercase")] @@ -25,13 +25,6 @@ impl<'a> Service<'a> { } } -#[derive(Deserialize)] -struct LegendsResult { - desc_en: String, - desc_nb: String, - desc_nn: String, -} - #[derive(Deserialize, Debug, Clone, Default)] pub(super) enum ApiLanguage { #[serde(rename = "en")] @@ -90,8 +83,14 @@ struct ForecastTimeInstant { relative_humidity: Option, } -static LEGENDS: Lazy> = - Lazy::new(|| serde_json::from_str(include_str!("met_no_legends.json")).ok()); +static LEGENDS: Lazy> = Lazy::new(|| { + bincode::deserialize(include_bytes!(concat!( + env!("OUT_DIR"), + "/", + "met_no_legends.bin" + ))) + .ok() +}); const FORECAST_URL: &str = "https://api.met.no/weatherapi/locationforecast/2.0/compact"; diff --git a/src/blocks/weather/open_weather_map.rs b/src/blocks/weather/open_weather_map.rs index e6bbb91817..6ad39d9ef8 100644 --- a/src/blocks/weather/open_weather_map.rs +++ b/src/blocks/weather/open_weather_map.rs @@ -1,6 +1,8 @@ use super::*; use chrono::Utc; +use i3status_rs_data::weather::open_weather_map::{CitiesStore, City}; + pub(super) const URL: &str = "https://api.openweathermap.org/data/2.5/weather"; pub(super) const GEO_URL: &str = "https://api.openweathermap.org/geo/1.0"; pub(super) const API_KEY_ENV: &str = "OPENWEATHERMAP_API_KEY"; @@ -90,16 +92,14 @@ struct ApiWeather { description: String, } -type CitiesStore = HashMap; - -#[derive(Deserialize, Debug)] -struct City { - lat: f64, - lon: f64, -} - -static CITIES: Lazy> = - Lazy::new(|| serde_json::from_str(include_str!("open_weather_map_cities.json")).ok()); +static CITIES: Lazy> = Lazy::new(|| { + bincode::deserialize(include_bytes!(concat!( + env!("OUT_DIR"), + "/", + "open_weather_map_cities.bin" + ))) + .ok() +}); static LOCATION_CACHE: Mutex> = Mutex::new(None);