Skip to content

Commit

Permalink
refactor: fixed all cargo clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCCF4 committed Jul 3, 2024
1 parent 1ef03d4 commit 495264c
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 88 deletions.
94 changes: 94 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: ci

on:
pull_request:
branches:
- 'main'
push:
branches-ignore:
- 'gh-readonly-queue/**'
workflow_dispatch:
merge_group:
types: [ checks_requested ]

env:
RUSTFLAGS: -Dwarnings

jobs:
build_and_test:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
strategy:
matrix:
os: [ubuntu-latest]
rustalias: [stable, nightly]
feature_flag: ["--all-features", "--no-default-features", ""]
include:
- rustalias: stable
rust: stable
- rustalias: nightly
rust: nightly

name: 'Build and test ${{ matrix.feature_flag }}: ${{ matrix.os }}, ${{ matrix.rustalias }}'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@master

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true

- name: Check
uses: actions-rs/cargo@v1
with:
command: check
args: --all ${{ matrix.feature_flag }} --bins --examples

- name: Tests
uses: actions-rs/cargo@v1
with:
command: test
args: --all ${{ matrix.feature_flag }}

cargo_fmt:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
runs-on: ubuntu-latest
name: 'Cargo fmt'

steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- name: fmt
run: cargo fmt --all -- --check

style_and_docs:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
strategy:
matrix:
feature_flag: ["--all-features", "--no-default-features", ""]
runs-on: ubuntu-latest
name: 'Style and docs ${{ matrix.feature_flag }}'
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: clippy
- name: clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets ${{ matrix.feature_flag }} -- -D warnings
- name: docs
uses: actions-rs/cargo@v1
with:
command: doc
args: --no-deps ${{ matrix.feature_flag }}
6 changes: 3 additions & 3 deletions src/data/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ impl<'a, R: Read> ArchiveEntry<'a, R> {
datetime.second() as u32,
);

