1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use predicates::Predicate;
use std::fmt;
use crate::{
CapturedEvents, CapturedSpan, CapturedSpans, DescendantEvents, DescendantSpans, Storage,
};
pub trait ScanExt<'a>: Sized {
fn scan_spans(self) -> Scanner<Self, CapturedSpans<'a>>;
fn scan_events(self) -> Scanner<Self, CapturedEvents<'a>>;
}
impl<'a> ScanExt<'a> for &'a Storage {
fn scan_spans(self) -> Scanner<Self, CapturedSpans<'a>> {
Scanner::new(self, Storage::all_spans)
}
fn scan_events(self) -> Scanner<Self, CapturedEvents<'a>> {
Scanner::new(self, Storage::all_events)
}
}
impl<'a> ScanExt<'a> for CapturedSpan<'a> {
fn scan_spans(self) -> Scanner<Self, CapturedSpans<'a>> {
Scanner::new(self, |span| span.children())
}
fn scan_events(self) -> Scanner<Self, CapturedEvents<'a>> {
Scanner::new(self, |span| span.events())
}
}
impl<'a> CapturedSpan<'a> {
pub fn deep_scan_spans(self) -> Scanner<Self, DescendantSpans<'a>> {
Scanner::new(self, |span| span.descendants())
}
pub fn deep_scan_events(self) -> Scanner<Self, DescendantEvents<'a>> {
Scanner::new(self, |span| span.descendant_events())
}
}
#[derive(Debug)]
pub struct Scanner<T, I> {
items: T,
into_iter: fn(T) -> I,
}
impl<T: Clone, I> Clone for Scanner<T, I> {
fn clone(&self) -> Self {
Self {
items: self.items.clone(),
into_iter: self.into_iter,
}
}
}
impl<T: Copy, I> Copy for Scanner<T, I> {}
impl<T, I> Scanner<T, I>
where
I: Iterator,
I::Item: fmt::Debug,
{
fn new(items: T, into_iter: fn(T) -> I) -> Self {
Self { items, into_iter }
}
fn iter(self) -> I {
(self.into_iter)(self.items)
}
pub fn single<P: Predicate<I::Item> + ?Sized>(self, predicate: &P) -> I::Item {
let mut iter = self.iter();
let first = iter
.find(|item| predicate.eval(item))
.unwrap_or_else(|| panic!("no items have matched predicate {predicate}"));
let second = iter.find(|item| predicate.eval(item));
if let Some(second) = second {
panic!(
"multiple items match predicate {predicate}: {:#?}",
[first, second]
);
}
first
}
pub fn first<P: Predicate<I::Item> + ?Sized>(self, predicate: &P) -> I::Item {
let mut iter = self.iter();
iter.find(|item| predicate.eval(item))
.unwrap_or_else(|| panic!("no items have matched predicate {predicate}"))
}
pub fn all<P: Predicate<I::Item> + ?Sized>(self, predicate: &P) {
let mut iter = self.iter();
if let Some(item) = iter.find(|item| !predicate.eval(item)) {
panic!("item does not match predicate {predicate}: {item:#?}");
}
}
pub fn none<P: Predicate<I::Item> + ?Sized>(self, predicate: &P) {
let mut iter = self.iter();
if let Some(item) = iter.find(|item| predicate.eval(item)) {
panic!("item matched predicate {predicate}: {item:#?}");
}
}
}
impl<T, I> Scanner<T, I>
where
I: DoubleEndedIterator,
I::Item: fmt::Debug,
{
pub fn last<P: Predicate<I::Item> + ?Sized>(self, predicate: &P) -> I::Item {
let mut iter = self.iter().rev();
iter.find(|item| predicate.eval(item))
.unwrap_or_else(|| panic!("no items have matched predicate {predicate}"))
}
}