Skip to content

Commit

Permalink
Add more lints
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Jun 21, 2024
1 parent 09ed33f commit 0c58cdf
Show file tree
Hide file tree
Showing 29 changed files with 112 additions and 64 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,23 @@ lto = true
[profile.dev.package.backtrace]
opt-level = 3

[workspace.lints.rust]
keyword_idents = "warn"
non_ascii_idents = "forbid"
noop_method_call = "warn"
unused_crate_dependencies = "warn"

[workspace.lints.clippy]
cognitive_complexity = "warn"
dbg_macro = "warn"
empty_structs_with_brackets = "warn"
explicit_iter_loop = "warn"
match_wildcard_for_single_variants = "warn"
print_stderr = "warn"
print_stdout = "warn"
question_mark = "warn"
redundant_closure_for_method_calls = "warn"
semicolon_if_nothing_returned = "warn"
unwrap_used = "warn"
used_underscore_binding = "warn"
wildcard_imports = "warn"
7 changes: 3 additions & 4 deletions gravel-core/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use itertools::Itertools;

use crate::frontend::FrontendMessage;
use crate::performance::Stopwatch;
use crate::scoring::ScoredHit;
use crate::{provider::*, scoring};
use crate::{scoring, scoring::ScoredHit, Hit, Provider};
use itertools::Itertools;
use std::sync::mpsc::Sender;

/// Holds a [`Provider`] and some additional metadata.
Expand All @@ -17,6 +15,7 @@ pub struct QueryEngine {
sender: Sender<FrontendMessage>,
}

#[derive(Debug)]
pub struct QueryResult {
pub hits: Vec<ScoredHit>,
}
Expand Down
22 changes: 20 additions & 2 deletions gravel-core/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config::PluginConfigAdapter, *};
use crate::{config::PluginConfigAdapter, Frontend, Provider, QueryEngine};
use std::collections::HashMap;

pub type ProviderFactory = Box<dyn Fn(&PluginConfigAdapter) -> Box<dyn Provider>>;
Expand All @@ -9,6 +9,24 @@ pub enum PluginFactory {
Frontend(FrontendFactory),
}

impl PluginFactory {
pub fn provider(&self) -> Option<&ProviderFactory> {
if let PluginFactory::Provider(factory) = self {
return Some(factory);
}

None
}

pub fn frontend(&self) -> Option<&FrontendFactory> {
if let PluginFactory::Frontend(factory) = self {
return Some(factory);
}

None
}
}

