Skip to content

Commit

Permalink
Merge pull request #28 from maxleiko/pedantic
Browse files Browse the repository at this point in the history
Fixed all clippy warnings
  • Loading branch information
acheronfail authored Apr 16, 2024
2 parents 4d840d3 + 8154154 commit 0c17fb5
Show file tree
Hide file tree
Showing 35 changed files with 159 additions and 171 deletions.
2 changes: 1 addition & 1 deletion bin/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> Result<()> {
let (output, _) = local_block_on(async {
let mut acpi = netlink_acpi_listen().await?;
while let Some(event) = acpi.recv().await {
let line = format!("{}", serde_json::to_string(&event)?);
let line = serde_json::to_string(&event)?;

// flush output each time to facilitate common usage patterns, such as
// `i3stat-acpi | while read x; do ... done`, etc.
Expand Down
30 changes: 16 additions & 14 deletions bin/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ fn main() -> Result<()> {
match theme.pointer_mut(&pointer) {
Some(value) => {
let trimmed = json_value.trim();
let new_value = match serde_json::from_str::<Value>(&trimmed) {
let new_value = match serde_json::from_str::<Value>(trimmed) {
// passed a direct JSON value
Ok(value) => Ok(value),
// assume string if it doesn't definitely look like some JSON value
Err(_) if !trimmed.starts_with(&['[', '{', '\'', '"']) => {
Err(_) if !trimmed.starts_with(['[', '{', '\'', '"']) => {
Ok(Value::String(trimmed.into()))
}
// pass through any other error
Expand Down Expand Up @@ -273,18 +273,20 @@ fn main() -> Result<()> {
width,
height,
} => {
let mut click = I3ClickEvent::default();
click.button = button.0;
click.instance = Some(target.clone());
click.modifiers = modifiers.into_iter().map(|m| m.0).collect();
x.map(|x| click.x = x);
y.map(|y| click.y = y);
relative_x.map(|relative_x| click.relative_x = relative_x);
relative_y.map(|relative_y| click.relative_y = relative_y);
output_x.map(|output_x| click.output_x = output_x);
output_y.map(|output_y| click.output_y = output_y);
width.map(|width| click.width = width);
height.map(|height| click.height = height);
let click = I3ClickEvent {
name: None,
button: button.0,
instance: Some(target.clone()),
modifiers: modifiers.into_iter().map(|m| m.0).collect(),
x: x.unwrap_or_default(),
y: y.unwrap_or_default(),
relative_x: relative_x.unwrap_or_default(),
relative_y: relative_y.unwrap_or_default(),
output_x: output_x.unwrap_or_default(),
output_y: output_y.unwrap_or_default(),
width: width.unwrap_or_default(),
height: height.unwrap_or_default(),
};

let event = IpcBarEvent::Click(click);
send_and_print_response(
Expand Down
2 changes: 1 addition & 1 deletion bin/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {

let sys = System::new_with_specifics(RefreshKind::new().with_components_list());
sys.components()
.into_iter()
.iter()
.for_each(|c| println!("{:>width$.2}°C:{}", c.temperature(), c.label(), width = 6));
}

Expand Down
8 changes: 5 additions & 3 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use crate::error::Result;
use crate::i3::{I3Item, I3Markup};
use crate::theme::Theme;

type ColorAdjusters = HashMap<HexColor, Box<dyn Fn(&HexColor) -> HexColor>>;

pub struct Bar {
/// The actual bar items - represents the latest state of each individual bar item
items: Vec<I3Item>,
/// Cache for any colour adjusters created
color_adjusters: HashMap<HexColor, Box<dyn Fn(&HexColor) -> HexColor>>,
color_adjusters: ColorAdjusters,
}

impl Debug for Bar {
Expand Down Expand Up @@ -47,7 +49,7 @@ impl Bar {
pub fn new(item_count: usize) -> Bar {
Bar {
items: vec![I3Item::empty(); item_count],
color_adjusters: HashMap::new(),
color_adjusters: ColorAdjusters::new(),
}
}

Expand All @@ -65,7 +67,7 @@ impl Bar {

/// Convert the bar to a `Value`
pub fn to_value(&mut self, theme: &Theme) -> Result<Value> {
Ok(serde_json::to_value(&self.get_items(theme))?)
Ok(serde_json::to_value(self.get_items(theme))?)
}

fn get_items(&mut self, theme: &Theme) -> Vec<I3Item> {
Expand Down
23 changes: 10 additions & 13 deletions src/bar_items/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,11 @@ impl Bat {

pub async fn get_info(&self) -> Result<BatInfo> {
let name = self.name()?.to_owned();
Ok(
try_join!(self.percent(), self.get_state()).map(|(charge, state)| BatInfo {
name,
charge,
state,
})?,
)
try_join!(self.percent(), self.get_state()).map(|(charge, state)| BatInfo {
name,
charge,
state,
})
}

pub async fn find_all() -> Result<Vec<Bat>> {
Expand Down Expand Up @@ -212,7 +210,7 @@ impl Battery {
let name = if info.name == "BAT0" {
icon
} else {
info.name.as_str().into()
info.name.as_str()
};
I3Item::new(format!("{} {:.0}%", name, info.charge))
.short_text(format!("{:.0}%", info.charge))
Expand All @@ -229,14 +227,14 @@ impl BarItem for Battery {

let mut show_watts = false;
let mut p = Paginator::new();
if batteries.len() == 0 {
if batteries.is_empty() {
bail!("no batteries found");
} else {
p.set_len(batteries.len())?;
}

let dbus = dbus_connection(BusType::Session).await?;
let notifications = NotificationsProxy::new(&dbus).await?;
let notifications = NotificationsProxy::new(dbus).await?;
let mut on_acpi_event = battery_acpi_events().await?;
let mut sent_critical_notification = false;
loop {
Expand Down Expand Up @@ -338,9 +336,8 @@ async fn battery_acpi_events() -> Result<Receiver<BatteryAcpiEvent>> {
_ => continue,
};

if result.is_err() {
// SAFETY: we just checked with `.is_err()`
break result.unwrap_err();
if let Err(err) = result {
break err;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/bar_items/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl DiskStats {
name,
ByteSize(self.available_bytes).to_string_as(true)
),
format!("{}", name),
name,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bar_items/dunst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl BarItem for Dunst {
async fn start(&self, mut ctx: Context) -> Result<StopAction> {
// get initial state
let connection = dbus_connection(BusType::Session).await?;
let dunst_proxy = DunstProxy::new(&connection).await?;
let dunst_proxy = DunstProxy::new(connection).await?;
let _ = ctx
.update_item(Dunst::item(&ctx.config.theme, dunst_proxy.paused().await?))
.await;
Expand Down
2 changes: 1 addition & 1 deletion src/bar_items/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl LightFile {
}

// sort by max brightness
backlights.sort_unstable_by_key(|ref pair| pair.1);
backlights.sort_unstable_by_key(|pair| pair.1);

// return a light for the "brightest" backlight
match backlights.last() {
Expand Down
14 changes: 6 additions & 8 deletions src/bar_items/net_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct NetUsage {

impl NetUsage {
fn get_color(&self, theme: &Theme, bytes: u64) -> Option<HexColor> {
if self.thresholds.len() == 0 {
if self.thresholds.is_empty() {
return None;
}

Expand Down Expand Up @@ -91,7 +91,7 @@ fn format_bytes(bytes: u64, si: bool, as_bits: bool) -> String {
impl BarItem for NetUsage {
async fn start(&self, mut ctx: Context) -> Result<StopAction> {
let fg = |bytes: u64, theme: &Theme| {
self.get_color(&theme, bytes)
self.get_color(theme, bytes)
.map(|c| format!(r#" foreground="{}""#, c))
.unwrap_or("".into())
};
Expand Down Expand Up @@ -126,7 +126,7 @@ impl BarItem for NetUsage {

// this returns the number of bytes since the last refresh
let (down, up) = networks.iter().fold((0, 0), |(d, u), (interface, net)| {
if self.ignored_interfaces.contains(&interface) {
if self.ignored_interfaces.contains(interface) {
(d, u)
} else {
(d + net.received(), u + net.transmitted())
Expand Down Expand Up @@ -157,11 +157,9 @@ impl BarItem for NetUsage {
.await?;

// swap between bits and bytes on click
if let Some(event) = ctx.wait_for_event(Some(self.interval)).await {
if let BarEvent::Click(click) = event {
if click.button == I3Button::Left {
display.next();
}
if let Some(BarEvent::Click(click)) = ctx.wait_for_event(Some(self.interval)).await {
if click.button == I3Button::Left {
display.next();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bar_items/nic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'a> Connection<'a> {

Connection {
name: &interface.name,
addr: &addr,
addr,
detail: wireless_info.map(|info| match (info.ssid, info.signal) {
(Some(ssid), Some(signal)) => {
ConnectionDetail::SsidAndSignal(ssid.to_string(), signal)
Expand Down
5 changes: 2 additions & 3 deletions src/bar_items/pulse/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,13 @@ impl RcCell<PulseState> {
{
let mut tx = Some(tx);
let mut f = Some(failure_fn);
move |success| match (tx.take(), f.take()) {
(Some(tx), Some(f)) => {
move |success| {
if let (Some(tx), Some(f)) = (tx.take(), f.take()) {
let _ = tx.send(CustomResponse::Json(json!(match success {
true => PulseResponse::Success,
false => PulseResponse::Failure(f()),
})));
}
_ => {}
}
}

Expand Down
49 changes: 21 additions & 28 deletions src/bar_items/pulse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl RcCell<PulseState> {
}

// get the next object (that isn't a source monitor)
let next_obj = match dir.cycle(curr_obj_idx, &objects, |o| !o.is_source_monitor) {
let next_obj = match dir.cycle(curr_obj_idx, objects, |o| !o.is_source_monitor) {
Some(obj) => obj,
None => return,
};
Expand Down Expand Up @@ -219,7 +219,7 @@ impl RcCell<PulseState> {
// groups, etc)
let mut inner = self.clone();
let next_obj_index = next_obj.index;
self.set_object_port(what, next_obj_index, &next_prt_name, move |success| {
self.set_object_port(what, next_obj_index, next_prt_name, move |success| {
// sometimes setting the active port doesn't change the default, so check for
// that and set it ourselves if needed
let should_try_set_default = success
Expand Down Expand Up @@ -252,19 +252,15 @@ impl RcCell<PulseState> {
log::trace!("set_{what}_port_by_index {object_idx} {port_name}");
match what {
Object::Sink => {
introspect.set_sink_port_by_index(object_idx, &port_name, Some(Box::new(f)));
introspect.set_sink_port_by_index(object_idx, port_name, Some(Box::new(f)));
}
Object::Source => {
introspect.set_source_port_by_index(object_idx, &port_name, Some(Box::new(f)));
introspect.set_source_port_by_index(object_idx, port_name, Some(Box::new(f)));
}
}
}

fn update_volume<'a, 'b>(
&'a self,
cv: &'b mut ChannelVolumes,
vol: Vol,
) -> &'b mut ChannelVolumes {
fn update_volume<'b>(&self, cv: &'b mut ChannelVolumes, vol: Vol) -> &'b mut ChannelVolumes {
let step = Volume::NORMAL.0 / 100;
let current_pct = cv.max().0 / step;
match vol {
Expand Down Expand Up @@ -302,7 +298,7 @@ impl RcCell<PulseState> {
F: FnMut(bool) + 'static,
{
log::trace!("set_volume_{what} {vol}");
(match what {
if let Some(p) = match what {
Object::Sink => self.default_sink().map(|mut p| {
self.set_volume_sink(p.index, self.update_volume(&mut p.volume, vol), f);
p
Expand All @@ -311,20 +307,19 @@ impl RcCell<PulseState> {
self.set_volume_source(p.index, self.update_volume(&mut p.volume, vol), f);
p
}),
})
.map(|p| {
} {
// send notification
let _ = self.tx.send(p.notify_volume_mute());
self.play_volume_sample_if_enabled(what);
});
}
}

fn set_mute<F>(&mut self, what: Object, mute: bool, f: F)
where
F: FnMut(bool) + 'static,
{
log::trace!("set_mute_{what} {mute}");
(match what {
if let Some(p) = match what {
Object::Sink => self.default_sink().map(|mut p| {
p.mute = mute;
self.set_mute_sink(p.index, p.mute, f);
Expand All @@ -335,18 +330,17 @@ impl RcCell<PulseState> {
self.set_mute_source(p.index, p.mute, f);
p
}),
})
.map(|p| {
} {
let _ = self.tx.send(p.notify_volume_mute());
self.play_volume_sample_if_enabled(what);
});
}
}

fn toggle_mute<F>(&mut self, what: Object, f: F)
where
F: FnMut(bool) + 'static,
{
(match what {
if let Some(p) = match what {
Object::Sink => self.default_sink().map(|mut p| {
p.mute = !p.mute;
self.set_mute_sink(p.index, p.mute, f);
Expand All @@ -357,11 +351,10 @@ impl RcCell<PulseState> {
self.set_mute_source(p.index, p.mute, f);
p
}),
})
.map(|p| {
} {
let _ = self.tx.send(p.notify_volume_mute());
self.play_volume_sample_if_enabled(what);
});
}
}

fn play_volume_sample_if_enabled(&mut self, what: Object) {
Expand Down Expand Up @@ -516,13 +509,13 @@ impl RcCell<PulseState> {
});
};

info.default_sink_name
.as_ref()
.map(|name| update_if_needed(&mut inner, Object::Sink, name.to_string().into()));
if let Some(name) = info.default_sink_name.as_ref() {
update_if_needed(&mut inner, Object::Sink, name.to_string().into())
}

info.default_source_name
.as_ref()
.map(|name| update_if_needed(&mut inner, Object::Source, name.to_string().into()));
if let Some(name) = info.default_source_name.as_ref() {
update_if_needed(&mut inner, Object::Source, name.to_string().into())
}

inner.update_item();
});
Expand Down Expand Up @@ -639,7 +632,7 @@ impl BarItem for Pulse {
});

let dbus = dbus_connection(BusType::Session).await?;
let notifications = NotificationsProxy::new(&dbus).await?;
let notifications = NotificationsProxy::new(dbus).await?;
loop {
tokio::select! {
// handle events
Expand Down
Loading

0 comments on commit 0c17fb5

Please sign in to comment.