Skip to content

Commit

Permalink
chore: move to 2021 edition, clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
doums committed Aug 10, 2022
1 parent 8b14cc9 commit 03469f6
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 99 deletions.
258 changes: 202 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "baru"
version = "0.2.8"
version = "0.2.9"
authors = ["pierre <[email protected]>"]
edition = "2018"
edition = "2021"
links = "netlink,audio"
build = "build.rs"

[dependencies]
chrono= "0.4"
regex = "1"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
serde_yaml = "0.9"

[build-dependencies]
cmake = "0.1"
10 changes: 2 additions & 8 deletions src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ pub struct Config {

#[derive(Debug)]
pub struct InternalConfig<'a> {
name: &'a str,
low_level: u32,
full_design: bool,
tick: Duration,
uevent: String,
now_attribute: String,
Expand Down Expand Up @@ -118,9 +116,7 @@ impl<'a> TryFrom<&'a MainConfig> for InternalConfig<'a> {
let uevent = format!("{}{}/{}", SYS_PATH, &name, UEVENT);
let attribute_prefix = find_attribute_prefix(&uevent)?;
Ok(InternalConfig {
name,
low_level,
full_design,
tick,
uevent,
now_attribute: format!("{}_{}_{}", POWER_SUPPLY, attribute_prefix, NOW_ATTRIBUTE),
Expand All @@ -137,7 +133,6 @@ impl<'a> TryFrom<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Battery<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -154,7 +149,6 @@ impl<'a> Battery<'a> {
}
}
Battery {
config,
format,
placeholder,
}
Expand Down Expand Up @@ -234,10 +228,10 @@ fn parse_attributes(
let mut status = None;
for line in f.lines() {
if now.is_none() {
now = parse_attribute(&line, &now_attribute);
now = parse_attribute(&line, now_attribute);
}
if full.is_none() {
full = parse_attribute(&line, &full_attribute);
full = parse_attribute(&line, full_attribute);
}
if status.is_none() {
status = parse_status(&line);
Expand Down
4 changes: 1 addition & 3 deletions src/brightness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
let mut label = LABEL;
if let Some(c) = &config.brightness {
if let Some(v) = &c.sys_path {
sys_path = &v;
sys_path = v;
}
if let Some(t) = c.tick {
tick = Duration::from_millis(t as u64)
Expand All @@ -61,7 +61,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Brightness<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -79,7 +78,6 @@ impl<'a> Brightness<'a> {
}
Brightness {
placeholder,
config,
format,
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Cpu<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -93,7 +92,6 @@ impl<'a> Cpu<'a> {
}
Cpu {
placeholder,
config,
format,
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/cpu_freq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl<'a> TryFrom<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct CpuFreq<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -154,7 +153,6 @@ impl<'a> CpuFreq<'a> {
}
CpuFreq {
placeholder,
config,
format,
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct DateTime<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -77,7 +76,6 @@ impl<'a> DateTime<'a> {
}
DateTime {
placeholder,
config,
format,
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,31 @@ mod tests {
#[test]
fn parse_empty_format() {
let result = parse_format("");
assert_eq!(result.is_empty(), true);
assert!(result.is_empty());
}

#[test]
fn parse_one_char() {
let result = parse_format("a");
assert_eq!(result.is_empty(), true);
assert!(result.is_empty());
}

#[test]
fn parse_one_percent() {
let result = parse_format("%");
assert_eq!(result.is_empty(), true);
assert!(result.is_empty());
}

#[test]
fn parse_one_escaped_percent_i() {
let result = parse_format("\\%");
assert_eq!(result.is_empty(), true);
assert!(result.is_empty());
}

#[test]
fn parse_one_escaped_percent_ii() {
let result = parse_format("\\%%");
assert_eq!(result.is_empty(), true);
assert!(result.is_empty());
}

#[test]
Expand Down
4 changes: 1 addition & 3 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Memory<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -106,7 +105,6 @@ impl<'a> Memory<'a> {
}
Memory {
placeholder,
config,
format,
}
}
Expand Down Expand Up @@ -245,7 +243,7 @@ fn humanize<'a>(v1: f32, v2: f32, u1: &'a str, u2: &'a str) -> String {

fn find_meminfo<'a>(regex: &Regex, meminfo: &'a str, error: &'a str) -> Result<i32, String> {
regex
.captures(&meminfo)
.captures(meminfo)
.ok_or_else(|| error.to_string())?
.get(1)
.ok_or_else(|| error.to_string())?
Expand Down
2 changes: 0 additions & 2 deletions src/mic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Mic<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -80,7 +79,6 @@ impl<'a> Mic<'a> {
}
Mic {
placeholder,
config,
format,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ impl<'a> ModuleData<'a> {
pub fn new_data(&mut self, value: Option<&str>, label: Option<&str>) {
let mut module_format = self.module.format().to_string();
module_format = match value {
Some(v) => module_format.replace("%v", &v),
Some(v) => module_format.replace("%v", v),
None => module_format.replace("%v", ""),
};
module_format = match label {
Some(l) => module_format.replace("%l", &l),
Some(l) => module_format.replace("%l", l),
None => module_format.replace("%l", ""),
};
self.data = Some(module_format);
Expand Down
2 changes: 0 additions & 2 deletions src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Sound<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -80,7 +79,6 @@ impl<'a> Sound<'a> {
}
Sound {
placeholder,
config,
format,
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> TryFrom<&'a MainConfig> for InternalConfig<'a> {
let mut high_label = HIGH_LABEL;
if let Some(c) = &config.temperature {
if let Some(v) = &c.coretemp {
coretemp = &v;
coretemp = v;
}
if let Some(v) = c.high_level {
high_level = v;
Expand Down Expand Up @@ -108,7 +108,6 @@ impl<'a> TryFrom<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Temperature<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -126,7 +125,6 @@ impl<'a> Temperature<'a> {
}
Temperature {
placeholder,
config,
format,
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/wired.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Wired<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -96,7 +95,6 @@ impl<'a> Wired<'a> {
}
Wired {
placeholder,
config,
format,
}
}
Expand Down Expand Up @@ -131,7 +129,7 @@ pub fn run(
let mut iteration_end: Duration;
loop {
iteration_start = Instant::now();
if let Some(state) = netlink::wired_data(&config.interface) {
if let Some(state) = netlink::wired_data(config.interface) {
if let WiredState::Connected = state {
tx.send(ModuleMsg(key, None, Some(config.label.to_string())))?;
} else if config.discrete {
Expand Down
4 changes: 1 addition & 3 deletions src/wireless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> {
#[derive(Debug)]
pub struct Wireless<'a> {
placeholder: &'a str,
config: &'a MainConfig,
format: &'a str,
}

Expand All @@ -110,7 +109,6 @@ impl<'a> Wireless<'a> {
}
Wireless {
placeholder,
config,
format,
}
}
Expand Down Expand Up @@ -148,7 +146,7 @@ pub fn run(
let label;
let mut essid = "".to_owned();
let mut signal = None;
if let Some(state) = netlink::wireless_data(&config.interface) {
if let Some(state) = netlink::wireless_data(config.interface) {
if let WirelessState::Connected(data) = state {
label = config.label;
if let Some(strength) = data.signal {
Expand Down

0 comments on commit 03469f6

Please sign in to comment.