Skip to main content

gix_trace/forest/printer/
mod.rs

1//! Utilities for formatting and writing trace trees.
2use crate::forest::{Formatter, PrettyPrinter, Printer, Processor, TestCapturePrinter, Tree, processor};
3use gix_error::ResultExt;
4use std::error::Error;
5use std::io::{self, Write};
6use tracing_subscriber::fmt::MakeWriter;
7
8mod pretty;
9pub use pretty::{FilteredPretty, Pretty};
10
11impl<F, E> Formatter for F
12where
13    F: Fn(&Tree) -> Result<String, E>,
14    E: Error + Send + Sync,
15{
16    type Error = E;
17
18    #[inline]
19    fn fmt(&self, tree: &Tree) -> Result<String, E> {
20        self(tree)
21    }
22}
23
24/// A [`MakeWriter`] that writes to stdout.
25///
26/// This is functionally the same as using [`std::io::stdout`] as a `MakeWriter`,
27/// except it has a named type and can therefore be used in type signatures.
28#[derive(Debug)]
29pub struct MakeStdout;
30
31/// A [`MakeWriter`] that writes to stderr.
32///
33/// This is functionally the same as using [`std::io::stderr`] as a `MakeWriter`,
34/// except it has a named type and can therefore be used in type signatures.
35#[derive(Debug)]
36pub struct MakeStderr;
37
38impl MakeWriter<'_> for MakeStdout {
39    type Writer = io::Stdout;
40
41    fn make_writer(&self) -> Self::Writer {
42        io::stdout()
43    }
44}
45
46impl MakeWriter<'_> for MakeStderr {
47    type Writer = io::Stderr;
48
49    fn make_writer(&self) -> Self::Writer {
50        io::stderr()
51    }
52}
53
54impl PrettyPrinter {
55    /// Returns a new [`PrettyPrinter`] that pretty-prints to stdout.
56    ///
57    /// Use [`Printer::formatter`] and [`Printer::writer`] for custom configuration.
58    pub const fn new() -> Self {
59        Printer {
60            formatter: Pretty,
61            make_writer: MakeStdout,
62        }
63    }
64}
65
66impl<F, W> Printer<F, W>
67where
68    F: 'static + Formatter,
69    W: 'static + for<'a> MakeWriter<'a>,
70{
71    /// Set the formatter.
72    ///
73    /// See the [`Formatter`] trait for details on possible inputs.
74    pub fn formatter<F2>(self, formatter: F2) -> Printer<F2, W>
75    where
76        F2: 'static + Formatter,
77    {
78        Printer {
79            formatter,
80            make_writer: self.make_writer,
81        }
82    }
83
84    /// Set the writer.
85    pub fn writer<W2>(self, make_writer: W2) -> Printer<F, W2>
86    where
87        W2: 'static + for<'a> MakeWriter<'a>,
88    {
89        Printer {
90            formatter: self.formatter,
91            make_writer,
92        }
93    }
94}
95
96impl Default for PrettyPrinter {
97    fn default() -> Self {
98        PrettyPrinter::new()
99    }
100}
101
102impl<F, W> Processor for Printer<F, W>
103where
104    F: 'static + Formatter,
105    W: 'static + for<'a> MakeWriter<'a>,
106{
107    fn process(&self, tree: Tree) -> processor::Result {
108        let string = match self.formatter.fmt(&tree).or_error() {
109            Ok(s) => s,
110            Err(e) => return Err(processor::error(tree, e)),
111        };
112
113        match self.make_writer.make_writer().write_all(string.as_bytes()).or_error() {
114            Ok(()) => Ok(()),
115            Err(e) => Err(processor::error(tree, e)),
116        }
117    }
118}
119
120impl TestCapturePrinter<Pretty> {
121    /// Construct a new test capturing printer with the default `Pretty` formatter. This printer
122    /// is intended for use in tests only as it works with Rust's default stdout capture mechanism.
123    #[allow(clippy::new_without_default)]
124    pub const fn new() -> Self {
125        TestCapturePrinter { formatter: Pretty }
126    }
127}
128
129impl<F> Processor for TestCapturePrinter<F>
130where
131    F: 'static + Formatter,
132{
133    fn process(&self, tree: Tree) -> processor::Result {
134        let string = self
135            .formatter
136            .fmt(&tree)
137            .or_error()
138            .map_err(|e| processor::error(tree, e))?;
139
140        print!("{string}");
141        Ok(())
142    }
143}