Skip to content

Commit

Permalink
clippy pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Tricoire committed Apr 14, 2024
1 parent 830e184 commit 5e97cf6
Show file tree
Hide file tree
Showing 32 changed files with 141 additions and 155 deletions.
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 @@ -121,13 +121,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 @@ -188,7 +186,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 @@ -205,14 +203,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 @@ -314,9 +312,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
12 changes: 6 additions & 6 deletions src/bar_items/pulse/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ impl InOut {
let current_idx = self.current_port_idx();
match dir {
Dir::Next => self.ports[current_idx + 1..]
.into_iter()
.iter()
.find(|p| p.available()),
Dir::Prev => self.ports[..current_idx]
.into_iter()
.iter()
.rev()
.find(|p| p.available()),
}
Expand All @@ -207,7 +207,7 @@ impl InOut {
r#"<span foreground="{}">{} {}%</span>"#,
if self.mute { theme.dim } else { theme.fg },
self.port_symbol()
.unwrap_or_else(|| match (what, self.mute) {
.unwrap_or(match (what, self.mute) {
(Object::Sink, false) => "",
(Object::Sink, true) => "",
(Object::Source, false) => "󰍬",
Expand Down Expand Up @@ -270,20 +270,20 @@ impl Dir {
/// Returns `None` if
/// * `items` was empty
/// * `f` excludes all items
pub fn cycle<'a, 'b, T, F>(&'a self, start: usize, items: &'b [T], f: F) -> Option<&'b T>
pub fn cycle<'b, T, F>(&self, start: usize, items: &'b [T], f: F) -> Option<&'b T>
where
F: Fn(&&T) -> bool,
{
let limit = items.len() * 2;
match self {
Dir::Next => items
.into_iter()
.iter()
.cycle()
.skip(start + 1)
.take(limit)
.find(f),
Dir::Prev => items
.into_iter()
.iter()
.rev()
.cycle()
.skip(items.len() - start)
Expand Down
6 changes: 3 additions & 3 deletions src/bar_items/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl BarItem for Script {
}
BarEvent::Click(c) => {
env.remove("I3_SIGNAL");
c.name.map(|name| {
env.insert("I3_NAME", name.to_string());
});
if let Some(name) = c.name {
env.insert("I3_NAME", name);
}
env.insert(
"I3_MODIFIERS",
c.modifiers
Expand Down
Loading

0 comments on commit 5e97cf6

Please sign in to comment.