Skip to content

Commit

Permalink
Merge pull request #167 from yijunyu/master
Browse files Browse the repository at this point in the history
Add new Ratio output type --output-format=Ratio
  • Loading branch information
anderejd authored Dec 25, 2020
2 parents b1f9a97 + 697ceeb commit 65cd410
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cargo-geiger/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ OPTIONS:
--format <FORMAT> Format string used for printing dependencies
[default: {p}].
--output-format Output format for the report: Ascii, GitHubMarkdown,
Json, Utf8 [default: Utf8]
Json, Utf8, Ratio [default: Utf8]
--update-readme Writes output to ./README.md. Looks for a Safety
Report section, replaces if found, adds if not.
Throws an error if no README.md exists.
Expand Down
1 change: 1 addition & 0 deletions cargo-geiger/src/format/print_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum OutputFormat {
Ascii,
Json,
GitHubMarkdown,
Ratio,
Utf8,
}

Expand Down
115 changes: 106 additions & 9 deletions cargo-geiger/src/format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub struct TableParameters<'a> {
pub rs_files_used: &'a HashSet<PathBuf>,
}

fn table_footer(
fn table_footer_unsafe_counts(
used: CounterBlock,
not_used: CounterBlock,
output_format: OutputFormat,
Expand All @@ -121,18 +121,110 @@ fn table_footer(
colorize(&status, output_format, output)
}

fn table_row(used: &CounterBlock, not_used: &CounterBlock) -> String {
fn table_footer_safe_ratio(
used: CounterBlock,
not_used: CounterBlock,
output_format: OutputFormat,
status: CrateDetectionStatus,
) -> ColoredString {
let fmt = |used: &Count, not_used: &Count| {
format!("{}/{}", used.unsafe_, used.unsafe_ + not_used.unsafe_)
format!(
"{:>5}/{:<}={:.2}%",
(used.safe + not_used.safe),
(used.safe + used.unsafe_ + not_used.unsafe_ + not_used.safe),
if used.safe + used.unsafe_ + not_used.unsafe_ + not_used.safe == 0
{
100.0
} else {
(100.00 * (used.safe + not_used.safe) as f32)
/ ((used.safe
+ used.unsafe_
+ not_used.unsafe_
+ not_used.safe) as f32)
}
)
};
format!(
"{: <10} {: <12} {: <6} {: <7} {: <7}",
let output = format!(
"{: <12} {: <18} {: <18} {: <12} {: <12}",
fmt(&used.functions, &not_used.functions),
fmt(&used.exprs, &not_used.exprs),
fmt(&used.item_impls, &not_used.item_impls),
fmt(&used.item_traits, &not_used.item_traits),
fmt(&used.methods, &not_used.methods),
)
);
colorize(&status, output_format, output)
}

fn table_footer(
used: CounterBlock,
not_used: CounterBlock,
output_format: OutputFormat,
status: CrateDetectionStatus,
) -> ColoredString {
match output_format {
OutputFormat::Ratio => {
table_footer_safe_ratio(used, not_used, output_format, status)
}
_ => table_footer_unsafe_counts(used, not_used, output_format, status),
}
}

fn table_row(
used: &CounterBlock,
not_used: &CounterBlock,
output_format: OutputFormat,
) -> String {
match output_format {
OutputFormat::Ratio => {
// print safe ratio
let fmt = |used: &Count, not_used: &Count| {
format!(
"{:>5}/{:<}={:.2}%",
(used.safe + not_used.safe),
(used.safe
+ used.unsafe_
+ not_used.unsafe_
+ not_used.safe),
if used.safe
+ used.unsafe_
+ not_used.unsafe_
+ not_used.safe
== 0
{
100.0
} else {
(100.00 * (used.safe + not_used.safe) as f32)
/ ((used.safe
+ used.unsafe_
+ not_used.unsafe_
+ not_used.safe)
as f32)
}
)
};
format!(
"{: <12} {: <18} {: <18} {: <12} {: <12}",
fmt(&used.functions, &not_used.functions),
fmt(&used.exprs, &not_used.exprs),
fmt(&used.item_impls, &not_used.item_impls),
fmt(&used.item_traits, &not_used.item_traits),
fmt(&used.methods, &not_used.methods)
)
}
_ => {
let fmt = |used: &Count, not_used: &Count| {
format!("{}/{}", used.unsafe_, used.unsafe_ + not_used.unsafe_)
};
format!(
"{: <10} {: <12} {: <6} {: <7} {: <7}",
fmt(&used.functions, &not_used.functions),
fmt(&used.exprs, &not_used.exprs),
fmt(&used.item_impls, &not_used.item_impls),
fmt(&used.item_traits, &not_used.item_traits),
fmt(&used.methods, &not_used.methods)
)
}
}
}

fn table_row_empty() -> String {
Expand All @@ -142,7 +234,7 @@ fn table_row_empty() -> String {
.iter()
.map(|s| s.len())
.sum::<usize>()
+ headers_but_last.len() // Space after each column
+ headers_but_last.len() + 4// Space after each column
+ 2 // Unsafety symbol width
+ 1; // Space after symbol
" ".repeat(n)
Expand Down Expand Up @@ -171,6 +263,10 @@ mod table_tests {
OutputFormat::GitHubMarkdown,
String::from("2/4 4/8 6/12 8/16 10/20 ")
),
case(
OutputFormat::Ratio,
String::from(" 2/6=33.33% 6/14=42.86% 10/22=45.45% 14/30=46.67% 18/38=47.37%")
),
case(
OutputFormat::Utf8,
String::from("2/4 4/8 6/12 8/16 10/20 ")
Expand Down Expand Up @@ -232,14 +328,15 @@ mod table_tests {
.collect();
let unsafety = unsafe_stats(&package_metrics, &rs_files_used);

let table_row = table_row(&unsafety.used, &unsafety.unused);
let table_row =
table_row(&unsafety.used, &unsafety.unused, OutputFormat::Ascii);
assert_eq!(table_row, "4/6 8/12 12/18 16/24 20/30 ");
}

#[rstest]
fn table_row_empty_test() {
let empty_table_row = table_row_empty();
assert_eq!(empty_table_row.len(), 51);
assert_eq!(empty_table_row.len(), 55);
}

#[rstest(
Expand Down
6 changes: 5 additions & 1 deletion cargo-geiger/src/format/table/handle_text_tree_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ pub fn handle_text_tree_line_package(
let unsafe_info = colorize(
&crate_detection_status,
table_parameters.print_config.output_format,
table_row(&unsafe_info.used, &unsafe_info.unused),
table_row(
&unsafe_info.used,
&unsafe_info.unused,
table_parameters.print_config.output_format,
),
);

let shift_chars = unsafe_info.chars().count() + 4;
Expand Down
26 changes: 21 additions & 5 deletions cargo-geiger/src/scan/default/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,27 @@ fn construct_key_lines(
let mut output_key_lines = Vec::<String>::new();

output_key_lines.push(String::new());
output_key_lines.push(String::from("Metric output format: x/y"));
output_key_lines
.push(String::from(" x = unsafe code used by the build"));
output_key_lines
.push(String::from(" y = total unsafe code found in the crate"));
match output_format {
OutputFormat::Ratio => {
// Change the prompt for Safe Ratio report:
output_key_lines.push(String::from("Metric output format: x/y=z%"));
output_key_lines
.push(String::from(" x = safe code found in the crate"));
output_key_lines
.push(String::from(" y = total code found in the crate"));
output_key_lines.push(String::from(
" z = percentage of safe ratio as defined by x/y",
));
}
_ => {
output_key_lines.push(String::from("Metric output format: x/y"));
output_key_lines
.push(String::from(" x = unsafe code used by the build"));
output_key_lines.push(String::from(
" y = total unsafe code found in the crate",
));
}
}
output_key_lines.push(String::new());
output_key_lines.push(String::from("Symbols: "));

Expand Down

0 comments on commit 65cd410

Please sign in to comment.