Skip to content

Commit

Permalink
Merge pull request #155 from JakeRoggenbuck/clean-code
Browse files Browse the repository at this point in the history
Clean code
  • Loading branch information
JakeRoggenbuck authored Jan 30, 2022
2 parents fffebbf + b390481 commit e70b2e1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 23 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ WARN: Using default config. Create file ~/.config/acs/acs.toml for custom config
```
This warning recommends creating a config file, use the following example and install at `~/.config/acs/acs.toml`

```sh
mkdir -p ~/.config/acs
cp ./acs.toml ~/.config/acs/acs.toml
```

### This is an example config
also the default settings if not config is provided

```yaml
# acs.toml
powersave_under: 20
powersave_under = 20
```

## Turn Off
Expand Down
2 changes: 2 additions & 0 deletions acs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# acs.toml
powersave_under = 20
63 changes: 56 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,75 @@ pub fn default_config() -> Config {
}
}

pub fn open_config() -> Result<Config, std::io::Error> {
// Open config file
let mut config_file = match File::open(config_path().as_str()) {
Ok(a) => a,
Err(e) => return Err(e),
};
fn open_config_file(conf_path: &str) -> Result<File, std::io::Error> {
File::open(conf_path)
}

fn read_as_string(config_file: &mut File) -> String {
// Read it to new string
let mut config: String = String::new();
config_file.read_to_string(&mut config).unwrap();
return config;
}

fn parse_as_toml(config: String) -> Config {
// Try parsing as string, warn user if broken
// e.g. WARN: missing field `charging_powersave_under` at line 1 column 1
let config_toml: Config = match toml::from_str(config.as_str()) {
match toml::from_str(config.as_str()) {
Ok(a) => a,
Err(e) => {
warn_user!(format!("{}", e));
panic!("{}", e);
}
}
}

pub fn open_config() -> Result<Config, std::io::Error> {
let conf_path = config_path();
let mut config_file: File = match open_config_file(&conf_path) {
Ok(a) => a,
Err(e) => return Err(e),
};

let config_string = read_as_string(&mut config_file);
let config_toml = parse_as_toml(config_string);

Ok(config_toml)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn default_config_test() {
let config: Config = default_config();
assert!(config.powersave_under > 0 && config.powersave_under < 100);
}

#[test]
fn open_config_file_test() -> Result<(), std::io::Error> {
let conf_file = "acs.toml";
open_config_file(conf_file)?;
Ok(())
}

#[test]
fn read_as_string_test() -> Result<(), std::io::Error> {
let conf_file = "acs.toml";
let conf_str: String = read_as_string(&mut File::open(conf_file)?);

assert!(conf_str.contains("# acs.toml\n"));
assert!(conf_str.contains("powersave_under = 20\n"));
Ok(())
}

#[test]
fn parse_as_toml_test() -> Result<(), std::io::Error> {
let conf_file = "acs.toml";
let conf_str: String = read_as_string(&mut File::open(conf_file)?);
let toml = parse_as_toml(conf_str);
assert_eq!(toml.powersave_under, 20);
Ok(())
}
}
39 changes: 24 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,9 @@ enum Command {
},
}

fn main() {
env_logger::init();
let mut main_daemon: daemon::Daemon;

// Create config directory if it doesn't exist
if !local_config_dir_exists() {
create_local_config_dir();
}

fn get_config() -> config::Config {
// Config will always exist, default or otherwise
let config: config::Config = match open_config() {
match open_config() {
Ok(conf) => conf,
Err(_) => {
warn_user!(
Expand All @@ -161,7 +153,11 @@ fn main() {
// Use default config as config
default_config()
}
};
}
}

fn parse_args(config: config::Config) {
let mut daemon: daemon::Daemon;

match Command::from_args() {
// Everything starting with "get"
Expand Down Expand Up @@ -236,8 +232,8 @@ fn main() {
no_animation,
} => match daemon_init(!quiet, delay, true, config, no_animation) {
Ok(d) => {
main_daemon = d;
main_daemon.run().unwrap_err();
daemon = d;
daemon.run().unwrap_err();
}
Err(_) => eprint!("Could not run daemon in edit mode"),
},
Expand All @@ -248,10 +244,23 @@ fn main() {
no_animation,
} => match daemon_init(true, delay, false, config, no_animation) {
Ok(d) => {
main_daemon = d;
main_daemon.run().unwrap_err();
daemon = d;
daemon.run().unwrap_err();
}
Err(_) => eprint!("Could not run daemon in monitor mode"),
},
}
}

fn main() {
env_logger::init();

// Create config directory if it doesn't exist
if !local_config_dir_exists() {
create_local_config_dir();
}

let config: config::Config = get_config();

parse_args(config);
}

0 comments on commit e70b2e1

Please sign in to comment.