Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Created using spr 1.3.6-beta.1
  • Loading branch information
sunshowers committed Jul 5, 2024
1 parent 50cf800 commit 68fdbb1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 76 deletions.
42 changes: 23 additions & 19 deletions dev-tools/openapi-manager/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::process::ExitCode;
use std::{io::Write, process::ExitCode};

use anyhow::Result;
use camino::Utf8Path;
Expand All @@ -12,8 +12,8 @@ use similar::TextDiff;

use crate::{
output::{
display_error, display_summary, headers::*, plural, write_diff,
OutputOpts, Styles,
display_api_spec, display_error, display_summary, headers::*, plural,
write_diff, OutputOpts, Styles,
},
spec::{all_apis, CheckStatus},
FAILURE_EXIT_CODE, NEEDS_UPDATE_EXIT_CODE,
Expand Down Expand Up @@ -48,6 +48,7 @@ pub(crate) fn check_impl(
let all_apis = all_apis();
let total = all_apis.len();
let count_width = total.to_string().len();
let continued_indent = continued_indent(count_width);

eprintln!("{:>HEADER_WIDTH$}", SEPARATOR);

Expand All @@ -62,16 +63,16 @@ pub(crate) fn check_impl(
let mut num_missing = 0;
let mut num_failed = 0;

for (ix, api) in all_apis.iter().enumerate() {
for (ix, spec) in all_apis.iter().enumerate() {
let count = ix + 1;

match api.check(&dir) {
match spec.check(&dir) {
Ok(status) => match status {
CheckStatus::Ok(summary) => {
eprintln!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}: {}",
UP_TO_DATE.style(styles.success_header),
api.filename,
display_api_spec(spec, &styles),
display_summary(&summary, &styles),
);

Expand All @@ -81,7 +82,7 @@ pub(crate) fn check_impl(
eprintln!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}",
STALE.style(styles.warning_header),
api.filename,
display_api_spec(spec, &styles),
);

let diff = TextDiff::from_lines(&actual, &expected);
Expand All @@ -90,7 +91,10 @@ pub(crate) fn check_impl(
&full_path,
&styles,
// Add an indent to align diff with the status message.
&mut IndentWriter::new(" ", std::io::stderr()),
&mut IndentWriter::new(
&continued_indent,
std::io::stderr(),
),
)?;

num_stale += 1;
Expand All @@ -99,32 +103,32 @@ pub(crate) fn check_impl(
eprintln!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}",
MISSING.style(styles.warning_header),
api.filename,
display_api_spec(spec, &styles),
);

num_missing += 1;
}
},
Err(err) => {
Err(error) => {
eprint!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}: ",
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}",
FAILURE.style(styles.failure_header),
api.filename
display_api_spec(spec, &styles),
);
display_error(
&err,
styles.failure,
&mut IndentWriter::new_skip_initial(
" ",
std::io::stderr(),
),
let display = display_error(&error, styles.failure);
write!(
IndentWriter::new(&continued_indent, std::io::stderr()),
"{}",
display,
)?;

num_failed += 1;
}
};
}

eprintln!("{:>HEADER_WIDTH$}", SEPARATOR);

