[Refactor] Move configuration reading to the config module

This commit is contained in:
Gergely Polonkai 2022-05-23 18:09:17 +02:00
parent 852e8e090e
commit 3d4224b560
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 21 additions and 16 deletions

View File

@ -1,3 +1,5 @@
use std::fs;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize, Copy, Clone)] #[derive(Deserialize, Copy, Clone)]
@ -8,6 +10,21 @@ pub struct Config {
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct CompleteConfig { struct CompleteConfig {
pub seasonal_clock: Config, seasonal_clock: Config,
}
pub fn get_config() -> Option<Config> {
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
let config_path = xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory");
let data: std::io::Result<String> = fs::read_to_string(config_path);
if let Ok(..) = data {
let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap();
Some(complete_config.seasonal_clock)
} else {
None
}
} }

View File

@ -1,7 +1,5 @@
extern crate smithay_client_toolkit as sctk; extern crate smithay_client_toolkit as sctk;
use std::fs;
use calloop::{timer::Timer, EventLoop}; use calloop::{timer::Timer, EventLoop};
use sctk::reexports::client::protocol::{wl_shm, wl_surface}; use sctk::reexports::client::protocol::{wl_shm, wl_surface};
use sctk::shm::AutoMemPool; use sctk::shm::AutoMemPool;
@ -11,23 +9,13 @@ use svg::node::element::path::Data as PathData;
mod config; mod config;
mod svg_clock; mod svg_clock;
use config::{CompleteConfig, Config}; use config::{get_config, Config};
use svg_clock::{cache_hour_name_paths, gen_svg, svg_to_usvg}; use svg_clock::{cache_hour_name_paths, gen_svg, svg_to_usvg};
sctk::default_environment!(SeasonalClock, desktop); sctk::default_environment!(SeasonalClock, desktop);
fn main() { fn main() {
let xdg_dirs = xdg::BaseDirectories::new().unwrap(); let config = get_config();
let config_path = xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory");
let data: std::io::Result<String> = fs::read_to_string(config_path);
let config: Option<Config> = if let Ok(..) = data {
let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap();
Some(complete_config.seasonal_clock)
} else {
None
};
let (env, display, queue) = sctk::new_default_environment!(SeasonalClock, desktop) let (env, display, queue) = sctk::new_default_environment!(SeasonalClock, desktop)
.expect("Unable to connect to a Wayland compositor"); .expect("Unable to connect to a Wayland compositor");