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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::{collections::HashMap, fmt::Display, rc::Rc};
use rustdoc_types::{Crate, Id, Impl, Item, ItemEnum, Type};
use super::intermediate_public_item::IntermediatePublicItem;
use crate::{tokens::Token, Options};
type Impls<'a> = HashMap<&'a Id, Vec<&'a Impl>>;
pub struct ItemIterator<'a> {
crate_: &'a Crate,
items_left: Vec<Rc<IntermediatePublicItem<'a>>>,
missing_ids: Vec<&'a Id>,
impls: Impls<'a>,
}
impl<'a> ItemIterator<'a> {
pub fn new(crate_: &'a Crate, options: Options) -> Self {
let mut s = ItemIterator {
crate_,
items_left: vec![],
missing_ids: vec![],
impls: find_all_impls(crate_, options),
};
s.try_add_item_to_visit(&crate_.root, None);
s
}
fn add_children_for_item(&mut self, public_item: &Rc<IntermediatePublicItem<'a>>) {
let mut add_after_borrow = vec![];
if let Some(impls) = self.impls.get(&public_item.item.id) {
for impl_ in impls {
for id in &impl_.items {
add_after_borrow.push(id);
}
}
}
for id in add_after_borrow {
self.try_add_item_to_visit(id, Some(public_item.clone()));
}
for child in items_in_container(public_item.item).into_iter().flatten() {
self.try_add_item_to_visit(child, Some(public_item.clone()));
}
}
fn try_add_item_to_visit(
&mut self,
id: &'a Id,
parent: Option<Rc<IntermediatePublicItem<'a>>>,
) {
match self.crate_.index.get(id) {
Some(Item {
inner: ItemEnum::Impl { .. },
..
}) => (),
Some(item) => self.items_left.push(Rc::new(IntermediatePublicItem::new(
item,
self.crate_,
parent,
))),
None => self.missing_ids.push(id),
}
}
}
impl<'a> Iterator for ItemIterator<'a> {
type Item = Rc<IntermediatePublicItem<'a>>;
fn next(&mut self) -> Option<Self::Item> {
let mut result = None;
if let Some(public_item) = self.items_left.pop() {
self.add_children_for_item(&public_item.clone());
result = Some(public_item);
}
result
}
}
fn find_all_impls(crate_: &Crate, options: Options) -> Impls {
let mut impls = HashMap::new();
for item in crate_.index.values() {
if let ItemEnum::Impl(impl_) = &item.inner {
if let Impl {
for_: Type::ResolvedPath { id, .. },
blanket_impl,
..
} = impl_
{
let omit = !options.with_blanket_implementations && matches!(blanket_impl, Some(_));
if !omit {
impls.entry(id).or_insert_with(Vec::new).push(impl_);
}
}
}
}
impls
}
fn items_in_container(item: &Item) -> Option<&Vec<Id>> {
match &item.inner {
ItemEnum::Module(m) => Some(&m.items),
ItemEnum::Union(u) => Some(&u.fields),
ItemEnum::Struct(s) => Some(&s.fields),
ItemEnum::Enum(e) => Some(&e.variants),
ItemEnum::Trait(t) => Some(&t.items),
ItemEnum::Impl(i) => Some(&i.items),
ItemEnum::Variant(rustdoc_types::Variant::Struct(ids)) => Some(ids),
_ => None,
}
}
pub fn public_api_in_crate(
crate_: &Crate,
options: Options,
) -> impl Iterator<Item = PublicItem> + '_ {
ItemIterator::new(crate_, options).map(|p| intermediate_public_item_to_public_item(&p))
}
fn intermediate_public_item_to_public_item(
public_item: &Rc<IntermediatePublicItem<'_>>,
) -> PublicItem {
PublicItem {
path: public_item
.path()
.iter()
.map(|i| i.get_effective_name())
.collect::<Vec<String>>(),
tokens: public_item.render_token_stream(),
}
}
#[derive(Clone)]
pub struct PublicItem {
pub(crate) path: Vec<String>,
pub(crate) tokens: Vec<Token>,
}
impl PublicItem {
pub fn tokens(&self) -> impl Iterator<Item = &Token> {
self.tokens.iter()
}
}
impl std::fmt::Debug for PublicItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl Display for PublicItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", tokens_to_string(&self.tokens))
}
}
pub(crate) fn tokens_to_string(tokens: &[Token]) -> String {
tokens.iter().map(Token::text).collect()
}
impl PartialEq for PublicItem {
fn eq(&self, other: &Self) -> bool {
self.path == other.path && self.tokens == other.tokens
}
}
impl Eq for PublicItem {}
impl PartialOrd for PublicItem {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PublicItem {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.to_string().cmp(&other.to_string())
}
}