crate_api/
api.rs

1#[derive(Clone, Default, Debug, serde::Serialize, serde::Deserialize)]
2#[serde(rename_all = "snake_case")]
3#[non_exhaustive]
4pub struct Api {
5    pub root_id: Option<PathId>,
6    pub paths: Paths,
7    pub items: Items,
8    pub crates: Crates,
9    pub features: std::collections::BTreeMap<String, AnyFeature>,
10}
11
12impl Api {
13    pub fn new() -> Self {
14        Self::default()
15    }
16}
17
18#[derive(Clone, Default, Debug, serde::Serialize, serde::Deserialize)]
19#[serde(rename_all = "snake_case")]
20pub struct Paths {
21    paths: Vec<(PathId, Path)>,
22}
23
24impl Paths {
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    pub fn push(&mut self, path_: Path) -> PathId {
30        let id = PathId(self.paths.len());
31        self.paths.push((id, path_));
32        id
33    }
34
35    pub fn len(&self) -> usize {
36        self.paths.len()
37    }
38
39    pub fn is_empty(&self) -> bool {
40        self.paths.is_empty()
41    }
42
43    pub fn get(&self, id: PathId) -> Option<&Path> {
44        self.paths.get(id.0).map(|(_i, c)| c)
45    }
46
47    pub fn get_mut(&mut self, id: PathId) -> Option<&mut Path> {
48        self.paths.get_mut(id.0).map(|(_i, c)| c)
49    }
50
51    pub fn iter(&self) -> impl Iterator<Item = (PathId, &Path)> {
52        self.paths.iter().map(|(i, c)| (*i, c))
53    }
54
55    pub fn iter_mut(&mut self) -> impl Iterator<Item = (PathId, &mut Path)> {
56        self.paths.iter_mut().map(|(i, c)| (*i, c))
57    }
58}
59
60#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
61#[serde(rename_all = "snake_case")]
62#[repr(transparent)]
63pub struct PathId(usize);
64
65#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
66#[serde(rename_all = "snake_case")]
67#[non_exhaustive]
68pub struct Path {
69    pub crate_id: Option<CrateId>,
70    pub path: String,
71    pub kind: PathKind,
72    pub span: Option<Span>,
73    pub item_id: Option<ItemId>,
74    pub children: Vec<PathId>,
75}
76
77impl Path {
78    pub fn new(kind: PathKind, path: impl Into<String>) -> Self {
79        Self {
80            crate_id: None,
81            path: path.into(),
82            kind,
83            span: None,
84            item_id: None,
85            children: Vec::new(),
86        }
87    }
88}
89
90#[derive(
91    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
92)]
93#[serde(rename_all = "snake_case")]
94#[non_exhaustive]
95pub enum PathKind {
96    Module,
97    ExternCrate,
98    Import,
99    Struct,
100    Union,
101    Enum,
102    Variant,
103    Function,
104    Typedef,
105    OpaqueTy,
106    Constant,
107    Trait,
108    TraitAlias,
109    Method,
110    Impl,
111    Static,
112    ForeignType,
113    Macro,
114    ProcAttribute,
115    ProcDerive,
116    AssocConst,
117    AssocType,
118    Primitive,
119    Keyword,
120}
121
122#[derive(Clone, Default, Debug, serde::Serialize, serde::Deserialize)]
123#[serde(rename_all = "snake_case")]
124pub struct Items {
125    items: Vec<(ItemId, Item)>,
126}
127
128impl Items {
129    pub fn new() -> Self {
130        Self::default()
131    }
132
133    pub fn push(&mut self, item_: Item) -> ItemId {
134        let id = ItemId(self.items.len());
135        self.items.push((id, item_));
136        id
137    }
138
139    pub fn len(&self) -> usize {
140        self.items.len()
141    }
142
143    pub fn is_empty(&self) -> bool {
144        self.items.is_empty()
145    }
146
147    pub fn get(&self, id: ItemId) -> Option<&Item> {
148        self.items.get(id.0).map(|(_i, c)| c)
149    }
150
151    pub fn get_mut(&mut self, id: ItemId) -> Option<&mut Item> {
152        self.items.get_mut(id.0).map(|(_i, c)| c)
153    }
154
155    pub fn iter(&self) -> impl Iterator<Item = (ItemId, &Item)> {
156        self.items.iter().map(|(i, c)| (*i, c))
157    }
158
159    pub fn iter_mut(&mut self) -> impl Iterator<Item = (ItemId, &mut Item)> {
160        self.items.iter_mut().map(|(i, c)| (*i, c))
161    }
162}
163
164#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
165#[serde(rename_all = "snake_case")]
166#[repr(transparent)]
167pub struct ItemId(usize);
168
169#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
170#[serde(rename_all = "snake_case")]
171#[non_exhaustive]
172pub struct Item {
173    pub crate_id: Option<CrateId>,
174    pub name: Option<String>,
175    pub span: Option<Span>,
176}
177
178impl Item {
179    pub fn new() -> Self {
180        Self {
181            crate_id: None,
182            name: None,
183            span: None,
184        }
185    }
186}
187
188impl Default for Item {
189    fn default() -> Self {
190        Self::new()
191    }
192}
193
194#[derive(Clone, Default, Debug, serde::Serialize, serde::Deserialize)]
195#[serde(rename_all = "snake_case")]
196pub struct Crates {
197    crates: Vec<(CrateId, Crate)>,
198}
199
200impl Crates {
201    pub fn new() -> Self {
202        Self::default()
203    }
204
205    pub fn push(&mut self, crate_: Crate) -> CrateId {
206        let id = CrateId(self.crates.len());
207        self.crates.push((id, crate_));
208        id
209    }
210
211    pub fn len(&self) -> usize {
212        self.crates.len()
213    }
214
215    pub fn is_empty(&self) -> bool {
216        self.crates.is_empty()
217    }
218
219    pub fn get(&self, id: CrateId) -> Option<&Crate> {
220        self.crates.get(id.0).map(|(_i, c)| c)
221    }
222
223    pub fn get_mut(&mut self, id: CrateId) -> Option<&mut Crate> {
224        self.crates.get_mut(id.0).map(|(_i, c)| c)
225    }
226
227    pub fn iter(&self) -> impl Iterator<Item = (CrateId, &Crate)> {
228        self.crates.iter().map(|(i, c)| (*i, c))
229    }
230
231    pub fn iter_mut(&mut self) -> impl Iterator<Item = (CrateId, &mut Crate)> {
232        self.crates.iter_mut().map(|(i, c)| (*i, c))
233    }
234}
235
236#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
237#[serde(rename_all = "snake_case")]
238#[repr(transparent)]
239pub struct CrateId(usize);
240
241#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
242#[serde(rename_all = "snake_case")]
243#[non_exhaustive]
244pub struct Crate {
245    pub name: String,
246    pub version: Option<cargo_metadata::VersionReq>,
247}
248
249impl Crate {
250    pub fn new(name: impl Into<String>) -> Self {
251        Self {
252            name: name.into(),
253            version: None,
254        }
255    }
256}
257
258#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
259#[serde(rename_all = "snake_case")]
260pub struct Span {
261    /// The relative path to the source file for this span
262    pub filename: std::path::PathBuf,
263    /// Zero indexed Line and Column of the first character of the `Span`
264    pub begin: (usize, usize),
265    /// Zero indexed Line and Column of the last character of the `Span`
266    pub end: (usize, usize),
267}
268
269#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
270#[serde(rename_all = "snake_case")]
271#[serde(tag = "kind")]
272pub enum AnyFeature {
273    Feature(Feature),
274    OptionalDependency(OptionalDependency),
275}
276
277#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
278#[serde(rename_all = "snake_case")]
279#[non_exhaustive]
280pub struct Feature {
281    pub name: String,
282    pub dependencies: Vec<String>,
283}
284
285impl Feature {
286    pub fn new(name: impl Into<String>) -> Self {
287        Self {
288            name: name.into(),
289            dependencies: Vec::new(),
290        }
291    }
292}
293
294#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
295#[serde(rename_all = "snake_case")]
296#[non_exhaustive]
297pub struct OptionalDependency {
298    /// The name used to activate this dependency
299    pub name: String,
300    /// If renamed, the actual dependency name
301    pub package: Option<String>,
302}
303
304impl OptionalDependency {
305    pub fn new(name: impl Into<String>) -> Self {
306        Self {
307            name: name.into(),
308            package: None,
309        }
310    }
311}