Skip to content

Commit

Permalink
Enable nursery lints
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Oct 16, 2024
1 parent a992bc0 commit 829a945
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cli/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
// Add multiple messages to the database
#[allow(clippy::module_name_repetitions)]
pub async fn import_messages<B: Backend>(
db: &mut Database<B>,
db: &Database<B>,
config: &Option<Config>,
new_messages: Vec<NewMessage>,
) -> Result<Vec<Message>> {
Expand Down
16 changes: 6 additions & 10 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![deny(clippy::clone_on_ref_ptr)]
#![deny(clippy::pedantic)]
#![warn(clippy::clone_on_ref_ptr, clippy::pedantic, clippy::nursery)]
#![allow(clippy::future_not_send, clippy::missing_const_for_fn)]

mod cli;
mod config;
Expand Down Expand Up @@ -111,7 +111,7 @@ fn states_from_view_message_state(state: ViewMessageState) -> Vec<State> {

async fn run<B: Backend + Send + Sync + 'static>(
config: Option<Config>,
mut db: Database<B>,
db: Database<B>,
) -> Result<()> {
let cli = Cli::parse();
let formatter = create_formatter(&cli);
Expand All @@ -132,17 +132,13 @@ async fn run<B: Backend + Send + Sync + 'static>(
content,
state: Some(cli_state),
}];
let messages = import_messages(&mut db, &config, raw_messages).await?;
let messages = import_messages(&db, &config, raw_messages).await?;
print!("{}", formatter.format_messages(&messages)?);
}

Command::Import { format } => {
let messages = import_messages(
&mut db,
&config,
read_messages_stdin(stdin().lock(), format),
)
.await?;
let messages =
import_messages(&db, &config, read_messages_stdin(stdin().lock(), format)).await?;
print!("{}", formatter.format_messages(&messages)?);
}

