From 68fdbb1fb265794f8317a4550952aa167ba084d3 Mon Sep 17 00:00:00 2001 From: Rain Date: Fri, 5 Jul 2024 15:19:51 -0700 Subject: [PATCH] Cleanup Created using spr 1.3.6-beta.1 --- dev-tools/openapi-manager/src/check.rs | 42 +++++----- dev-tools/openapi-manager/src/generate.rs | 36 +++++---- dev-tools/openapi-manager/src/list.rs | 34 ++++---- dev-tools/openapi-manager/src/output.rs | 97 ++++++++++++++++++----- 4 files changed, 133 insertions(+), 76 deletions(-) diff --git a/dev-tools/openapi-manager/src/check.rs b/dev-tools/openapi-manager/src/check.rs index 7e2ed4e4b3..182ed9fb19 100644 --- a/dev-tools/openapi-manager/src/check.rs +++ b/dev-tools/openapi-manager/src/check.rs @@ -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; @@ -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, @@ -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); @@ -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), ); @@ -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); @@ -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; @@ -99,25 +103,23 @@ 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; @@ -125,6 +127,8 @@ pub(crate) fn check_impl( }; } + eprintln!("{:>HEADER_WIDTH$}", SEPARATOR); + let status_header = if num_failed > 0 { FAILURE.style(styles.failure_header) } else if num_stale > 0 { diff --git a/dev-tools/openapi-manager/src/generate.rs b/dev-tools/openapi-manager/src/generate.rs index 017100d183..f776ff2709 100644 --- a/dev-tools/openapi-manager/src/generate.rs +++ b/dev-tools/openapi-manager/src/generate.rs @@ -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; @@ -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, @@ -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); @@ -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; @@ -75,25 +77,23 @@ 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; @@ -101,6 +101,8 @@ pub(crate) fn generate_impl( }; } + eprintln!("{:>HEADER_WIDTH$}", SEPARATOR); + let status_header = if num_failed > 0 { FAILURE.style(styles.failure_header) } else { @@ -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), diff --git a/dev-tools/openapi-manager/src/list.rs b/dev-tools/openapi-manager/src/list.rs index 27582fba78..bf1920c69d 100644 --- a/dev-tools/openapi-manager/src/list.rs +++ b/dev-tools/openapi-manager/src/list.rs @@ -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, }; @@ -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; @@ -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), )?; } diff --git a/dev-tools/openapi-manager/src/output.rs b/dev-tools/openapi-manager/src/output.rs index b127f3e46a..6cd578e778 100644 --- a/dev-tools/openapi-manager/src/output.rs +++ b/dev-tools/openapi-manager/src/output.rs @@ -2,15 +2,15 @@ // 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::{fmt, io, io::Write}; +use std::{fmt, fmt::Write, io}; use camino::Utf8Path; use clap::{Args, ColorChoice}; -use indent_write::io::IndentWriter; +use indent_write::fmt::IndentWriter; use owo_colors::{OwoColorize, Style}; use similar::{ChangeTag, DiffableStr, TextDiff}; -use crate::spec::DocumentSummary; +use crate::spec::{ApiSpec, DocumentSummary}; #[derive(Debug, Args)] #[clap(next_help_heading = "Global options")] @@ -40,6 +40,7 @@ pub(crate) struct Styles { pub(crate) failure_header: Style, pub(crate) warning_header: Style, pub(crate) unchanged_header: Style, + pub(crate) filename: Style, pub(crate) diff_before: Style, pub(crate) diff_after: Style, } @@ -53,6 +54,7 @@ impl Styles { self.failure_header = Style::new().red().bold(); self.unchanged_header = Style::new().blue().bold(); self.warning_header = Style::new().yellow().bold(); + self.filename = Style::new().cyan(); self.diff_before = Style::new().red(); self.diff_after = Style::new().green(); } @@ -112,17 +114,30 @@ where Ok(()) } +pub(crate) fn display_api_spec(spec: &ApiSpec, styles: &Styles) -> String { + format!( + "{} ({} v{})", + spec.filename.style(styles.filename), + spec.title, + spec.version, + ) +} + pub(crate) fn display_summary( summary: &DocumentSummary, styles: &Styles, ) -> String { - let mut summary_str = - format!("{} paths", summary.path_count.to_string().style(styles.bold)); + let mut summary_str = format!( + "{} {}", + summary.path_count.style(styles.bold), + plural::paths(summary.path_count), + ); if let Some(schema_count) = summary.schema_count { summary_str.push_str(&format!( - ", {} schemas", - schema_count.to_string().style(styles.bold), + ", {} {}", + schema_count.style(styles.bold), + plural::schemas(schema_count), )); } else { summary_str.push_str(&format!( @@ -137,22 +152,32 @@ pub(crate) fn display_summary( pub(crate) fn display_error( error: &anyhow::Error, failure_style: Style, - mut out: &mut dyn io::Write, -) -> io::Result<()> { - writeln!(out, "{}", error.style(failure_style))?; - - let mut source = error.source(); - while let Some(curr) = source { - write!(out, "-> ")?; - writeln!( - IndentWriter::new_skip_initial(" ", &mut out), - "{}", - curr.style(failure_style), - )?; - source = curr.source(); +) -> impl fmt::Display + '_ { + struct DisplayError<'a> { + error: &'a anyhow::Error, + failure_style: Style, } - Ok(()) + impl fmt::Display for DisplayError<'_> { + fn fmt(&self, mut f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!(f, "{}", self.error.style(self.failure_style))?; + + let mut source = self.error.source(); + while let Some(curr) = source { + write!(f, "-> ")?; + writeln!( + IndentWriter::new_skip_initial(" ", &mut f), + "{}", + curr.style(self.failure_style), + )?; + source = curr.source(); + } + + Ok(()) + } + } + + DisplayError { error, failure_style } } struct MissingNewlineHint(bool); @@ -185,6 +210,20 @@ pub(crate) mod headers { pub(crate) static SUCCESS: &str = "Success"; pub(crate) static FAILURE: &str = "Failure"; + + pub(crate) fn continued_indent(count_width: usize) -> String { + // Status strings are of the form: + // + // Generated [ 1/12] api.json: 1 path, 1 schema + // + // So the continued indent is: + // + // HEADER_WIDTH for the status string + // + (count_width * 2) for current and total counts + // + 3 for '[/]' + // + 2 for spaces on either side. + " ".repeat(HEADER_WIDTH + count_width * 2 + 3 + 2) + } } pub(crate) mod plural { @@ -195,4 +234,20 @@ pub(crate) mod plural { "documents" } } + + pub(crate) fn paths(count: usize) -> &'static str { + if count == 1 { + "path" + } else { + "paths" + } + } + + pub(crate) fn schemas(count: usize) -> &'static str { + if count == 1 { + "schema" + } else { + "schemas" + } + } }