-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config file format and config file parsing
- Loading branch information
Showing
5 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |