Skip to content

Commit

Permalink
Add config file format and config file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdi265 committed Feb 18, 2024
1 parent a0709bc commit 438fd15
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
45 changes: 41 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ path = "src/input-backend/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Config dependencies
toml = "0.8"
serde = "1"
serde_derive = "1"
# GUI Dependencies
gtk = "0.17.1"
gtk-layer-shell = "0.6.1"
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![allow(dead_code)]

#[path = "config/backend.rs"]
pub mod backend;
#[path = "config/user.rs"]
pub mod user;

pub const DBUS_PATH: &str = "/org/erikreider/swayosd";
pub const DBUS_BACKEND_NAME: &str = "org.erikreider.swayosd";
pub const DBUS_SERVER_NAME: &str = "org.erikreider.swayosd-server";
Expand Down
30 changes: 30 additions & 0 deletions src/config/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::error::Error;
use std::path::Path;
use serde_derive::Deserialize;

pub const BACKEND_CONFIG_PATH: &str = "/etc/xdg/swayosd/backend.toml";

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct InputBackendConfig {

}

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct BackendConfig {
#[serde(default)]
pub input: InputBackendConfig
}

pub fn read_backend_config() -> Result<BackendConfig, Box<dyn Error>> {
let path = Path::new(BACKEND_CONFIG_PATH);
if !path.exists() {
return Ok(Default::default())
}

let config_file = std::fs::read_to_string(path)?;
let config: BackendConfig = toml::from_str(&config_file)?;
Ok(config)
}

51 changes: 51 additions & 0 deletions src/config/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::error::Error;
use std::path::PathBuf;
use serde_derive::Deserialize;
use gtk::glib::user_config_dir;
use gtk::glib::system_config_dirs;

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct ClientConfig {

}

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct ServerConfig {
pub style: Option<PathBuf>,
pub top_margin: Option<f32>,
pub max_volume: Option<u8>,
}

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct UserConfig {
#[serde(default)]
pub server: ServerConfig,
#[serde(default)]
pub client: ClientConfig,
}

fn find_user_config() -> Option<PathBuf> {
let path = user_config_dir().join("swayosd").join("config.toml");
if path.exists() { return Some(path); }

for path in system_config_dirs() {
let path = path.join("swayosd").join("config.toml");
if path.exists() { return Some(path); }
}

None
}

pub fn read_user_config() -> Result<UserConfig, Box<dyn Error>> {
let path = match find_user_config() {
Some(path) => path,
None => return Ok(Default::default())
};

let config_file = std::fs::read_to_string(path)?;
let config: UserConfig = toml::from_str(&config_file)?;
Ok(config)
}

0 comments on commit 438fd15

Please sign in to comment.