diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index f9301a6d..1b27a4e7 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -1,9 +1,9 @@ name: Development Checks -on: [pull_request] +on: [push,pull_request] jobs: - format: + run: runs-on: ubuntu-latest env: CARGO_TERM_COLOR: always @@ -11,10 +11,30 @@ jobs: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@nightly with: - components: rustfmt + components: rustfmt, clippy - uses: Swatinem/rust-cache@v2 - - name: Verify Formatting + - name: Format uses: ClementTsang/cargo-action@main with: command: fmt - args: --all --check \ No newline at end of file + args: --all --check + - name: Check + uses: ClementTsang/cargo-action@main + with: + command: check + args: --all-targets + - name: Clippy + uses: ClementTsang/cargo-action@main + with: + command: clippy + args: --all-targets + - name: Build + uses: ClementTsang/cargo-action@main + with: + command: build + args: --all-targets + - name: Test + uses: ClementTsang/cargo-action@main + with: + command: test + args: --all-targets \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..f1027e9b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[rust]": { + "editor.formatOnSave": true + }, + "rust-analyzer.checkOnSave.command": "clippy", +} \ No newline at end of file diff --git a/src/auth.rs b/src/auth.rs index 2b38c14e..57ec50e5 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -29,9 +29,7 @@ impl AuthorizationService { let claims = UserClaims { user, exp: exp_date }; - let token = encode(&Header::default(), &claims, &EncodingKey::from_secret(key)).unwrap(); - - token + encode(&Header::default(), &claims, &EncodingKey::from_secret(key)).unwrap() } pub async fn verify_jwt(&self, token: &str) -> Result { diff --git a/src/config.rs b/src/config.rs index 00d390dc..110d7fb2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -135,9 +135,9 @@ impl Configuration { eprintln!("Creating config file.."); let config = Configuration::default(); let _ = config.save_to_file().await; - return Err(ConfigError::Message(format!( - "Please edit the config.TOML in the root folder and restart the tracker." - ))); + return Err(ConfigError::Message( + "Please edit the config.TOML in the root folder and restart the tracker.".to_string(), + )); } let torrust_config: TorrustConfig = match config.try_into() { diff --git a/src/databases/database.rs b/src/databases/database.rs index c4dad043..0f06f702 100644 --- a/src/databases/database.rs +++ b/src/databases/database.rs @@ -11,7 +11,7 @@ use crate::models::tracker_key::TrackerKey; use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile}; /// Database drivers. -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub enum DatabaseDriver { Sqlite3, Mysql, diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index 75afe1c8..ea228e3c 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -323,7 +323,7 @@ impl Database for MysqlDatabase { i += 1; } } - if category_filters.len() > 0 { + if !category_filters.is_empty() { format!( "INNER JOIN torrust_categories tc ON tt.category_id = tc.category_id AND ({}) ", category_filters @@ -563,7 +563,7 @@ impl Database for MysqlDatabase { let torrent_files: Vec = db_torrent_files .into_iter() .map(|tf| TorrentFile { - path: tf.path.unwrap_or("".to_string()).split('/').map(|v| v.to_string()).collect(), + path: tf.path.unwrap_or_default().split('/').map(|v| v.to_string()).collect(), length: tf.length, md5sum: tf.md5sum, }) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index 44e297a2..7b39a52f 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -318,7 +318,7 @@ impl Database for SqliteDatabase { i += 1; } } - if category_filters.len() > 0 { + if !category_filters.is_empty() { format!( "INNER JOIN torrust_categories tc ON tt.category_id = tc.category_id AND ({}) ", category_filters @@ -558,7 +558,7 @@ impl Database for SqliteDatabase { let torrent_files: Vec = db_torrent_files .into_iter() .map(|tf| TorrentFile { - path: tf.path.unwrap_or("".to_string()).split('/').map(|v| v.to_string()).collect(), + path: tf.path.unwrap_or_default().split('/').map(|v| v.to_string()).collect(), length: tf.length, md5sum: tf.md5sum, }) diff --git a/src/errors.rs b/src/errors.rs index 6f5ee0a2..24a413e6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -10,7 +10,7 @@ use crate::databases::database::DatabaseError; pub type ServiceResult = Result; -#[derive(Debug, Display, PartialEq, Error)] +#[derive(Debug, Display, PartialEq, Eq, Error)] #[allow(dead_code)] pub enum ServiceError { #[display(fmt = "internal server error")] @@ -182,7 +182,6 @@ impl ResponseError for ServiceError { HttpResponseBuilder::new(self.status_code()) .append_header((header::CONTENT_TYPE, "application/json; charset=UTF-8")) .body(serde_json::to_string(&ErrorToResponse { error: self.to_string() }).unwrap()) - .into() } } diff --git a/src/mailer.rs b/src/mailer.rs index 94dd8325..a8fd4de8 100644 --- a/src/mailer.rs +++ b/src/mailer.rs @@ -126,7 +126,7 @@ impl MailerService { let token = encode(&Header::default(), &claims, &EncodingKey::from_secret(key)).unwrap(); - let mut base_url = base_url.clone(); + let mut base_url = &base_url.to_string(); if let Some(cfg_base_url) = &settings.net.base_url { base_url = cfg_base_url; } diff --git a/src/models/torrent.rs b/src/models/torrent.rs index 4adab162..9063c7f3 100644 --- a/src/models/torrent.rs +++ b/src/models/torrent.rs @@ -4,7 +4,7 @@ use crate::models::torrent_file::Torrent; use crate::routes::torrent::CreateTorrent; #[allow(dead_code)] -#[derive(Debug, PartialEq, Serialize, Deserialize, sqlx::FromRow)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)] pub struct TorrentListing { pub torrent_id: i64, pub uploader: String, diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 98cd4c00..581d73a8 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -6,10 +6,10 @@ use sha1::{Digest, Sha1}; use crate::config::Configuration; use crate::utils::hex::{bytes_to_hex, hex_to_bytes}; -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct TorrentNode(String, i64); -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct TorrentFile { pub path: Vec, pub length: i64, @@ -17,7 +17,7 @@ pub struct TorrentFile { pub md5sum: Option, } -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct TorrentInfo { pub name: String, #[serde(default)] @@ -160,7 +160,7 @@ impl Torrent { pub fn file_size(&self) -> i64 { if self.info.length.is_some() { - return self.info.length.unwrap(); + self.info.length.unwrap() } else { match &self.info.files { None => 0, @@ -176,7 +176,7 @@ impl Torrent { } } -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] pub struct DbTorrentFile { pub path: Option, pub length: i64, @@ -184,7 +184,7 @@ pub struct DbTorrentFile { pub md5sum: Option, } -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] pub struct DbTorrentInfo { pub name: String, pub pieces: String, @@ -194,7 +194,7 @@ pub struct DbTorrentInfo { pub root_hash: i64, } -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] pub struct DbTorrentAnnounceUrl { pub tracker_url: String, } diff --git a/src/models/user.rs b/src/models/user.rs index 53d0a4e0..f64b88b4 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -13,7 +13,7 @@ pub struct UserAuthentication { pub password_hash: String, } -#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, sqlx::FromRow)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, sqlx::FromRow)] pub struct UserProfile { pub user_id: i64, pub username: String, diff --git a/src/routes/category.rs b/src/routes/category.rs index 8bcd0348..defca8a8 100644 --- a/src/routes/category.rs +++ b/src/routes/category.rs @@ -57,7 +57,7 @@ pub async fn delete_category( return Err(ServiceError::Unauthorized); } - let _ = app_data.database.delete_category(&payload.name).await?; + app_data.database.delete_category(&payload.name).await?; Ok(HttpResponse::Ok().json(OkResponse { data: payload.name.clone(), diff --git a/src/routes/torrent.rs b/src/routes/torrent.rs index 5d87e8b2..41074b4b 100644 --- a/src/routes/torrent.rs +++ b/src/routes/torrent.rs @@ -250,12 +250,12 @@ pub async fn update_torrent( // update torrent title if let Some(title) = &payload.title { - let _res = app_data.database.update_torrent_title(torrent_id, title).await?; + app_data.database.update_torrent_title(torrent_id, title).await?; } // update torrent description if let Some(description) = &payload.description { - let _res = app_data.database.update_torrent_description(torrent_id, description).await?; + app_data.database.update_torrent_description(torrent_id, description).await?; } let torrent_listing = app_data.database.get_torrent_listing_from_id(torrent_id).await?; @@ -278,7 +278,7 @@ pub async fn delete_torrent(req: HttpRequest, app_data: WebAppData) -> ServiceRe // needed later for removing torrent from tracker whitelist let torrent_listing = app_data.database.get_torrent_listing_from_id(torrent_id).await?; - let _res = app_data.database.delete_torrent(torrent_id).await?; + app_data.database.delete_torrent(torrent_id).await?; // remove info_hash from tracker whitelist let _ = app_data @@ -344,7 +344,7 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result title = parsed_data.to_string(), diff --git a/src/routes/user.rs b/src/routes/user.rs index ed6b1564..6b535bc6 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -179,7 +179,7 @@ pub async fn verify_token(payload: web::Json, app_data: WebAppData) -> Se let _claims = app_data.auth.verify_jwt(&payload.token).await?; Ok(HttpResponse::Ok().json(OkResponse { - data: format!("Token is valid."), + data: "Token is valid.".to_string(), })) } @@ -256,7 +256,7 @@ pub async fn ban_user(req: HttpRequest, app_data: WebAppData) -> ServiceResult) -> Result) { - let add_test_user_result = add_test_user(&db).await; + let add_test_user_result = add_test_user(db).await; assert!(add_test_user_result.is_ok()); @@ -49,7 +49,7 @@ pub async fn it_can_add_a_user(db: &Box) { user_id: inserted_user_id, username: TEST_USER_USERNAME.to_string(), email: TEST_USER_EMAIL.to_string(), - email_verified: returned_user_profile.email_verified.clone(), + email_verified: returned_user_profile.email_verified, bio: returned_user_profile.bio.clone(), avatar: returned_user_profile.avatar.clone() } @@ -57,7 +57,7 @@ pub async fn it_can_add_a_user(db: &Box) { } pub async fn it_can_add_a_torrent_category(db: &Box) { - let add_test_torrent_category_result = add_test_torrent_category(&db).await; + let add_test_torrent_category_result = add_test_torrent_category(db).await; assert!(add_test_torrent_category_result.is_ok()); @@ -72,8 +72,8 @@ pub async fn it_can_add_a_torrent_category(db: &Box) { pub async fn it_can_add_a_torrent_and_tracker_stats_to_that_torrent(db: &Box) { // set pre-conditions - let user_id = add_test_user(&db).await.expect("add_test_user failed."); - let torrent_category_id = add_test_torrent_category(&db) + let user_id = add_test_user(db).await.expect("add_test_user failed."); + let torrent_category_id = add_test_torrent_category(db) .await .expect("add_test_torrent_category failed.");