gix_trace/forest/
processor.rs1use crate::forest::{Processor, Tree};
5use std::{error, fmt, sync::Arc};
6
7#[derive(Debug)]
9pub struct Error {
10 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
28pub fn error(tree: Tree, source: gix_error::Error) -> Error {
30 Error { tree, source }
31}
32
33pub type Result = std::result::Result<(), Error>;
35
36#[derive(Debug)]
40pub struct WithFallback<P, F> {
41 pub(super) primary: P,
42 pub(super) fallback: F,
43}
44
45#[derive(Debug)]
49pub struct Sink;
50
51#[derive(Debug)]
55pub struct FromFn<F>(F);
56
57pub 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}