hakari/verify/
display.rs

1// Copyright (c) The cargo-guppy Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use crate::verify::VerifyErrors;
5use indenter::indented;
6use owo_colors::{OwoColorize, Style};
7use std::fmt::{self, Write};
8
9/// A display formatter for [`VerifyErrors`].
10///
11/// Requires the `cli-support` feature.
12#[derive(Clone, Debug)]
13pub struct VerifyErrorsDisplay<'g, 'verify> {
14    verify: &'verify VerifyErrors<'g>,
15    styles: Styles,
16    color: bool,
17}
18
19impl<'g, 'verify> VerifyErrorsDisplay<'g, 'verify> {
20    pub(super) fn new(verify: &'verify VerifyErrors<'g>) -> Self {
21        Self {
22            verify,
23            styles: Styles::default(),
24            color: false,
25        }
26    }
27
28    /// Adds ANSI color codes to the output.
29    pub fn colorize(&mut self) -> &mut Self {
30        self.styles.colorize();
31        self.color = true;
32        self
33    }
34}
35
36impl fmt::Display for VerifyErrorsDisplay<'_, '_> {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        for explain in self.verify.errors() {
39            writeln!(
40                f,
41                "for dependency {}:\n",
42                explain
43                    .dependency()
44                    .id()
45                    .style(self.styles.dependency_id_style)
46            )?;
47            let mut display = explain.display();
48            if self.color {
49                display.colorize();
50            }
51            write!(indented(f).with_str("  "), "{}", display)?;
52        }
53
54        Ok(())
55    }
56}
57
58#[derive(Clone, Debug, Default)]
59struct Styles {
60    dependency_id_style: Style,
61}
62
63impl Styles {
64    fn colorize(&mut self) {
65        self.dependency_id_style = Style::new().bright_magenta();
66    }
67}