Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clippy to CI and fix all clippy warnings #10

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Build
run: cargo build --verbose

- name: Run tests
run: cargo test --verbose

- name: Run clippy
run: cargo clippy --all-targets --all-features
env:
RUSTFLAGS: "-Dwarnings"
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
utoipa = "4"
utoipa-swagger-ui = { version = "7", features = ["actix-web"] }

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
enum_glob_use = "deny"
uninlined-format-args = "warn"
needless-pass-by-value = "warn"
wildcard-imports = "warn"
2 changes: 1 addition & 1 deletion src/api_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a> LookupResult<'a> {
.schema_type(utoipa::openapi::SchemaType::Object)
.title(Some("AnonymousIpLookupResult"));

const BOOLEAN_FIELDS: &'static [&'static str] = &[
const BOOLEAN_FIELDS: &[&str] = &[
"is_anonymous",
"is_anonymous_vpn",
"is_hosting_provider",
Expand Down
4 changes: 2 additions & 2 deletions src/db_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub async fn start_db_update_daemon(data: web::Data<impl UpdatableDB + 'static>,
let duration = match data.update_db(interval).await {
Ok(_) => success_update_sleep,
Err(error) => {
println!("Failed to update database {:?}", error);
println!("Failed to update database {error:?}");
failure_update_sleep
}
};

println!("Updater sleeping for {:?}", duration);
println!("Updater sleeping for {duration:?}");
sleep(duration).await;
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/download_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct AlreadyDownloaded;

impl fmt::Display for AlreadyDownloaded {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ pub async fn extract_db(path: &str, filename: &str) -> Result<String, Box<dyn Er
let output = command.arg("*.mmdb").output().await?;

if !output.status.success() {
println!("{:?}", output);
println!("{output:?}");
return Err("failed to extract archive".into());
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn init_db(
}

pub async fn start_db_refresher(maxmind_db_arc: web::Data<MaxmindDB>, update_interval: u64) {
db_refresher::start_db_update_daemon(maxmind_db_arc.clone(), update_interval).await;
db_refresher::start_db_update_daemon(maxmind_db_arc, update_interval).await;
}

pub async fn start_server(
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::{Error, ErrorKind, Result};
use atlas_rs::api_docs;
use tokio::io::AsyncWriteExt;

const SPEC_FILENAME: &'static str = "openapi-spec.json";
const SPEC_FILENAME: &str = "openapi-spec.json";

#[actix_web::main]
async fn main() -> Result<()> {
Expand All @@ -27,7 +27,7 @@ async fn main() -> Result<()> {
.parse()
.expect("Invalid SWAGGER_UI_ENABLED value. Expected `false` or `true`");

let subcommand = env::args().skip(1).next();
let subcommand = env::args().nth(1);

match subcommand.as_deref() {
Some("server") | None => {
Expand Down Expand Up @@ -71,7 +71,7 @@ async fn main() -> Result<()> {
}
Some(command) => Err(Error::new(
ErrorKind::InvalidInput,
format!("Invalid command: {}", command),
format!("Invalid command: {command}"),
)),
}
}
13 changes: 8 additions & 5 deletions src/maxmind_db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{db_refresher::UpdatableDB, download_utils::*};
use crate::{
db_refresher::UpdatableDB,
download_utils::{download_with_basic_auth, extract_db, AlreadyDownloaded},
};
use maxminddb::{MaxMindDBError, Reader};
use serde::Deserialize;
use std::{
Expand Down Expand Up @@ -30,15 +33,15 @@ pub struct MaxmindDBInner {

impl<'de> MaxmindDB {
pub async fn init(variant: &str, base_path: &str) -> Result<Self, Box<dyn Error>> {
let db_path = match Self::get_latest_variant(&variant, &base_path).await? {
let db_path = match Self::get_latest_variant(variant, base_path).await? {
Some(db) => db,
None => {
println!("No database found! Fetching latest from upstream...");
Self::fetch_latest_db(&variant, &base_path).await?
Self::fetch_latest_db(variant, base_path).await?
}
};

let inner_db = MaxmindDBInner::load(&db_path, &variant)?;
let inner_db = MaxmindDBInner::load(db_path, variant)?;

Ok(Self {
db: RwLock::new(inner_db),
Expand Down Expand Up @@ -144,7 +147,7 @@ impl<'de> MaxmindDBInner {
let filename = path.file_name().unwrap().to_str().unwrap().to_string();
let full_path = path.to_str().unwrap().to_string();

println!("Loading database from {}", full_path);
println!("Loading database from {full_path}");
let reader = Reader::open_readfile(&full_path)?;

Ok(Self {
Expand Down
5 changes: 2 additions & 3 deletions src/services/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn handle(data: web::Data<MaxmindDB>, path: web::Path<(String, String)>) -
.map(|ip_address| {
ip_address.parse().map_err(|_| {
bad_request(
format!("Invalid IP Address {:?}", ip_address),
format!("Invalid IP Address {ip_address:?}"),
"INVALID_IP".to_string(),
)
})
Expand All @@ -81,8 +81,7 @@ async fn handle(data: web::Data<MaxmindDB>, path: web::Path<(String, String)>) -
if ip.is_special_ip() {
Err(bad_request(
format!(
"IP Address is part of a special list and not allowed: {}",
ip
"IP Address is part of a special list and not allowed: {ip}"
),
"SPECIAL_IP".to_string(),
))
Expand Down
2 changes: 1 addition & 1 deletion tests/lookup_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn test_rejects_too_many_ips() {
let (service, _app_data) = setup().await;

let ip_addresses = (1..=51)
.map(|i| format!("1.1.1.{}", i))
.map(|i| format!("1.1.1.{i}"))
.collect::<Vec<_>>()
.join(",");

Expand Down