Expand Down
8 changes: 4 additions & 4 deletions cli/src/message_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl MessageComponents {
// Attempt to truncate the combined length of the message components down
// to max_length. If this isn't possible, the message components will be
// truncated as much as possible.
pub fn truncate(self, max_length: usize) -> MessageComponents {
pub fn truncate(self, max_length: usize) -> Self {
let total_length =
8 + self.content.width() + self.mailbox.width() + self.time.len() + self.appendix.len();
if total_length <= max_length {
Expand All @@ -30,22 +30,22 @@ impl MessageComponents {
let others_length = total_length - self.mailbox.width();
if others_length + 4 <= max_length {
let mailbox = truncate_string(&self.mailbox, max_length - others_length).0;
return MessageComponents { mailbox, ..self };
return Self { mailbox, ..self };
}

// Next try to truncate the content
let others_length = total_length - self.content.width();
if others_length + 4 <= max_length {
let content = truncate_string(&self.content, max_length - others_length).0;
return MessageComponents { content, ..self };
return Self { content, ..self };
}

// Lastly, truncate the content and the mailbox
let others_length = total_length - self.content.width() - self.mailbox.width();
let mailbox_and_content_length = max(max_length.saturating_sub(others_length) / 2, 4);
let mailbox = truncate_string(&self.mailbox, mailbox_and_content_length).0;
let content = truncate_string(&self.content, mailbox_and_content_length).0;
MessageComponents {
Self {
content,
mailbox,
..self
Expand Down
2 changes: 1 addition & 1 deletion cli/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct TruncatedLine {
impl TruncatedLine {
// Create a new instance
pub fn new(max_columns: usize) -> Self {
TruncatedLine {
Self {
available_columns: max_columns,
sections: Vec::default(),
}
Expand Down
8 changes: 4 additions & 4 deletions cli/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl App {
db: Database<B>,
initial_mailbox: Option<database::Mailbox>,
initial_states: Vec<State>,
) -> Result<App> {
) -> Result<Self> {
let db = Arc::new(db);
let (worker_tx, worker_rx) = spawn(Arc::clone(&db));
let mut app = App {
let mut app = Self {
active_pane: Pane::Messages,
mailboxes: TreeList::new(),
messages: MultiselectList::new(),
Expand Down Expand Up @@ -145,15 +145,15 @@ impl App {
}

// Update the mailboxes list
pub fn update_mailboxes(&mut self) -> Result<()> {
pub fn update_mailboxes(&self) -> Result<()> {
self.worker_tx.send(Request::LoadMailboxes(
Filter::new().with_states(self.get_active_states()),
))?;
Ok(())
}

// Update the messages list based on the mailbox and other filters
pub fn update_messages(&mut self) -> Result<()> {
pub fn update_messages(&self) -> Result<()> {
let filter = self.get_display_filter();
self.worker_tx.send(Request::LoadMessages(filter))?;
Ok(())
Expand Down
28 changes: 12 additions & 16 deletions cli/src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ fn handle_mailbox_key(app: &mut App, key: KeyEvent) -> Result<()> {
app.mailboxes.parent();
}
KeyCode::Char('a') => {
if let Some(active_mailbox) = old_active_mailbox.clone() {
if let Some(active_mailbox) = old_active_mailbox {
app.set_mailbox_message_state(active_mailbox, State::Archived)?;
}
return Ok(());
}
KeyCode::Char('r') => {
if let Some(active_mailbox) = old_active_mailbox.clone() {
if let Some(active_mailbox) = old_active_mailbox {
app.set_mailbox_message_state(active_mailbox, State::Read)?;
}
return Ok(());
}
KeyCode::Char('u') => {
if let Some(active_mailbox) = old_active_mailbox.clone() {
if let Some(active_mailbox) = old_active_mailbox {
app.set_mailbox_message_state(active_mailbox, State::Unread)?;
}
return Ok(());
Expand All @@ -163,11 +163,9 @@ fn handle_mailbox_key(app: &mut App, key: KeyEvent) -> Result<()> {
// If the new active mailbox is a descendant of the old one or if there wasn't an old active mailbox, the
// messages list can be optimistically updated by filtering against the new active mailbox instead of needing
// to refresh the whole list
let local_update = if let Some(old_active_mailbox) = old_active_mailbox {
let local_update = old_active_mailbox.map_or(true, |old_active_mailbox| {
old_active_mailbox.is_ancestor_of(active_mailbox)
} else {
true
};
});

if local_update {
// Optimistically update the messages list
Expand Down Expand Up @@ -258,7 +256,7 @@ fn ui<B: Backend>(frame: &mut Frame<B>, app: &mut App) {
}

// Render the footer section of the UI
fn render_footer<B: Backend>(frame: &mut Frame<B>, app: &mut App, area: Rect) {
fn render_footer<B: Backend>(frame: &mut Frame<B>, app: &App, area: Rect) {
const ACTIVE_STYLE: Style = Style::new().fg(Color::Black).bg(Color::Green);
const INACTIVE_STYLE: Style = Style::new();
const SELECTING_STYLE: Style = Style::new().fg(Color::LightBlue);
Expand Down Expand Up @@ -338,10 +336,9 @@ fn render_mailboxes<B: Backend>(frame: &mut Frame<B>, app: &mut App, area: Rect)
.border_style(border_style)
.title(format!(
"Mailboxes ({}{})",
match app.mailboxes.get_cursor() {
None => String::new(),
Some(index) => format!("{}/", index + 1),
},
app.mailboxes
.get_cursor()
.map_or_else(String::new, |index| format!("{}/", index + 1)),
app.mailboxes.get_items().len()
)),
)
Expand Down Expand Up @@ -398,10 +395,9 @@ fn render_messages<B: Backend>(frame: &mut Frame<B>, app: &mut App, area: Rect)
.border_style(border_style)
.title(format!(
"Messages ({}{})",
match app.messages.get_cursor() {
None => String::new(),
Some(index) => format!("{}/", index + 1),
},
app.messages
.get_cursor()
.map_or_else(String::new, |index| format!("{}/", index + 1)),
app.messages.get_items().len()
)),
)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tui/monotonic_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct MonotonicCounter {
impl MonotonicCounter {
// Create a new monotonic counter
pub fn new() -> Self {
MonotonicCounter {
Self {
last_id: Arc::new(AtomicU64::new(0)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tui/multiselect_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
{
// Create a new list with no items
pub fn new() -> Self {
MultiselectList {
Self {
state: ListState::default(),
items: vec![],
selected_items: HashSet::new(),
Expand Down
15 changes: 5 additions & 10 deletions cli/src/tui/navigable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ where
self.set_cursor(match self.get_items().len() {
0 => None,
num_items => {
let new_index = (match self.get_cursor() {
None => -1,
Some(index) => index as i32,
}) + change;
let new_index = self.get_cursor().map_or(-1, |index| index as i32) + change;
// Bounds check the new index
Some((new_index).clamp(0, num_items as i32 - 1) as usize)
}
Expand Down Expand Up @@ -75,20 +72,18 @@ where

// Replace the list's items with a new set of items
fn replace_items(&mut self, items: Vec<Item>) {
let cursor_candidates = match self.get_cursor() {
None => vec![],
let cursor_candidates = self.get_cursor().map_or_else(Vec::new, |index| {
// The items at and after the cursor are the first candidates,
// starting with the items closest to the cursor, followed by
// the items before the cursor, again starting with the items
// closest to the cursor
Some(index) => self
.get_items()
self.get_items()
.iter()
.skip(index)
.chain(self.get_items().iter().take(index).rev())
.map(Keyed::get_key)
.collect(),
};
.collect()
});

*self.get_items_mut() = items;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/tui/tree_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
impl<Item: Depth + Keyed> TreeList<Item> {
// Create a new list with no items
pub fn new() -> Self {
TreeList {
Self {
state: ListState::default(),
items: vec![],
}
Expand Down

0 comments on commit 829a945

Please sign in to comment.