let status_header = if num_failed > 0 {
FAILURE.style(styles.failure_header)
} else if num_stale > 0 {
Expand Down
36 changes: 19 additions & 17 deletions dev-tools/openapi-manager/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::process::ExitCode;
use std::{io::Write, process::ExitCode};

use anyhow::Result;
use camino::Utf8Path;
Expand All @@ -11,7 +11,8 @@ use owo_colors::OwoColorize;

use crate::{
output::{
display_error, display_summary, headers::*, plural, OutputOpts, Styles,
display_api_spec, display_error, display_summary, headers::*, plural,
OutputOpts, Styles,
},
spec::{all_apis, OverwriteStatus},
FAILURE_EXIT_CODE,
Expand Down Expand Up @@ -44,6 +45,7 @@ pub(crate) fn generate_impl(
let all_apis = all_apis();
let total = all_apis.len();
let count_width = total.to_string().len();
let continued_indent = continued_indent(count_width);

eprintln!("{:>HEADER_WIDTH$}", SEPARATOR);

Expand All @@ -57,16 +59,16 @@ pub(crate) fn generate_impl(
let mut num_unchanged = 0;
let mut num_failed = 0;

for (ix, api) in all_apis.iter().enumerate() {
for (ix, spec) in all_apis.iter().enumerate() {
let count = ix + 1;

match api.overwrite(&dir) {
match spec.overwrite(&dir) {
Ok((status, summary)) => match status {
OverwriteStatus::Updated => {
eprintln!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}: {}",
UPDATED.style(styles.success_header),
api.filename,
display_api_spec(spec, &styles),
display_summary(&summary, &styles),
);
num_updated += 1;
Expand All @@ -75,32 +77,32 @@ pub(crate) fn generate_impl(
eprintln!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}: {}",
UNCHANGED.style(styles.unchanged_header),
api.filename,
display_api_spec(spec, &styles),
display_summary(&summary, &styles),
);
num_unchanged += 1;
}
},
Err(err) => {
eprint!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}: ",
eprintln!(
"{:>HEADER_WIDTH$} [{count:>count_width$}/{total}] {}",
FAILURE.style(styles.failure_header),
api.filename
display_api_spec(spec, &styles),
);
display_error(
&err,
styles.failure,
&mut IndentWriter::new_skip_initial(
" ",
std::io::stderr(),
),
let display = display_error(&err, styles.failure);
write!(
IndentWriter::new(&continued_indent, std::io::stderr()),
"{}",
display,
)?;

num_failed += 1;
}
};
}

eprintln!("{:>HEADER_WIDTH$}", SEPARATOR);

let status_header = if num_failed > 0 {
FAILURE.style(styles.failure_header)
} else {
Expand All @@ -111,7 +113,7 @@ pub(crate) fn generate_impl(
"{:>HEADER_WIDTH$} {} {} generated: \
{} updated, {} unchanged, {} failed",
status_header,
all_apis.len().style(styles.bold),
total.style(styles.bold),
plural::documents(total),
num_updated.style(styles.bold),
num_unchanged.style(styles.bold),
Expand Down
34 changes: 15 additions & 19 deletions dev-tools/openapi-manager/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indent_write::io::IndentWriter;
use owo_colors::OwoColorize;

use crate::{
output::{display_error, OutputOpts, Styles},
output::{display_api_spec, display_error, OutputOpts, Styles},
spec::all_apis,
};

Expand All @@ -23,17 +23,15 @@ pub(crate) fn list_impl(
let mut out = std::io::stdout();

let all_apis = all_apis();

let count_width = all_apis.len().to_string().len();
let total = all_apis.len();
let count_width = total.to_string().len();

if verbose {
// A string for verbose indentation. +1 for the closing ), and +2 for
// further indentation.
let initial_indent =
format!("{:width$}", "", width = count_width + 1 + 2);
let initial_indent = " ".repeat(count_width + 1 + 2);
// This plus 4 more for continued indentation.
let continued_indent =
format!("{:width$}", "", width = count_width + 1 + 2 + 4);
let continued_indent = " ".repeat(count_width + 1 + 2 + 4);

for (ix, api) in all_apis.iter().enumerate() {
let count = ix + 1;
Expand Down Expand Up @@ -91,32 +89,30 @@ pub(crate) fn list_impl(
"{initial_indent} {}: ",
"error".style(styles.failure),
)?;
display_error(
&error,
styles.failure,
&mut IndentWriter::new_skip_initial(
let display = display_error(&error, styles.failure);
write!(
IndentWriter::new_skip_initial(
&continued_indent,
&mut out,
std::io::stderr(),
),
"{}",
display,
)?;
continue;
}
};

if ix + 1 < all_apis.len() {
if ix + 1 < total {
writeln!(&mut out)?;
}
}
} else {
for (ix, api) in all_apis.iter().enumerate() {
for (ix, spec) in all_apis.iter().enumerate() {
let count = ix + 1;

writeln!(
&mut out,
"{count:count_width$}) {}: {} v{}",
api.filename.style(styles.bold),
api.title,
api.version,
"{count:count_width$}) {}",
display_api_spec(spec, &styles),
)?;
}

Expand Down
Loading

0 comments on commit 68fdbb1

Please sign in to comment.