if ymd.is_none() || hms.is_none() {
NaiveDateTime::UNIX_EPOCH
if let (Some(date), Some(time)) = (ymd, hms) {
NaiveDateTime::new(date, time)
} else {
NaiveDateTime::new(ymd.unwrap(), hms.unwrap())
NaiveDateTime::UNIX_EPOCH
}
})
.map(|naive_datetime| {
Expand Down
18 changes: 1 addition & 17 deletions src/data/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct PathComponent {
/// let path = FilePath::from_realpath(PathBuf::from("test.txt"));
///
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct FilePath {
/// The path components.
pub path: Vec<PathComponent>,
Expand Down Expand Up @@ -210,22 +210,6 @@ impl FilePath {
}
}

impl PartialEq for FilePath {
/// Compares two file paths.
///
/// # Arguments
/// * `other` - The other file path.
///
/// # Returns
/// Whether the file paths are equal.
fn eq(&self, other: &Self) -> bool {
self.path.len() == other.path.len()
&& self.path.iter().zip(other.path.iter()).all(|(a, b)| a == b)
}
}

impl Eq for FilePath {}

impl std::fmt::Display for FilePath {
/// Formats the file path to a string.
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn main() {
trace!("Initializing program");

if let Some(threads) = args.threads {
if threads <= 0 {
if threads == 0 {
eprintln!("Invalid number of threads: {}", threads);
std::process::exit(exitcode::CONFIG);
}
Expand Down Expand Up @@ -229,7 +229,7 @@ fn main() {
info!("Executing clean command");
match clean::cmd::run(CleanSettings {
input: output.clone(),
output: output,
output,
root: None,
follow_symlinks,
}) {
Expand Down
8 changes: 5 additions & 3 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ impl<Job: Send + JobTrait + 'static, Result: Send + ResultTrait + 'static> Threa
None => {
error!("ThreadPool is shutting down. Cannot publish job.");
}
Some(job_publish) => if let Err(e) = job_publish.send(job) {
error!("Failed to publish job on thread pool. {}", e);
},
Some(job_publish) => {
if let Err(e) = job_publish.send(job) {
error!("Failed to publish job on thread pool. {}", e);
}
}
},
}
}
Expand Down
1 change: 1 addition & 0 deletions src/stages/analyze/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl ResultTrait for AnalysisResult {}
/// # Returns
/// The parent file and the parent path.
/// If the parent file is not present, return None.
#[allow(clippy::type_complexity)] // non-public function
fn parent_file<'a>(
file: &AnalysisIntermediaryFile,
arg: &'a AnalysisWorkerArgument,
Expand Down
1 change: 1 addition & 0 deletions src/stages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod output {

/// Contains the cli command implementation for the build command.
pub mod cmd {
#[allow(clippy::module_inception)] // private module
mod cmd;
/// Contains the job definition for the build command.
pub mod job;
Expand Down
26 changes: 14 additions & 12 deletions src/stages/build/cmd/worker.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::hash::GeneralHashType;
use crate::path::FilePath;
use crate::stages::build::cmd::job::{BuildJob, JobResult, JobResultContent};
use crate::stages::build::cmd::worker::directory::worker_run_directory;
use crate::stages::build::cmd::worker::file::worker_run_file;
use crate::stages::build::cmd::worker::other::worker_run_other;
use crate::stages::build::cmd::worker::symlink::worker_run_symlink;
use crate::stages::build::cmd::worker::directory::{
worker_run_directory, WorkerRunDirectoryArguments,
};
use crate::stages::build::cmd::worker::file::{worker_run_file, WorkerRunFileArguments};
use crate::stages::build::cmd::worker::other::{worker_run_other, WorkerRunOtherArguments};
use crate::stages::build::cmd::worker::symlink::{worker_run_symlink, WorkerRunSymlinkArguments};
use crate::stages::build::intermediary_build_data::{
BuildFile, BuildOtherInformation, BuildStubInformation,
};
Expand Down Expand Up @@ -123,7 +125,7 @@ pub fn worker_run(
let size = metadata.len();

if metadata.is_symlink() {
worker_run_symlink(
worker_run_symlink(WorkerRunSymlinkArguments {
path,
modified,
size,
Expand All @@ -132,9 +134,9 @@ pub fn worker_run(
result_publish,
job_publish,
arg,
);
});
} else if metadata.is_dir() {
worker_run_directory(
worker_run_directory(WorkerRunDirectoryArguments {
path,
modified,
size,
Expand All @@ -143,9 +145,9 @@ pub fn worker_run(
result_publish,
job_publish,
arg,
);
});
} else if metadata.is_file() {
worker_run_file(
worker_run_file(WorkerRunFileArguments {
path,
modified,
size,
Expand All @@ -154,9 +156,9 @@ pub fn worker_run(
result_publish,
job_publish,
arg,
);
});
} else {
worker_run_other(
worker_run_other(WorkerRunOtherArguments {
path,
modified,
size,
Expand All @@ -165,7 +167,7 @@ pub fn worker_run(
result_publish,
job_publish,
arg,
);
});
}
}

Expand Down
12 changes: 3 additions & 9 deletions src/stages/build/cmd/worker/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ pub fn worker_run_archive<R: Read>(
.open(input)
.map_err(|err| anyhow!("Failed to open archive: {}", err))?;

let context = Context {
#[allow(dead_code)]
let _context = Context {
id,
path: FilePath::from_realpath(path).new_archive(),
};

let mut entries = Vec::new();

// todo remove placeholder
entries.push(BuildFile::Other(BuildOtherInformation {
path: context.path.child("test-placeholder.txt"),
modified: 0,
content_size: 0,
}));
let entries = Vec::new();

/* for entry in archive {
let entry = entry.map_err(|err| {
Expand Down
52 changes: 42 additions & 10 deletions src/stages/build/cmd/worker/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ use std::path::PathBuf;
use std::sync::mpsc::Sender;
use std::sync::Arc;

/// Arguments of the [worker_run_directory] function.
///
/// # Fields
/// * `path` - The path to the directory.
/// * `modified` - The last modified time of the directory.
/// * `size` - The size of the directory (given by fs::metadata).
/// * `id` - The id of the worker.
/// * `job` - The job to process.
/// * `result_publish` - The channel to publish the result to.
/// * `job_publish` - The channel to publish new jobs to.
/// * `arg` - The argument for the worker thread.
pub struct WorkerRunDirectoryArguments<'a, 'b, 'c> {
/// The path to the directory.
pub path: PathBuf,
/// The last modified time of the directory.
pub modified: u64,
/// The size of the directory (given by fs::metadata).
pub size: u64,
/// The id of the worker.
pub id: usize,
/// The job to process.
pub job: BuildJob,
/// The channel to publish the result to.
pub result_publish: &'a Sender<JobResult>,
/// The channel to publish new jobs to.
pub job_publish: &'b Sender<BuildJob>,
/// The argument for the worker thread.
pub arg: &'c mut WorkerArgument,
}

/// Analyze a directory.
///
/// # Arguments
Expand All @@ -25,16 +55,18 @@ use std::sync::Arc;
/// * `result_publish` - The channel to publish the result to.
/// * `job_publish` - The channel to publish new jobs to.
/// * `arg` - The argument for the worker thread.
pub fn worker_run_directory(
path: PathBuf,
modified: u64,
size: u64,
id: usize,
mut job: BuildJob,
result_publish: &Sender<JobResult>,
job_publish: &Sender<BuildJob>,
arg: &mut WorkerArgument,
) {
pub fn worker_run_directory(arguments: WorkerRunDirectoryArguments) {
let WorkerRunDirectoryArguments {
path,
modified,
size,
id,
mut job,
result_publish,
job_publish,
arg,
} = arguments;

trace!(
"[{}] analyzing directory {} > {:?}",
id,
Expand Down
Loading

0 comments on commit 495264c

Please sign in to comment.