use std::path::PathBuf;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Crate {
pub root: Id,
pub crate_version: Option<String>,
pub includes_private: bool,
pub index: HashMap<Id, Item>,
pub paths: HashMap<Id, ItemSummary>,
pub external_crates: HashMap<u32, ExternalCrate>,
pub format_version: u32,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExternalCrate {
pub name: String,
pub html_root_url: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ItemSummary {
pub crate_id: u32,
pub path: Vec<String>,
pub kind: ItemKind,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Item {
pub id: Id,
pub crate_id: u32,
pub name: Option<String>,
pub source: Option<Span>,
pub visibility: Visibility,
pub docs: String,
pub links: HashMap<String, Id>,
pub attrs: Vec<String>,
pub deprecation: Option<Deprecation>,
pub kind: ItemKind,
pub inner: ItemEnum,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Span {
pub filename: PathBuf,
pub begin: (usize, usize),
pub end: (usize, usize),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Deprecation {
pub since: Option<String>,
pub note: Option<String>,
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Visibility {
Public,
Default,
Crate,
Restricted {
parent: Id,
path: String,
},
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum GenericArgs {
AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
Parenthesized { inputs: Vec<Type>, output: Option<Type> },
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum GenericArg {
Lifetime(String),
Type(Type),
Const(Constant),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Constant {
#[serde(rename = "type")]
pub type_: Type,
pub expr: String,
pub value: Option<String>,
pub is_literal: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TypeBinding {
pub name: String,
pub binding: TypeBindingKind,
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TypeBindingKind {
Equality(Type),
Constraint(Vec<GenericBound>),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Id(pub String);
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ItemKind {
Module,
ExternCrate,
Import,
Struct,
StructField,
Union,
Enum,
Variant,
Function,
Typedef,
OpaqueTy,
Constant,
Trait,
TraitAlias,
Method,
Impl,
Static,
ForeignType,
Macro,
ProcAttribute,
ProcDerive,
AssocConst,
AssocType,
Primitive,
Keyword,
}
#[serde(untagged)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ItemEnum {
ModuleItem(Module),
ExternCrateItem {
name: String,
rename: Option<String>,
},
ImportItem(Import),
StructItem(Struct),
StructFieldItem(Type),
EnumItem(Enum),
VariantItem(Variant),
FunctionItem(Function),
TraitItem(Trait),
TraitAliasItem(TraitAlias),
MethodItem(Method),
ImplItem(Impl),
TypedefItem(Typedef),
OpaqueTyItem(OpaqueTy),
ConstantItem(Constant),
StaticItem(Static),
ForeignTypeItem,
MacroItem(String),
ProcMacroItem(ProcMacro),
AssocConstItem {
#[serde(rename = "type")]
type_: Type,
default: Option<String>,
},
AssocTypeItem {
bounds: Vec<GenericBound>,
default: Option<Type>,
},
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Module {
pub is_crate: bool,
pub items: Vec<Id>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Struct {
pub struct_type: StructType,
pub generics: Generics,
pub fields_stripped: bool,
pub fields: Vec<Id>,
pub impls: Vec<Id>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Enum {
pub generics: Generics,
pub variants_stripped: bool,
pub variants: Vec<Id>,
pub impls: Vec<Id>,
}
#[serde(rename_all = "snake_case")]
#[serde(tag = "variant_kind", content = "variant_inner")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Variant {
Plain,
Tuple(Vec<Type>),
Struct(Vec<Id>),
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum StructType {
Plain,
Tuple,
Unit,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Function {
pub decl: FnDecl,
pub generics: Generics,
pub header: String,
pub abi: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Method {
pub decl: FnDecl,
pub generics: Generics,
pub header: String,
pub has_body: bool,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Generics {
pub params: Vec<GenericParamDef>,
pub where_predicates: Vec<WherePredicate>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GenericParamDef {
pub name: String,
pub kind: GenericParamDefKind,
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum GenericParamDefKind {
Lifetime,
Type { bounds: Vec<GenericBound>, default: Option<Type> },
Const(Type),
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum WherePredicate {
BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
EqPredicate { lhs: Type, rhs: Type },
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum GenericBound {
TraitBound {
#[serde(rename = "trait")]
trait_: Type,
generic_params: Vec<GenericParamDef>,
modifier: TraitBoundModifier,
},
Outlives(String),
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TraitBoundModifier {
None,
Maybe,
MaybeConst,
}
#[serde(rename_all = "snake_case")]
#[serde(tag = "kind", content = "inner")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Type {
ResolvedPath {
name: String,
id: Id,
args: Option<Box<GenericArgs>>,
param_names: Vec<GenericBound>,
},
Generic(String),
Primitive(String),
FunctionPointer(Box<FunctionPointer>),
Tuple(Vec<Type>),
Slice(Box<Type>),
Array {
#[serde(rename = "type")]
type_: Box<Type>,
len: String,
},
ImplTrait(Vec<GenericBound>),
Never,
Infer,
RawPointer {
mutable: bool,
#[serde(rename = "type")]
type_: Box<Type>,
},
BorrowedRef {
lifetime: Option<String>,
mutable: bool,
#[serde(rename = "type")]
type_: Box<Type>,
},
QualifiedPath {
name: String,
self_type: Box<Type>,
#[serde(rename = "trait")]
trait_: Box<Type>,
},
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FunctionPointer {
pub is_unsafe: bool,
pub generic_params: Vec<GenericParamDef>,
pub decl: FnDecl,
pub abi: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FnDecl {
pub inputs: Vec<(String, Type)>,
pub output: Option<Type>,
pub c_variadic: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Trait {
pub is_auto: bool,
pub is_unsafe: bool,
pub items: Vec<Id>,
pub generics: Generics,
pub bounds: Vec<GenericBound>,
pub implementors: Vec<Id>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TraitAlias {
pub generics: Generics,
pub params: Vec<GenericBound>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Impl {
pub is_unsafe: bool,
pub generics: Generics,
pub provided_trait_methods: Vec<String>,
#[serde(rename = "trait")]
pub trait_: Option<Type>,
#[serde(rename = "for")]
pub for_: Type,
pub items: Vec<Id>,
pub negative: bool,
pub synthetic: bool,
pub blanket_impl: Option<Type>,
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Import {
pub span: String,
pub name: String,
pub id: Option<Id>,
pub glob: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProcMacro {
pub kind: MacroKind,
pub helpers: Vec<String>,
}
#[serde(rename_all = "snake_case")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum MacroKind {
Bang,
Attr,
Derive,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Typedef {
#[serde(rename = "type")]
pub type_: Type,
pub generics: Generics,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OpaqueTy {
pub bounds: Vec<GenericBound>,
pub generics: Generics,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Static {
#[serde(rename = "type")]
pub type_: Type,
pub mutable: bool,
pub expr: String,
}