Move config parsing to util crate
This commit is contained in:
@ -17,7 +17,6 @@ anyhow = "1"
|
||||
clap = "3"
|
||||
bscreensaver-command = { path = "../command" }
|
||||
bscreensaver-util = { path = "../util" }
|
||||
humantime = "2"
|
||||
log = "0.4"
|
||||
nix = "0.23"
|
||||
# git source needed until extension event error resolution fix is released
|
||||
|
@ -1,7 +1,6 @@
|
||||
#![feature(linux_pidfd)]
|
||||
#![feature(option_result_contains)]
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::{Arg, Command as ClapCommand};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use nix::{
|
||||
@ -29,50 +28,11 @@ use xcb::{randr, x, xinput, Xid};
|
||||
use xcb_xembed::embedder::Embedder;
|
||||
|
||||
use bscreensaver_command::{BCommand, create_command_window};
|
||||
use bscreensaver_util::*;
|
||||
use bscreensaver_util::{*, settings::Configuration};
|
||||
|
||||
const BLANKED_ARG: &str = "blanked";
|
||||
const LOCKED_ARG: &str = "locked";
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum DialogBackend {
|
||||
Gtk3,
|
||||
}
|
||||
|
||||
impl DialogBackend {
|
||||
pub fn binary_name(&self) -> &str {
|
||||
match self {
|
||||
Self::Gtk3 => "bscreensaver-dialog-gtk3",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for DialogBackend {
|
||||
type Error = anyhow::Error;
|
||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
"gtk3" => Ok(Self::Gtk3),
|
||||
other => Err(anyhow!("'{}' is not a valid dialog backend (valid: 'gtk3')", other)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Configuration {
|
||||
lock_timeout: Duration,
|
||||
blank_before_locking: Duration,
|
||||
dialog_backend: DialogBackend,
|
||||
}
|
||||
|
||||
impl Default for Configuration {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
lock_timeout: Duration::from_secs(60 * 10),
|
||||
blank_before_locking: Duration::ZERO,
|
||||
dialog_backend: DialogBackend::Gtk3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Monitor {
|
||||
pub root: x::Window,
|
||||
@ -132,7 +92,7 @@ macro_rules! maybe_add_fd {
|
||||
fn main() -> anyhow::Result<()> {
|
||||
init_logging("BSCREENSAVER_LOG");
|
||||
|
||||
let config = parse_config()?;
|
||||
let config = Configuration::load()?;
|
||||
|
||||
let args = ClapCommand::new("Blanks and locks the screen after a period of time")
|
||||
.author(env!("CARGO_PKG_AUTHORS"))
|
||||
@ -275,32 +235,6 @@ fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_config() -> anyhow::Result<Configuration> {
|
||||
use humantime::parse_duration;
|
||||
|
||||
let mut config = Configuration::default();
|
||||
for config_toml in load_configuration()? {
|
||||
config.lock_timeout = match config_toml.get("lock-timeout") {
|
||||
None => config.lock_timeout,
|
||||
Some(val) => parse_duration(val.as_str().ok_or(anyhow!("'lock-timeout' must be a duration string like '10m' or '90s'"))?)?,
|
||||
};
|
||||
config.blank_before_locking = match config_toml.get("blank-before-locking") {
|
||||
None => config.blank_before_locking,
|
||||
Some(val) => parse_duration(val.as_str().ok_or(anyhow!("'blank-before-locking' must be a duration string like '10m' or '90s'"))?)?,
|
||||
};
|
||||
config.dialog_backend = match config_toml.get("dialog-backend") {
|
||||
None => config.dialog_backend,
|
||||
Some(val) => DialogBackend::try_from(val.as_str().ok_or(anyhow!("'dialog-backend' must be a string"))?)?,
|
||||
};
|
||||
|
||||
if config.blank_before_locking >= config.lock_timeout {
|
||||
Err(anyhow!("'blank-before-locking' cannot be greater than 'lock-timeout'"))?
|
||||
}
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn init_signals() -> anyhow::Result<SignalFd> {
|
||||
let sigs = {
|
||||
let mut s = SigSet::empty();
|
||||
|
Reference in New Issue
Block a user