Skip to content

[Enhancement] New Option for Help, Initial Module, and Tests for the Source Code Documentation #6

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

Merged
merged 31 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4e045cb
Fix typo: `analzer` instead of `analyzer`
kevinmatthes May 19, 2022
1c12084
Create usage.rs
kevinmatthes May 19, 2022
eed1607
Load function from module
kevinmatthes May 19, 2022
f580e79
Fix typo: wrong parameter name
kevinmatthes May 19, 2022
8db2f26
Make the functon `return` at the end explicitly
kevinmatthes May 19, 2022
0ad091b
Add missing full stop
kevinmatthes May 19, 2022
f6cf184
Add option for help
kevinmatthes May 19, 2022
66d03eb
Remove obsolete visibility level
kevinmatthes May 19, 2022
962bf78
Load required module appropriately
kevinmatthes May 19, 2022
c9ed9e7
Hard code application name
kevinmatthes May 19, 2022
36a3e8c
Adjust function call due to previous commit
kevinmatthes May 19, 2022
b6cc104
Remove obsolete parameter
kevinmatthes May 19, 2022
7063416
Unite control flow branches
kevinmatthes May 19, 2022
5600974
Add missing space character
kevinmatthes May 19, 2022
383b3c2
Document function
kevinmatthes May 19, 2022
0e6aa27
Document source file
kevinmatthes May 19, 2022
df07842
Add missing parentheses
kevinmatthes May 19, 2022
3cf8f98
Add new ignore pattern
kevinmatthes May 19, 2022
a761615
Sort entries by alphabet
kevinmatthes May 19, 2022
4b71ef4
Optimise code with `cargo fmt`
kevinmatthes May 19, 2022
8d3226d
Apply linter suggestions
kevinmatthes May 19, 2022
84126f2
Add accidentally forgotten newline whitespace
kevinmatthes May 20, 2022
bf4adf5
Adjust function documentation
kevinmatthes May 20, 2022
6899b5f
Shorten the import of project related module
kevinmatthes May 20, 2022
ee1cdfa
Revert hexadecimal representation
kevinmatthes May 20, 2022
04d2396
Remove obsolete semicolon
kevinmatthes May 20, 2022
fb72242
Fix linter warnings with `cargo clippy --fix`
kevinmatthes May 20, 2022
50ce612
Move module import
kevinmatthes May 20, 2022
575ece7
Move function
kevinmatthes May 20, 2022
750505d
Delete usage.rs
kevinmatthes May 20, 2022
bcedcee
Reformat code
kevinmatthes May 20, 2022
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
*.txt
*.csv
*.log
*.txt
52 changes: 30 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use getopts::Options;
use std::{collections::HashMap, error::Error, io::Write, num::ParseIntError, ops::AddAssign};

fn main() -> Result<(), Box<dyn Error>> {
let mut opts = getopts::Options::new();
let opts = opts
.optflag("q", "quiet", "Hides the output of commit messages")
.optflag("q", "quiet", "Hides the output of commit messages.")
.optflag("h", "help", "Show this help and exit.")
.optmulti(
"a",
"author-contains",
Expand Down Expand Up @@ -79,12 +81,12 @@ fn main() -> Result<(), Box<dyn Error>> {
let matches = match opts.parse(std::env::args()) {
Ok(it) => it,
Err(err) => {
usage("commit-analzer", opts);
usage(opts);
return Err(err.into());
}
};
if matches.free.len() < 2 {
usage(&matches.free[0], opts);
if matches.opt_present("h") || matches.free.len() < 2 {
usage(opts);
return Ok(());
}
let is_quiet = matches.opt_present("q");
Expand Down Expand Up @@ -176,9 +178,15 @@ fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn usage(program_name: &str, opts: &getopts::Options) {
println!("Usage: {} <FILE> [OPTIONS]\n", program_name);
println!("{}", opts.usage("Parses the output of `git log`."));
/// A small in-app documentation.
///
/// This function will write a brief usage information, including a short
/// introduction to the meaning of the configured `options`, to `stdout`.
fn usage(options: &Options) {
println!(
"Usage: commit-analyzer <FILE> [OPTIONS]\n\n{}",
options.usage("Parses the output of `git log`.")
);
}

fn matches_filter(commit: &Commit, filter: &Filter) -> bool {
Expand Down Expand Up @@ -239,7 +247,11 @@ impl Filter {
}

fn check_loc(&self, loc: &&Loc) -> bool {
self.file_extension.is_empty() || self.file_extension.iter().any(|ext| loc.file.ends_with(&format!(".{}", ext)))
self.file_extension.is_empty()
|| self
.file_extension
.iter()
.any(|ext| loc.file.ends_with(&format!(".{}", ext)))
}
}

Expand Down Expand Up @@ -320,7 +332,7 @@ fn parse_commit(commit: &str) -> Result<(Commit, &str), CommitParseError> {
if loc.is_empty() || loc.starts_with("commit") {
break;
}
locs.push(Loc::parse(loc).map_err(|err| CommitParseError::LocFailed(err))?);
locs.push(Loc::parse(loc).map_err(CommitParseError::LocFailed)?);
remainder_result = remainder;
if let Some(remainder) = remainder_result.strip_prefix('\n') {
remainder_result = remainder;
Expand All @@ -331,9 +343,9 @@ fn parse_commit(commit: &str) -> Result<(Commit, &str), CommitParseError> {
let commit = Commit {
commit: commit.into(),
merge,
author: Author::parse(author).map_err(|err| CommitParseError::AuthorFailed(err))?,
author: Author::parse(author).map_err(CommitParseError::AuthorFailed)?,
date: chrono::DateTime::parse_from_str(date, "%a %b %e %T %Y %z")
.map_err(|err| CommitParseError::DateFailed(err))?,
.map_err(CommitParseError::DateFailed)?,
message: message.into(),
locs,
};
Expand All @@ -352,7 +364,11 @@ pub struct Commit {

impl Commit {
pub fn loc(&self, filter: &Filter) -> i64 {
self.locs.iter().filter(|l| filter.check_loc(l)).map(|l| l.loc()).sum()
self.locs
.iter()
.filter(|l| filter.check_loc(l))
.map(|l| l.loc())
.sum()
}
}

Expand Down Expand Up @@ -407,20 +423,12 @@ impl Loc {
let added = if added == "-" {
None
} else {
Some(
added
.parse()
.map_err(|err| LocParseError::AddedParseError(err))?,
)
Some(added.parse().map_err(LocParseError::AddedParseError)?)
};
let removed = if removed == "-" {
None
} else {
Some(
removed
.parse()
.map_err(|err| LocParseError::RemovedParseError(err))?,
)
Some(removed.parse().map_err(LocParseError::RemovedParseError)?)
};
let file = file.into();
Ok(Self {
Expand Down