~grtcdr/citron

76ea8eba4e4c272da151c943431166d8d372ea57 — grtcdr 2 years ago 667691b
Simply project structure, removing config package.
3 files changed, 46 insertions(+), 52 deletions(-)

R src/{config/components.rs => config.rs}
D src/config/base.rs
D src/config/mod.rs
R src/config/components.rs => src/config.rs +46 -10
@@ 1,24 1,20 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Network {
    pub title: Option<String>,
    pub interface: Option<String>,
    pub format: Option<String>,
pub struct Icon {
    pub theme: Option<String>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Date {
pub struct Network {
    pub title: Option<String>,
    pub interface: Option<String>,
    pub format: Option<String>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Icon {
    pub theme: Option<String>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Battery {
    pub title: Option<String>,
    pub format: Option<String>,


@@ 39,6 35,12 @@ pub struct Memory {
    pub title: Option<String>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Date {
    pub title: Option<String>,
    pub format: Option<String>,
}

impl Date {
    pub fn is_railway(&self) -> bool {
        if let Some(format) = &self.format {


@@ 48,3 50,37 @@ impl Date {
        }
    }
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Config {
    pub date: Option<Date>,
    pub network: Option<Network>,
    pub backlight: Option<Backlight>,
    pub battery: Option<Battery>,
    pub memory: Option<Memory>,
    pub uptime: Option<Uptime>,
    pub icon: Option<Icon>,
    pub timeout: Option<u32>,
}

impl Config {
    pub fn read_config() -> Option<Config> {
        let path = if let Ok(dot_config) = std::env::var("XDG_CONFIG_HOME") {
            PathBuf::from(dot_config).join("citron").join("config.yml")
        } else if let Ok(home) = std::env::var("HOME") {
            PathBuf::from(home)
                .join(".config")
                .join("citron")
                .join("config.yml")
        } else {
            return None;
        };

        if let Ok(file) = fs::File::open(path) {
            let config: Config = serde_yaml::from_reader(file).expect("Could not read values.");
            return Some(config);
        }

        None
    }
}

D src/config/base.rs => src/config/base.rs +0 -38
@@ 1,38 0,0 @@
use super::components::*;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Config {
    pub date: Option<Date>,
    pub network: Option<Network>,
    pub backlight: Option<Backlight>,
    pub battery: Option<Battery>,
    pub memory: Option<Memory>,
    pub uptime: Option<Uptime>,
    pub icon: Option<Icon>,
    pub timeout: Option<u32>,
}

impl Config {
    pub fn read_config() -> Option<Config> {
        let path = if let Ok(dot_config) = std::env::var("XDG_CONFIG_HOME") {
            PathBuf::from(dot_config).join("citron").join("config.yml")
        } else if let Ok(home) = std::env::var("HOME") {
            PathBuf::from(home)
                .join(".config")
                .join("citron")
                .join("config.yml")
        } else {
            return None;
        };

        if let Ok(file) = fs::File::open(path) {
            let config: Config = serde_yaml::from_reader(file).expect("Could not read values.");
            return Some(config);
        }

        None
    }
}

D src/config/mod.rs => src/config/mod.rs +0 -4
@@ 1,4 0,0 @@
pub mod base;
pub mod components;

pub use base::Config;