/// Holds metadata about a frontend or provider, as well as
/// a way to construct them.
pub struct PluginDefinition {
Expand Down Expand Up @@ -67,7 +85,7 @@ impl PluginRegistry {
self
}

pub fn get_plugin(&self, name: &str) -> Option<&PluginDefinition> {
pub fn get(&self, name: &str) -> Option<&PluginDefinition> {
self.plugins.get(name)
}
}
21 changes: 17 additions & 4 deletions gravel-core/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::frontend::FrontendMessage;
use nameof::name_of;
use std::fmt::{Debug, Formatter, Result};
use std::sync::{mpsc::Sender, Arc};

/// A provider takes a query and provides some relevant results.
Expand All @@ -7,6 +9,7 @@ pub trait Provider {
}

/// A collection of hits.
#[derive(Debug)]
pub struct ProviderResult {
pub hits: Vec<Arc<dyn Hit>>,
}
Expand All @@ -33,13 +36,15 @@ impl ProviderResult {
///
/// The hit can be given a score, in which case it will not be further
/// scored and simply ordered as-is.
pub trait Hit: Sync + Send {
pub trait Hit: Sync + Send + Debug {
fn get_title(&self) -> &str;
fn get_subtitle(&self) -> &str;
fn get_override_score(&self) -> Option<u32>;
fn action(&self, sender: &Sender<FrontendMessage>);
}

pub type SimpleHitAction = Box<dyn Fn(&SimpleHit, &Sender<FrontendMessage>) + Send + Sync>;

/// Reference implementation for [`Hit`].
///
/// Takes a function for an action and can store extra data.
Expand All @@ -48,9 +53,7 @@ pub struct SimpleHit {
subtitle: Box<str>,
override_score: Option<u32>,

// I think inlining it is easier to read in this case, due to T.
#[allow(clippy::type_complexity)]
action_func: Box<dyn Fn(&Self, &Sender<FrontendMessage>) + Send + Sync>,
action_func: SimpleHitAction,
}

impl SimpleHit {
Expand All @@ -75,6 +78,16 @@ impl SimpleHit {
}
}

impl Debug for SimpleHit {
fn fmt(&self, fmt: &mut Formatter) -> Result {
fmt.debug_struct(name_of!(type SimpleHit))
.field(name_of!(title in SimpleHit), &self.title)
.field(name_of!(subtitle in SimpleHit), &self.subtitle)
.field(name_of!(override_score in SimpleHit), &self.override_score)
.finish()
}
}

impl Hit for SimpleHit {
fn action(&self, sender: &Sender<FrontendMessage>) {
(self.action_func)(self, sender);
Expand Down
3 changes: 2 additions & 1 deletion gravel-core/src/scoring.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::provider::*;
use crate::Hit;
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use itertools::Itertools;
Expand All @@ -13,6 +13,7 @@ lazy_static! {
static ref MATCHER: SkimMatcherV2 = SkimMatcherV2::default();
}

#[derive(Debug)]
pub struct ScoredHit {
pub hit: Arc<dyn Hit>,
pub score: u32,
Expand Down
1 change: 0 additions & 1 deletion gravel-frontend-fltk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition.workspace = true
gravel-core = { path = "../gravel-core" }

fltk = { workspace = true, features = ["fltk-bundled"] }
lazy_static.workspace = true
log.workspace = true
serde.workspace = true

Expand Down
7 changes: 5 additions & 2 deletions gravel-frontend-fltk/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{config::*, scrollbar::Scrollbar, structs::*};
use fltk::{app, app::Sender, enums::*, frame::Frame, group::Group, input::Input, prelude::*, window::Window};
use crate::config::Config;
use crate::scrollbar::Scrollbar;
use crate::structs::{HitUi, Message, Ui};
use fltk::enums::{Align, Event, FrameType, Key};
use fltk::{app, app::Sender, frame::Frame, group::Group, input::Input, prelude::*, window::Window};

const WINDOW_TITLE: &str = "Gravel";
const WM_CLASS: &str = "gravel";
Expand Down
9 changes: 6 additions & 3 deletions gravel-frontend-fltk/src/implementation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{builder, config::*, native, scroll::Scroll, structs::*};
use fltk::{enums::*, prelude::*};
use gravel_core::{scoring::ScoredHit, *};
use crate::config::Config;
use crate::structs::{HitUi, Message, Ui};
use crate::{builder, native, scroll::Scroll};
use fltk::{enums::FrameType, prelude::*};
use gravel_core::scoring::ScoredHit;
use gravel_core::{Frontend, FrontendExitStatus, FrontendMessage, QueryEngine, QueryResult};
use std::sync::mpsc::Receiver;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

Expand Down
4 changes: 3 additions & 1 deletion gravel-frontend-fltk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! gravel's default frontend, based on fltk.
use gravel_core::{config::PluginConfigAdapter, plugin::*, *};
use gravel_core::config::PluginConfigAdapter;
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{Frontend, QueryEngine};
use implementation::FltkFrontend;

mod builder;
Expand Down
3 changes: 2 additions & 1 deletion gravel-frontend-fltk/src/scrollbar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use fltk::{enums::*, frame::Frame, prelude::*};
use fltk::enums::{Color, FrameType};
use fltk::{frame::Frame, prelude::*};

/// Custom Scrollbar implementation.
///
Expand Down
10 changes: 5 additions & 5 deletions gravel-provider-calculator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
//! Selecting the hit copies the calculated value to the system's clipboard.
use arboard::Clipboard;
use gravel_core::{config::PluginConfigAdapter, plugin::*, scoring::MAX_SCORE, *};
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{config::PluginConfigAdapter, scoring::MAX_SCORE};
use gravel_core::{FrontendMessage, Hit, Provider, ProviderResult, SimpleHit};
use mexprp::Answer;
use serde::Deserialize;
use std::{
cell::OnceCell,
sync::{mpsc::Sender, Arc, Mutex},
};
use std::cell::OnceCell;
use std::sync::{mpsc::Sender, Arc, Mutex};

const DEFAULT_CONFIG: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/config.yml"));

Expand Down
4 changes: 3 additions & 1 deletion gravel-provider-exec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! Always returns a hit with the minimum score that, when selected,
//! runs the command with the system shell.
use gravel_core::{config::PluginConfigAdapter, plugin::*, scoring::MIN_SCORE, *};
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{config::PluginConfigAdapter, scoring::MIN_SCORE};
use gravel_core::{FrontendMessage, Hit, Provider, ProviderResult, SimpleHit};
use serde::Deserialize;
use std::sync::{mpsc::Sender, Arc};

Expand Down
3 changes: 1 addition & 2 deletions gravel-provider-kill/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ gravel-core = { path = "../gravel-core" }
anyhow.workspace = true
itertools.workspace = true
log.workspace = true
serde.workspace = true
thiserror.workspace = true

[target.'cfg(windows)'.dependencies]
sysinfo.workspace = true
winapi.workspace = true
thiserror.workspace = true

[target.'cfg(unix)'.dependencies]
nix = { workspace = true, features = ["process", "signal"] }
Expand Down
6 changes: 4 additions & 2 deletions gravel-provider-kill/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! gravel's process killer
//! Lists running processes on your system and will allow you to kill them.
use gravel_core::{config::PluginConfigAdapter, plugin::*, *};
use gravel_core::config::PluginConfigAdapter;
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{FrontendMessage, Hit, Provider, ProviderResult, SimpleHit};
use implementation::Pid;
use std::sync::{mpsc::Sender, Arc};

Expand All @@ -19,7 +21,7 @@ fn get_provider(_config: &PluginConfigAdapter) -> Box<dyn Provider> {
Box::new(KillProvider {})
}

pub struct KillProvider {}
pub struct KillProvider;

impl Provider for KillProvider {
fn query(&self, _query: &str) -> ProviderResult {
Expand Down
3 changes: 1 addition & 2 deletions gravel-provider-kill/src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use anyhow::Result;
use gravel_core::Hit;
use itertools::Itertools;
use nix::sys::signal::{kill, Signal};
use procfs::process::Process;
use std::sync::Arc;

pub type Pid = i32;

Expand Down
4 changes: 3 additions & 1 deletion gravel-provider-program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ glob.workspace = true
itertools.workspace = true
log.workspace = true
serde.workspace = true
shellexpand.workspace = true

[target.'cfg(unix)'.dependencies]
freedesktop_entry_parser.workspace = true

[target.'cfg(windows)'.dependencies]
shellexpand.workspace = true

[lints]
workspace = true
7 changes: 4 additions & 3 deletions gravel-provider-program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
//!
//! Launches applications using explorer.
use std::{path::PathBuf, sync::Arc};

use glob::{glob, Paths};
use gravel_core::{config::*, plugin::*, *};
use gravel_core::config::PluginConfigAdapter;
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{Hit, Provider, ProviderResult};
use itertools::Itertools;
use serde::Deserialize;
use std::{path::PathBuf, sync::Arc};

#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(windows, path = "windows.rs")]
Expand Down
2 changes: 1 addition & 1 deletion gravel-provider-program/src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Config;
use gravel_core::paths::{get_xdg_data_dirs, get_xdg_data_home};
use gravel_core::*;
use gravel_core::{FrontendMessage, SimpleHit};
use std::iter::once;
use std::path::Path;
use std::process::{Command, Stdio};
Expand Down
2 changes: 1 addition & 1 deletion gravel-provider-program/src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Config;
use gravel_core::*;
use gravel_core::{FrontendMessage, SimpleHit};
use std::borrow::Cow;
use std::path::Path;
use std::process::Command;
Expand Down
4 changes: 3 additions & 1 deletion gravel-provider-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//! Provides system commands such as shutdown, log out or exiting gravel.
use anyhow::Result;
use gravel_core::{config::PluginConfigAdapter, plugin::*, *};
use gravel_core::config::PluginConfigAdapter;
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{FrontendMessage, Hit, Provider, ProviderResult, SimpleHit};
use serde::Deserialize;
use std::{env, sync::Arc};

Expand Down
1 change: 0 additions & 1 deletion gravel-provider-websearch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition.workspace = true
[dependencies]
gravel-core = { path = "../gravel-core" }

anyhow.workspace = true
log.workspace = true
open.workspace = true
serde.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion gravel-provider-websearch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! Always returns a hit with the minimum score that, when selected,
//! opens the user's default browser and searches for the query.
use gravel_core::{config::PluginConfigAdapter, plugin::*, scoring::MIN_SCORE, *};
use gravel_core::plugin::{plugin, PluginRegistry};
use gravel_core::{config::PluginConfigAdapter, scoring::MIN_SCORE};
use gravel_core::{FrontendMessage, Hit, Provider, ProviderResult, SimpleHit};
use serde::Deserialize;
use std::sync::{mpsc::Sender, Arc};

Expand Down
Loading

0 comments on commit 0c58cdf

Please sign in to comment.