Skip to main content

gix_trace/forest/
processor.rs

1//! Trait for processing log trees on completion.
2//!
3//! See [`Processor`] for more details.
4use crate::forest::{Processor, Tree};
5use std::{error, fmt, sync::Arc};
6
7/// Error type returned if a [`Processor`] fails.
8#[derive(Debug)]
9pub struct Error {
10    /// The recoverable [`Tree`] type that couldn't be processed.
11    pub tree: Tree,
12
13    source: gix_error::Error,
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        fmt::Display::fmt(&self.source, f)
19    }
20}
21
22impl error::Error for Error {
23    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
24        Some(&self.source)
25    }
26}
27
28/// Create an error for when a [`Processor`] fails to process a [`Tree`].
29pub fn error(tree: Tree, source: gix_error::Error) -> Error {
30    Error { tree, source }
31}
32
33/// The result type of [`Processor::process`].
34pub type Result = std::result::Result<(), Error>;
35
36/// A [`Processor`] composed of a primary and a fallback `Processor`.
37///
38/// This type is returned by [`Processor::or`].
39#[derive(Debug)]
40pub struct WithFallback<P, F> {
41    pub(super) primary: P,
42    pub(super) fallback: F,
43}
44
45/// A [`Processor`] that ignores any incoming logs.
46///
47/// This processor cannot fail.
48#[derive(Debug)]
49pub struct Sink;
50
51/// A [`Processor`] that processes incoming logs via a function.
52///
53/// Instances of `FromFn` are returned by the [`from_fn`] function.
54#[derive(Debug)]
55pub struct FromFn<F>(F);
56
57/// Create a processor that processes incoming logs via a function.
58///
59/// # Examples
60///
61/// Send completed trees across a channel for another thread to process.
62/// ```
63/// use gix_error::ErrorExt;
64/// use gix_trace::forest::processor;
65///
66/// let (tx, rx) = std::sync::mpsc::channel();
67///
68/// let sender_processor = processor::from_fn(move |tree| tx
69///     .send(tree)
70///     .map_err(|err| {
71///         let msg = gix_error::message!("{err}").raise();
72///         processor::error(err.0, msg)
73///     })
74/// );
75/// ```
76pub fn from_fn<F>(f: F) -> FromFn<F>
77where
78    F: 'static + Fn(Tree) -> Result,
79{
80    FromFn(f)
81}
82
83impl<P, F> Processor for WithFallback<P, F>
84where
85    P: Processor,
86    F: Processor,
87{
88    fn process(&self, tree: Tree) -> Result {
89        self.primary.process(tree).or_else(|err| {
90            eprintln!("{err}, using fallback processor...");
91            self.fallback.process(err.tree)
92        })
93    }
94}
95
96impl Processor for Sink {
97    fn process(&self, _tree: Tree) -> Result {
98        Ok(())
99    }
100}
101
102impl<F> Processor for FromFn<F>
103where
104    F: 'static + Fn(Tree) -> Result,
105{
106    fn process(&self, tree: Tree) -> Result {
107        (self.0)(tree)
108    }
109}
110
111impl<P: Processor> Processor for Box<P> {
112    fn process(&self, tree: Tree) -> Result {
113        self.as_ref().process(tree)
114    }
115}
116
117impl<P: Processor> Processor for Arc<P> {
118    fn process(&self, tree: Tree) -> Result {
119        self.as_ref().process(tree)
120    }
121}