]>
Commit | Line | Data |
---|---|---|
fc512014 XL |
1 | //! Rustdoc's JSON output interface |
2 | //! | |
3 | //! These types are the public API exposed through the `--output-format json` flag. The [`Crate`] | |
4 | //! struct is the root of the JSON blob and all other items are contained within. | |
5 | ||
5e7ed085 | 6 | use std::collections::HashMap; |
fc512014 XL |
7 | use std::path::PathBuf; |
8 | ||
fc512014 XL |
9 | use serde::{Deserialize, Serialize}; |
10 | ||
5099ac24 | 11 | /// rustdoc format-version. |
2b03887a | 12 | pub const FORMAT_VERSION: u32 = 22; |
5099ac24 | 13 | |
fc512014 XL |
14 | /// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information |
15 | /// about the language items in the local crate, as well as info about external items to allow | |
16 | /// tools to find or link to them. | |
064997fb | 17 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
fc512014 XL |
18 | pub struct Crate { |
19 | /// The id of the root [`Module`] item of the local crate. | |
20 | pub root: Id, | |
21 | /// The version string given to `--crate-version`, if any. | |
22 | pub crate_version: Option<String>, | |
23 | /// Whether or not the output includes private items. | |
24 | pub includes_private: bool, | |
25 | /// A collection of all items in the local crate as well as some external traits and their | |
26 | /// items that are referenced locally. | |
5869c6ff | 27 | pub index: HashMap<Id, Item>, |
fc512014 | 28 | /// Maps IDs to fully qualified paths and other info helpful for generating links. |
5869c6ff | 29 | pub paths: HashMap<Id, ItemSummary>, |
fc512014 | 30 | /// Maps `crate_id` of items to a crate name and html_root_url if it exists. |
5869c6ff | 31 | pub external_crates: HashMap<u32, ExternalCrate>, |
fc512014 XL |
32 | /// A single version number to be used in the future when making backwards incompatible changes |
33 | /// to the JSON output. | |
34 | pub format_version: u32, | |
35 | } | |
36 | ||
064997fb | 37 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
38 | pub struct ExternalCrate { |
39 | pub name: String, | |
40 | pub html_root_url: Option<String>, | |
41 | } | |
42 | ||
43 | /// For external (not defined in the local crate) items, you don't get the same level of | |
44 | /// information. This struct should contain enough to generate a link/reference to the item in | |
45 | /// question, or can be used by a tool that takes the json output of multiple crates to find | |
46 | /// the actual item definition with all the relevant info. | |
064997fb | 47 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
48 | pub struct ItemSummary { |
49 | /// Can be used to look up the name and html_root_url of the crate this item came from in the | |
50 | /// `external_crates` map. | |
51 | pub crate_id: u32, | |
52 | /// The list of path components for the fully qualified path of this item (e.g. | |
53 | /// `["std", "io", "lazy", "Lazy"]` for `std::io::lazy::Lazy`). | |
2b03887a FG |
54 | /// |
55 | /// Note that items can appear in multiple paths, and the one chosen is implementation | |
56 | /// defined. Currenty, this is the full path to where the item was defined. Eg | |
57 | /// [`String`] is currently `["alloc", "string", "String"]` and [`HashMap`] is | |
58 | /// `["std", "collections", "hash", "map", "HashMap"]`, but this is subject to change. | |
fc512014 XL |
59 | pub path: Vec<String>, |
60 | /// Whether this item is a struct, trait, macro, etc. | |
61 | pub kind: ItemKind, | |
62 | } | |
63 | ||
064997fb | 64 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
fc512014 XL |
65 | pub struct Item { |
66 | /// The unique identifier of this item. Can be used to find this item in various mappings. | |
67 | pub id: Id, | |
68 | /// This can be used as a key to the `external_crates` map of [`Crate`] to see which crate | |
69 | /// this item came from. | |
70 | pub crate_id: u32, | |
71 | /// Some items such as impls don't have names. | |
72 | pub name: Option<String>, | |
73 | /// The source location of this item (absent if it came from a macro expansion or inline | |
74 | /// assembly). | |
cdc7bbd5 | 75 | pub span: Option<Span>, |
fc512014 XL |
76 | /// By default all documented items are public, but you can tell rustdoc to output private items |
77 | /// so this field is needed to differentiate. | |
78 | pub visibility: Visibility, | |
5869c6ff XL |
79 | /// The full markdown docstring of this item. Absent if there is no documentation at all, |
80 | /// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`). | |
81 | pub docs: Option<String>, | |
fc512014 | 82 | /// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs |
5869c6ff | 83 | pub links: HashMap<String, Id>, |
fc512014 XL |
84 | /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`) |
85 | pub attrs: Vec<String>, | |
86 | pub deprecation: Option<Deprecation>, | |
6a06907d | 87 | #[serde(flatten)] |
fc512014 XL |
88 | pub inner: ItemEnum, |
89 | } | |
90 | ||
064997fb | 91 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
92 | pub struct Span { |
93 | /// The path to the source file for this span relative to the path `rustdoc` was invoked with. | |
94 | pub filename: PathBuf, | |
95 | /// Zero indexed Line and Column of the first character of the `Span` | |
96 | pub begin: (usize, usize), | |
97 | /// Zero indexed Line and Column of the last character of the `Span` | |
98 | pub end: (usize, usize), | |
99 | } | |
100 | ||
064997fb | 101 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
102 | pub struct Deprecation { |
103 | pub since: Option<String>, | |
104 | pub note: Option<String>, | |
105 | } | |
106 | ||
064997fb | 107 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 108 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
109 | pub enum Visibility { |
110 | Public, | |
111 | /// For the most part items are private by default. The exceptions are associated items of | |
112 | /// public traits and variants of public enums. | |
113 | Default, | |
114 | Crate, | |
115 | /// For `pub(in path)` visibility. `parent` is the module it's restricted to and `path` is how | |
116 | /// that module was referenced (like `"super::super"` or `"crate::foo::bar"`). | |
117 | Restricted { | |
118 | parent: Id, | |
119 | path: String, | |
120 | }, | |
121 | } | |
122 | ||
f2b60f7d FG |
123 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
124 | pub struct DynTrait { | |
125 | /// All the traits implemented. One of them is the vtable, and the rest must be auto traits. | |
126 | pub traits: Vec<PolyTrait>, | |
127 | /// The lifetime of the whole dyn object | |
128 | /// ```text | |
129 | /// dyn Debug + 'static | |
130 | /// ^^^^^^^ | |
131 | /// | | |
132 | /// this part | |
133 | /// ``` | |
134 | pub lifetime: Option<String>, | |
135 | } | |
136 | ||
137 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] | |
138 | /// A trait and potential HRTBs | |
139 | pub struct PolyTrait { | |
140 | #[serde(rename = "trait")] | |
141 | pub trait_: Path, | |
142 | /// Used for Higher-Rank Trait Bounds (HRTBs) | |
143 | /// ```text | |
144 | /// dyn for<'a> Fn() -> &'a i32" | |
145 | /// ^^^^^^^ | |
146 | /// | | |
147 | /// this part | |
148 | /// ``` | |
149 | pub generic_params: Vec<GenericParamDef>, | |
150 | } | |
151 | ||
064997fb | 152 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 153 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
154 | pub enum GenericArgs { |
155 | /// <'a, 32, B: Copy, C = u32> | |
156 | AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> }, | |
157 | /// Fn(A, B) -> C | |
158 | Parenthesized { inputs: Vec<Type>, output: Option<Type> }, | |
159 | } | |
160 | ||
064997fb | 161 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 162 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
163 | pub enum GenericArg { |
164 | Lifetime(String), | |
165 | Type(Type), | |
166 | Const(Constant), | |
94222f64 | 167 | Infer, |
fc512014 XL |
168 | } |
169 | ||
064997fb | 170 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
171 | pub struct Constant { |
172 | #[serde(rename = "type")] | |
173 | pub type_: Type, | |
174 | pub expr: String, | |
175 | pub value: Option<String>, | |
176 | pub is_literal: bool, | |
177 | } | |
178 | ||
064997fb | 179 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
180 | pub struct TypeBinding { |
181 | pub name: String, | |
5e7ed085 | 182 | pub args: GenericArgs, |
fc512014 XL |
183 | pub binding: TypeBindingKind, |
184 | } | |
185 | ||
064997fb | 186 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 187 | #[serde(rename_all = "snake_case")] |
fc512014 | 188 | pub enum TypeBindingKind { |
5099ac24 | 189 | Equality(Term), |
fc512014 XL |
190 | Constraint(Vec<GenericBound>), |
191 | } | |
192 | ||
193 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] | |
194 | pub struct Id(pub String); | |
195 | ||
064997fb | 196 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 197 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
198 | pub enum ItemKind { |
199 | Module, | |
200 | ExternCrate, | |
201 | Import, | |
202 | Struct, | |
203 | StructField, | |
204 | Union, | |
205 | Enum, | |
206 | Variant, | |
207 | Function, | |
208 | Typedef, | |
209 | OpaqueTy, | |
210 | Constant, | |
211 | Trait, | |
212 | TraitAlias, | |
213 | Method, | |
214 | Impl, | |
215 | Static, | |
216 | ForeignType, | |
217 | Macro, | |
218 | ProcAttribute, | |
219 | ProcDerive, | |
220 | AssocConst, | |
221 | AssocType, | |
222 | Primitive, | |
223 | Keyword, | |
224 | } | |
225 | ||
064997fb | 226 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 227 | #[serde(tag = "kind", content = "inner", rename_all = "snake_case")] |
fc512014 | 228 | pub enum ItemEnum { |
6a06907d XL |
229 | Module(Module), |
230 | ExternCrate { | |
fc512014 XL |
231 | name: String, |
232 | rename: Option<String>, | |
233 | }, | |
6a06907d | 234 | Import(Import), |
fc512014 | 235 | |
6a06907d XL |
236 | Union(Union), |
237 | Struct(Struct), | |
238 | StructField(Type), | |
239 | Enum(Enum), | |
240 | Variant(Variant), | |
fc512014 | 241 | |
6a06907d | 242 | Function(Function), |
fc512014 | 243 | |
6a06907d XL |
244 | Trait(Trait), |
245 | TraitAlias(TraitAlias), | |
246 | Method(Method), | |
247 | Impl(Impl), | |
fc512014 | 248 | |
6a06907d XL |
249 | Typedef(Typedef), |
250 | OpaqueTy(OpaqueTy), | |
251 | Constant(Constant), | |
fc512014 | 252 | |
6a06907d | 253 | Static(Static), |
fc512014 XL |
254 | |
255 | /// `type`s from an extern block | |
6a06907d | 256 | ForeignType, |
fc512014 XL |
257 | |
258 | /// Declarative macro_rules! macro | |
6a06907d XL |
259 | Macro(String), |
260 | ProcMacro(ProcMacro), | |
fc512014 | 261 | |
2b03887a | 262 | Primitive(Primitive), |
c295e0f8 | 263 | |
6a06907d | 264 | AssocConst { |
fc512014 XL |
265 | #[serde(rename = "type")] |
266 | type_: Type, | |
267 | /// e.g. `const X: usize = 5;` | |
268 | default: Option<String>, | |
269 | }, | |
6a06907d | 270 | AssocType { |
5e7ed085 | 271 | generics: Generics, |
fc512014 XL |
272 | bounds: Vec<GenericBound>, |
273 | /// e.g. `type X = usize;` | |
274 | default: Option<Type>, | |
275 | }, | |
276 | } | |
277 | ||
064997fb | 278 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
279 | pub struct Module { |
280 | pub is_crate: bool, | |
281 | pub items: Vec<Id>, | |
064997fb FG |
282 | /// If `true`, this module is not part of the public API, but it contains |
283 | /// items that are re-exported as public API. | |
284 | pub is_stripped: bool, | |
fc512014 XL |
285 | } |
286 | ||
064997fb | 287 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
5869c6ff XL |
288 | pub struct Union { |
289 | pub generics: Generics, | |
290 | pub fields_stripped: bool, | |
291 | pub fields: Vec<Id>, | |
292 | pub impls: Vec<Id>, | |
293 | } | |
294 | ||
064997fb | 295 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 | 296 | pub struct Struct { |
f2b60f7d | 297 | pub kind: StructKind, |
fc512014 | 298 | pub generics: Generics, |
fc512014 XL |
299 | pub impls: Vec<Id>, |
300 | } | |
301 | ||
f2b60f7d FG |
302 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
303 | #[serde(rename_all = "snake_case")] | |
304 | pub enum StructKind { | |
305 | /// A struct with no fields and no parentheses. | |
306 | /// | |
307 | /// ```rust | |
308 | /// pub struct Unit; | |
309 | /// ``` | |
310 | Unit, | |
311 | /// A struct with unnamed fields. | |
312 | /// | |
313 | /// ```rust | |
314 | /// pub struct TupleStruct(i32); | |
315 | /// pub struct EmptyTupleStruct(); | |
316 | /// ``` | |
317 | /// | |
318 | /// All [`Id`]'s will point to [`ItemEnum::StructField`]. Private and | |
319 | /// `#[doc(hidden)]` fields will be given as `None` | |
320 | Tuple(Vec<Option<Id>>), | |
321 | /// A struct with nammed fields. | |
322 | /// | |
323 | /// ```rust | |
324 | /// pub struct PlainStruct { x: i32 } | |
325 | /// pub struct EmptyPlainStruct {} | |
326 | /// ``` | |
327 | Plain { fields: Vec<Id>, fields_stripped: bool }, | |
328 | } | |
329 | ||
064997fb | 330 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
331 | pub struct Enum { |
332 | pub generics: Generics, | |
333 | pub variants_stripped: bool, | |
334 | pub variants: Vec<Id>, | |
335 | pub impls: Vec<Id>, | |
336 | } | |
337 | ||
064997fb | 338 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
339 | #[serde(rename_all = "snake_case")] |
340 | #[serde(tag = "variant_kind", content = "variant_inner")] | |
fc512014 | 341 | pub enum Variant { |
f2b60f7d FG |
342 | /// A variant with no parentheses, and possible discriminant. |
343 | /// | |
344 | /// ```rust | |
345 | /// enum Demo { | |
346 | /// PlainVariant, | |
347 | /// PlainWithDiscriminant = 1, | |
348 | /// } | |
349 | /// ``` | |
350 | Plain(Option<Discriminant>), | |
351 | /// A variant with unnamed fields. | |
352 | /// | |
353 | /// Unlike most of json, `#[doc(hidden)]` fields will be given as `None` | |
354 | /// instead of being ommited, because order matters. | |
355 | /// | |
356 | /// ```rust | |
357 | /// enum Demo { | |
358 | /// TupleVariant(i32), | |
359 | /// EmptyTupleVariant(), | |
360 | /// } | |
361 | /// ``` | |
362 | Tuple(Vec<Option<Id>>), | |
363 | /// A variant with named fields. | |
364 | /// | |
365 | /// ```rust | |
366 | /// enum Demo { | |
367 | /// StructVariant { x: i32 }, | |
368 | /// EmptyStructVariant {}, | |
369 | /// } | |
370 | /// ``` | |
371 | Struct { fields: Vec<Id>, fields_stripped: bool }, | |
fc512014 XL |
372 | } |
373 | ||
064997fb | 374 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
f2b60f7d FG |
375 | pub struct Discriminant { |
376 | /// The expression that produced the discriminant. | |
377 | /// | |
378 | /// Unlike `value`, this preserves the original formatting (eg suffixes, | |
379 | /// hexadecimal, and underscores), making it unsuitable to be machine | |
380 | /// interpreted. | |
381 | /// | |
382 | /// In some cases, when the value is to complex, this may be `"{ _ }"`. | |
383 | /// When this occurs is unstable, and may change without notice. | |
384 | pub expr: String, | |
385 | /// The numerical value of the discriminant. Stored as a string due to | |
386 | /// JSON's poor support for large integers, and the fact that it would need | |
387 | /// to store from [`i128::MIN`] to [`u128::MAX`]. | |
388 | pub value: String, | |
fc512014 XL |
389 | } |
390 | ||
064997fb | 391 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
5e7ed085 FG |
392 | pub struct Header { |
393 | #[serde(rename = "const")] | |
394 | pub const_: bool, | |
395 | #[serde(rename = "unsafe")] | |
396 | pub unsafe_: bool, | |
397 | #[serde(rename = "async")] | |
398 | pub async_: bool, | |
399 | pub abi: Abi, | |
400 | } | |
401 | ||
064997fb | 402 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
5e7ed085 FG |
403 | pub enum Abi { |
404 | // We only have a concrete listing here for stable ABI's because their are so many | |
405 | // See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list | |
406 | Rust, | |
407 | C { unwind: bool }, | |
408 | Cdecl { unwind: bool }, | |
409 | Stdcall { unwind: bool }, | |
410 | Fastcall { unwind: bool }, | |
411 | Aapcs { unwind: bool }, | |
412 | Win64 { unwind: bool }, | |
413 | SysV64 { unwind: bool }, | |
414 | System { unwind: bool }, | |
415 | Other(String), | |
6a06907d XL |
416 | } |
417 | ||
064997fb | 418 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
419 | pub struct Function { |
420 | pub decl: FnDecl, | |
421 | pub generics: Generics, | |
5e7ed085 | 422 | pub header: Header, |
fc512014 XL |
423 | } |
424 | ||
064997fb | 425 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
426 | pub struct Method { |
427 | pub decl: FnDecl, | |
428 | pub generics: Generics, | |
5e7ed085 | 429 | pub header: Header, |
fc512014 XL |
430 | pub has_body: bool, |
431 | } | |
432 | ||
064997fb | 433 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
434 | pub struct Generics { |
435 | pub params: Vec<GenericParamDef>, | |
436 | pub where_predicates: Vec<WherePredicate>, | |
437 | } | |
438 | ||
064997fb | 439 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
440 | pub struct GenericParamDef { |
441 | pub name: String, | |
442 | pub kind: GenericParamDefKind, | |
443 | } | |
444 | ||
064997fb | 445 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 446 | #[serde(rename_all = "snake_case")] |
fc512014 | 447 | pub enum GenericParamDefKind { |
5e7ed085 FG |
448 | Lifetime { |
449 | outlives: Vec<String>, | |
450 | }, | |
451 | Type { | |
452 | bounds: Vec<GenericBound>, | |
453 | default: Option<Type>, | |
454 | /// This is normally `false`, which means that this generic parameter is | |
455 | /// declared in the Rust source text. | |
456 | /// | |
457 | /// If it is `true`, this generic parameter has been introduced by the | |
458 | /// compiler behind the scenes. | |
459 | /// | |
460 | /// # Example | |
461 | /// | |
462 | /// Consider | |
463 | /// | |
464 | /// ```ignore (pseudo-rust) | |
465 | /// pub fn f(_: impl Trait) {} | |
466 | /// ``` | |
467 | /// | |
468 | /// The compiler will transform this behind the scenes to | |
469 | /// | |
470 | /// ```ignore (pseudo-rust) | |
471 | /// pub fn f<impl Trait: Trait>(_: impl Trait) {} | |
472 | /// ``` | |
473 | /// | |
474 | /// In this example, the generic parameter named `impl Trait` (and which | |
475 | /// is bound by `Trait`) is synthetic, because it was not originally in | |
476 | /// the Rust source text. | |
477 | synthetic: bool, | |
478 | }, | |
479 | Const { | |
480 | #[serde(rename = "type")] | |
481 | type_: Type, | |
482 | default: Option<String>, | |
483 | }, | |
fc512014 XL |
484 | } |
485 | ||
064997fb | 486 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 487 | #[serde(rename_all = "snake_case")] |
fc512014 | 488 | pub enum WherePredicate { |
5e7ed085 FG |
489 | BoundPredicate { |
490 | #[serde(rename = "type")] | |
491 | type_: Type, | |
492 | bounds: Vec<GenericBound>, | |
923072b8 | 493 | /// Used for Higher-Rank Trait Bounds (HRTBs) |
f2b60f7d | 494 | /// ```text |
923072b8 FG |
495 | /// where for<'a> &'a T: Iterator," |
496 | /// ^^^^^^^ | |
497 | /// | | |
498 | /// this part | |
499 | /// ``` | |
500 | generic_params: Vec<GenericParamDef>, | |
5e7ed085 FG |
501 | }, |
502 | RegionPredicate { | |
503 | lifetime: String, | |
504 | bounds: Vec<GenericBound>, | |
505 | }, | |
506 | EqPredicate { | |
507 | lhs: Type, | |
508 | rhs: Term, | |
509 | }, | |
fc512014 XL |
510 | } |
511 | ||
064997fb | 512 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 513 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
514 | pub enum GenericBound { |
515 | TraitBound { | |
516 | #[serde(rename = "trait")] | |
f2b60f7d | 517 | trait_: Path, |
923072b8 | 518 | /// Used for Higher-Rank Trait Bounds (HRTBs) |
f2b60f7d | 519 | /// ```text |
923072b8 FG |
520 | /// where F: for<'a, 'b> Fn(&'a u8, &'b u8) |
521 | /// ^^^^^^^^^^^ | |
522 | /// | | |
523 | /// this part | |
524 | /// ``` | |
fc512014 XL |
525 | generic_params: Vec<GenericParamDef>, |
526 | modifier: TraitBoundModifier, | |
527 | }, | |
528 | Outlives(String), | |
529 | } | |
530 | ||
064997fb | 531 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 532 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
533 | pub enum TraitBoundModifier { |
534 | None, | |
535 | Maybe, | |
536 | MaybeConst, | |
537 | } | |
538 | ||
064997fb | 539 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
5099ac24 FG |
540 | #[serde(rename_all = "snake_case")] |
541 | pub enum Term { | |
542 | Type(Type), | |
543 | Constant(Constant), | |
544 | } | |
545 | ||
064997fb | 546 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
547 | #[serde(rename_all = "snake_case")] |
548 | #[serde(tag = "kind", content = "inner")] | |
fc512014 | 549 | pub enum Type { |
f2b60f7d FG |
550 | /// Structs, enums, and unions |
551 | ResolvedPath(Path), | |
552 | DynTrait(DynTrait), | |
fc512014 XL |
553 | /// Parameterized types |
554 | Generic(String), | |
f2b60f7d | 555 | /// Built in numberic (i*, u*, f*) types, bool, and char |
fc512014 XL |
556 | Primitive(String), |
557 | /// `extern "ABI" fn` | |
558 | FunctionPointer(Box<FunctionPointer>), | |
559 | /// `(String, u32, Box<usize>)` | |
560 | Tuple(Vec<Type>), | |
561 | /// `[u32]` | |
562 | Slice(Box<Type>), | |
563 | /// [u32; 15] | |
564 | Array { | |
565 | #[serde(rename = "type")] | |
566 | type_: Box<Type>, | |
567 | len: String, | |
568 | }, | |
569 | /// `impl TraitA + TraitB + ...` | |
570 | ImplTrait(Vec<GenericBound>), | |
fc512014 XL |
571 | /// `_` |
572 | Infer, | |
573 | /// `*mut u32`, `*u8`, etc. | |
574 | RawPointer { | |
575 | mutable: bool, | |
576 | #[serde(rename = "type")] | |
577 | type_: Box<Type>, | |
578 | }, | |
579 | /// `&'a mut String`, `&str`, etc. | |
580 | BorrowedRef { | |
581 | lifetime: Option<String>, | |
582 | mutable: bool, | |
583 | #[serde(rename = "type")] | |
584 | type_: Box<Type>, | |
585 | }, | |
586 | /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator` | |
587 | QualifiedPath { | |
588 | name: String, | |
5e7ed085 | 589 | args: Box<GenericArgs>, |
fc512014 XL |
590 | self_type: Box<Type>, |
591 | #[serde(rename = "trait")] | |
f2b60f7d | 592 | trait_: Path, |
fc512014 XL |
593 | }, |
594 | } | |
595 | ||
f2b60f7d FG |
596 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
597 | pub struct Path { | |
598 | pub name: String, | |
599 | pub id: Id, | |
600 | /// Generic arguments to the type | |
601 | /// ```test | |
602 | /// std::borrow::Cow<'static, str> | |
603 | /// ^^^^^^^^^^^^^^ | |
604 | /// | | |
605 | /// this part | |
606 | /// ``` | |
607 | pub args: Option<Box<GenericArgs>>, | |
608 | } | |
609 | ||
064997fb | 610 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 | 611 | pub struct FunctionPointer { |
fc512014 | 612 | pub decl: FnDecl, |
923072b8 | 613 | /// Used for Higher-Rank Trait Bounds (HRTBs) |
f2b60f7d | 614 | /// ```text |
923072b8 FG |
615 | /// for<'c> fn(val: &'c i32) -> i32 |
616 | /// ^^^^^^^ | |
617 | /// | | |
618 | /// this part | |
619 | /// ``` | |
6a06907d | 620 | pub generic_params: Vec<GenericParamDef>, |
5e7ed085 | 621 | pub header: Header, |
fc512014 XL |
622 | } |
623 | ||
064997fb | 624 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
625 | pub struct FnDecl { |
626 | pub inputs: Vec<(String, Type)>, | |
627 | pub output: Option<Type>, | |
628 | pub c_variadic: bool, | |
629 | } | |
630 | ||
064997fb | 631 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
632 | pub struct Trait { |
633 | pub is_auto: bool, | |
634 | pub is_unsafe: bool, | |
635 | pub items: Vec<Id>, | |
636 | pub generics: Generics, | |
637 | pub bounds: Vec<GenericBound>, | |
5e7ed085 | 638 | pub implementations: Vec<Id>, |
fc512014 XL |
639 | } |
640 | ||
064997fb | 641 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
642 | pub struct TraitAlias { |
643 | pub generics: Generics, | |
644 | pub params: Vec<GenericBound>, | |
645 | } | |
646 | ||
064997fb | 647 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
648 | pub struct Impl { |
649 | pub is_unsafe: bool, | |
650 | pub generics: Generics, | |
651 | pub provided_trait_methods: Vec<String>, | |
652 | #[serde(rename = "trait")] | |
f2b60f7d | 653 | pub trait_: Option<Path>, |
fc512014 XL |
654 | #[serde(rename = "for")] |
655 | pub for_: Type, | |
656 | pub items: Vec<Id>, | |
657 | pub negative: bool, | |
658 | pub synthetic: bool, | |
659 | pub blanket_impl: Option<Type>, | |
660 | } | |
661 | ||
064997fb | 662 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 663 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
664 | pub struct Import { |
665 | /// The full path being imported. | |
cdc7bbd5 | 666 | pub source: String, |
fc512014 XL |
667 | /// May be different from the last segment of `source` when renaming imports: |
668 | /// `use source as name;` | |
669 | pub name: String, | |
f2b60f7d FG |
670 | /// The ID of the item being imported. Will be `None` in case of re-exports of primitives: |
671 | /// ```rust | |
672 | /// pub use i32 as my_i32; | |
673 | /// ``` | |
674 | pub id: Option<Id>, | |
fc512014 XL |
675 | /// Whether this import uses a glob: `use source::*;` |
676 | pub glob: bool, | |
677 | } | |
678 | ||
064997fb | 679 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
680 | pub struct ProcMacro { |
681 | pub kind: MacroKind, | |
682 | pub helpers: Vec<String>, | |
683 | } | |
684 | ||
064997fb | 685 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
6a06907d | 686 | #[serde(rename_all = "snake_case")] |
fc512014 XL |
687 | pub enum MacroKind { |
688 | /// A bang macro `foo!()`. | |
689 | Bang, | |
690 | /// An attribute macro `#[foo]`. | |
691 | Attr, | |
064997fb | 692 | /// A derive macro `#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]` |
fc512014 XL |
693 | Derive, |
694 | } | |
695 | ||
064997fb | 696 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
697 | pub struct Typedef { |
698 | #[serde(rename = "type")] | |
699 | pub type_: Type, | |
700 | pub generics: Generics, | |
701 | } | |
702 | ||
064997fb | 703 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
704 | pub struct OpaqueTy { |
705 | pub bounds: Vec<GenericBound>, | |
706 | pub generics: Generics, | |
707 | } | |
708 | ||
064997fb | 709 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
fc512014 XL |
710 | pub struct Static { |
711 | #[serde(rename = "type")] | |
712 | pub type_: Type, | |
713 | pub mutable: bool, | |
714 | pub expr: String, | |
715 | } | |
6a06907d | 716 | |
2b03887a FG |
717 | #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |
718 | pub struct Primitive { | |
719 | pub name: String, | |
720 | pub impls: Vec<Id>, | |
721 | } | |
722 | ||
6a06907d XL |
723 | #[cfg(test)] |
724 | mod tests; |