Skip to content

Commit

Permalink
refactor(clippy): apply all super pedantic lints
Browse files Browse the repository at this point in the history
Realised that I hadn't turned on super pedantic mode for clippy in the
komorebi-core and komorebic crates. This commit ensures the same clippy
config across all crates and applies the lint suggestions that arose as
a result of turning on the same config everywhere.
  • Loading branch information
LGUG2Z committed Aug 20, 2021
1 parent 1625ca6 commit 292bdb2
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 125 deletions.
3 changes: 2 additions & 1 deletion komorebi-core/src/cycle_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub enum CycleDirection {
}

impl CycleDirection {
pub fn next_idx(&self, idx: usize, len: usize) -> usize {
#[must_use]
pub const fn next_idx(&self, idx: usize, len: usize) -> usize {
match self {
CycleDirection::Previous => {
if idx == 0 {
Expand Down
63 changes: 27 additions & 36 deletions komorebi-core/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ pub enum Layout {

#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)]
#[strum(serialize_all = "snake_case")]
pub enum LayoutFlip {
pub enum Flip {
Horizontal,
Vertical,
HorizontalAndVertical,
}

impl Layout {
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn resize(
&self,
unaltered: &Rect,
Expand All @@ -35,10 +37,14 @@ impl Layout {
sizing: Sizing,
step: Option<i32>,
) -> Option<Rect> {
if !matches!(self, Self::BSP) {
return None;
};

let max_divisor = 1.005;
let mut r = resize.unwrap_or_default();

let resize_step = if let Some(step) = step { step } else { 50 };
let resize_step = step.unwrap_or(50);

match edge {
OperationDirection::Left => match sizing {
Expand Down Expand Up @@ -117,19 +123,21 @@ impl Layout {
},
};

if !r.eq(&Rect::default()) {
Option::from(r)
} else {
if r.eq(&Rect::default()) {
None
} else {
Option::from(r)
}
}

#[must_use]
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
pub fn calculate(
&self,
area: &Rect,
len: NonZeroUsize,
container_padding: Option<i32>,
layout_flip: Option<LayoutFlip>,
layout_flip: Option<Flip>,
resize_dimensions: &[Option<Rect>],
) -> Vec<Rect> {
let len = usize::from(len);
Expand Down Expand Up @@ -187,24 +195,6 @@ impl Layout {
}
}

impl Layout {
pub fn next(&mut self) {
match self {
Layout::BSP => *self = Layout::Columns,
Layout::Columns => *self = Layout::Rows,
Layout::Rows => *self = Layout::BSP,
}
}

pub fn previous(&mut self) {
match self {
Layout::BSP => *self = Layout::Rows,
Layout::Columns => *self = Layout::BSP,
Layout::Rows => *self = Layout::Columns,
}
}
}

fn calculate_resize_adjustments(resize_dimensions: &[Option<Rect>]) -> Vec<Option<Rect>> {
let mut resize_adjustments = resize_dimensions.to_vec();

Expand All @@ -213,6 +203,7 @@ fn calculate_resize_adjustments(resize_dimensions: &[Option<Rect>]) -> Vec<Optio
if let Some(resize_ref) = opt {
if i > 0 {
if resize_ref.left != 0 {
#[allow(clippy::if_not_else)]
let range = if i == 1 {
0..1
} else if i & 1 != 0 {
Expand Down Expand Up @@ -291,7 +282,7 @@ fn recursive_fibonacci(
idx: usize,
count: usize,
area: &Rect,
layout_flip: Option<LayoutFlip>,
layout_flip: Option<Flip>,
resize_adjustments: Vec<Option<Rect>>,
) -> Vec<Rect> {
let mut a = *area;
Expand All @@ -313,37 +304,37 @@ fn recursive_fibonacci(

let (main_x, alt_x, alt_y, main_y);

match layout_flip {
Some(flip) => match flip {
LayoutFlip::Horizontal => {
if let Some(flip) = layout_flip {
match flip {
Flip::Horizontal => {
main_x = resized.left + half_width + (half_width - half_resized_width);
alt_x = resized.left;

alt_y = resized.top + half_resized_height;
main_y = resized.top;
}
LayoutFlip::Vertical => {
Flip::Vertical => {
main_y = resized.top + half_height + (half_height - half_resized_height);
alt_y = resized.top;

main_x = resized.left;
alt_x = resized.left + half_resized_width;
}
LayoutFlip::HorizontalAndVertical => {
Flip::HorizontalAndVertical => {
main_x = resized.left + half_width + (half_width - half_resized_width);
alt_x = resized.left;
main_y = resized.top + half_height + (half_height - half_resized_height);
alt_y = resized.top;
}
},
None => {
main_x = resized.left;
alt_x = resized.left + half_resized_width;
main_y = resized.top;
alt_y = resized.top + half_resized_height;
}
} else {
main_x = resized.left;
alt_x = resized.left + half_resized_width;
main_y = resized.top;
alt_y = resized.top + half_resized_height;
}

#[allow(clippy::if_not_else)]
if count == 0 {
vec![]
} else if count == 1 {
Expand Down
10 changes: 7 additions & 3 deletions komorebi-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]

use std::str::FromStr;

use clap::ArgEnum;
Expand All @@ -8,8 +11,8 @@ use strum::Display;
use strum::EnumString;

pub use cycle_direction::CycleDirection;
pub use layout::Flip;
pub use layout::Layout;
pub use layout::LayoutFlip;
pub use operation_direction::OperationDirection;
pub use rect::Rect;

Expand Down Expand Up @@ -39,7 +42,7 @@ pub enum SocketMessage {
AdjustContainerPadding(Sizing, i32),
AdjustWorkspacePadding(Sizing, i32),
ChangeLayout(Layout),
FlipLayout(LayoutFlip),
FlipLayout(Flip),
// Monitor and Workspace Commands
EnsureWorkspaces(usize, usize),
NewWorkspace,
Expand Down Expand Up @@ -99,7 +102,8 @@ pub enum Sizing {
}

impl Sizing {
pub fn adjust_by(&self, value: i32, adjustment: i32) -> i32 {
#[must_use]
pub const fn adjust_by(&self, value: i32, adjustment: i32) -> i32 {
match self {
Sizing::Increase => value + adjustment,
Sizing::Decrease => {
Expand Down
86 changes: 37 additions & 49 deletions komorebi-core/src/operation_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use serde::Serialize;
use strum::Display;
use strum::EnumString;

use crate::Flip;
use crate::Layout;
use crate::LayoutFlip;

#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)]
#[strum(serialize_all = "snake_case")]
Expand All @@ -17,59 +17,46 @@ pub enum OperationDirection {
}

impl OperationDirection {
pub fn opposite(self) -> Self {
#[must_use]
pub const fn opposite(self) -> Self {
match self {
OperationDirection::Left => OperationDirection::Right,
OperationDirection::Right => OperationDirection::Left,
OperationDirection::Up => OperationDirection::Down,
OperationDirection::Down => OperationDirection::Up,
Self::Left => Self::Right,
Self::Right => Self::Left,
Self::Up => Self::Down,
Self::Down => Self::Up,
}
}

fn flip_direction(
direction: &OperationDirection,
layout_flip: Option<LayoutFlip>,
) -> OperationDirection {
if let Some(flip) = layout_flip {
match direction {
OperationDirection::Left => match flip {
LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Right
}
_ => *direction,
},
OperationDirection::Right => match flip {
LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Left
}
_ => *direction,
},
OperationDirection::Up => match flip {
LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Down
}
_ => *direction,
},
OperationDirection::Down => match flip {
LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Up
}
_ => *direction,
},
}
} else {
*direction
}
fn flip_direction(direction: Self, layout_flip: Option<Flip>) -> Self {
layout_flip.map_or(direction, |flip| match direction {
Self::Left => match flip {
Flip::Horizontal | Flip::HorizontalAndVertical => Self::Right,
Flip::Vertical => direction,
},
Self::Right => match flip {
Flip::Horizontal | Flip::HorizontalAndVertical => Self::Left,
Flip::Vertical => direction,
},
Self::Up => match flip {
Flip::Vertical | Flip::HorizontalAndVertical => Self::Down,
Flip::Horizontal => direction,
},
Self::Down => match flip {
Flip::Vertical | Flip::HorizontalAndVertical => Self::Up,
Flip::Horizontal => direction,
},
})
}

#[must_use]
pub fn is_valid(
&self,
self,
layout: Layout,
layout_flip: Option<LayoutFlip>,
layout_flip: Option<Flip>,
idx: usize,
len: usize,
) -> bool {
match OperationDirection::flip_direction(self, layout_flip) {
match Self::flip_direction(self, layout_flip) {
OperationDirection::Up => match layout {
Layout::BSP => len > 2 && idx != 0 && idx != 1,
Layout::Columns => false,
Expand All @@ -93,9 +80,10 @@ impl OperationDirection {
}
}

pub fn new_idx(&self, layout: Layout, layout_flip: Option<LayoutFlip>, idx: usize) -> usize {
match OperationDirection::flip_direction(self, layout_flip) {
OperationDirection::Up => match layout {
#[must_use]
pub fn new_idx(self, layout: Layout, layout_flip: Option<Flip>, idx: usize) -> usize {
match Self::flip_direction(self, layout_flip) {
Self::Up => match layout {
Layout::BSP => {
if idx % 2 == 0 {
idx - 1
Expand All @@ -106,11 +94,11 @@ impl OperationDirection {
Layout::Columns => unreachable!(),
Layout::Rows => idx - 1,
},
OperationDirection::Down => match layout {
Self::Down => match layout {
Layout::BSP | Layout::Rows => idx + 1,
Layout::Columns => unreachable!(),
},
OperationDirection::Left => match layout {
Self::Left => match layout {
Layout::BSP => {
if idx % 2 == 0 {
idx - 2
Expand All @@ -121,7 +109,7 @@ impl OperationDirection {
Layout::Columns => idx - 1,
Layout::Rows => unreachable!(),
},
OperationDirection::Right => match layout {
Self::Right => match layout {
Layout::BSP | Layout::Columns => idx + 1,
Layout::Rows => unreachable!(),
},
Expand Down
7 changes: 4 additions & 3 deletions komorebi-core/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Rect {

impl Default for Rect {
fn default() -> Self {
Rect {
Self {
left: 0,
top: 0,
right: 0,
Expand All @@ -23,7 +23,7 @@ impl Default for Rect {

impl From<RECT> for Rect {
fn from(rect: RECT) -> Self {
Rect {
Self {
left: rect.left,
top: rect.top,
right: rect.right - rect.left,
Expand All @@ -42,7 +42,8 @@ impl Rect {
}
}

pub fn contains_point(&self, point: (i32, i32)) -> bool {
#[must_use]
pub const fn contains_point(&self, point: (i32, i32)) -> bool {
point.0 >= self.left
&& point.0 <= self.left + self.right
&& point.1 >= self.top
Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl Window {
&& (allow_layered || !ex_style.contains(GwlExStyle::LAYERED))
|| managed_override
{
return Ok(true)
return Ok(true);
} else if event.is_some() {
tracing::debug!("ignoring (exe: {}, title: {})", exe_name, title);
}
Expand Down
Loading

0 comments on commit 292bdb2

Please sign in to comment.