rls_data/
lib.rs

1use rls_span as span;
2
3use std::path::PathBuf;
4
5#[cfg(feature = "derive")]
6use serde::{Deserialize, Serialize};
7
8pub mod config;
9pub use config::Config;
10
11#[derive(Debug, Clone, Default)]
12#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
13#[repr(C)]
14pub struct Analysis {
15    /// The Config used to generate this analysis data.
16    pub config: Config,
17    pub version: Option<String>,
18    pub compilation: Option<CompilationOptions>,
19    pub prelude: Option<CratePreludeData>,
20    pub imports: Vec<Import>,
21    pub defs: Vec<Def>,
22    pub impls: Vec<Impl>,
23    pub refs: Vec<Ref>,
24    pub macro_refs: Vec<MacroRef>,
25    pub relations: Vec<Relation>,
26}
27
28impl Analysis {
29    /// Returns an initialized `Analysis` struct with `config` and also
30    /// `version` field to Cargo package version.
31    pub fn new(config: Config) -> Analysis {
32        Analysis {
33            config,
34            version: option_env!("CARGO_PKG_VERSION").map(ToString::to_string),
35            ..Analysis::default()
36        }
37    }
38}
39
40// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
41// we use our own Id which is the same, but without the newtype.
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
43#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
44pub struct Id {
45    pub krate: u32,
46    pub index: u32,
47}
48
49/// Crate name, along with its disambiguator (128-bit hash) represents a globally
50/// unique crate identifier, which should allow for differentiation between
51/// different crate targets or versions and should point to the same crate when
52/// pulled by different other, dependent crates.
53#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
55pub struct GlobalCrateId {
56    pub name: String,
57    pub disambiguator: (u64, u64),
58}
59
60#[derive(Debug, Clone)]
61#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
62pub struct SpanData {
63    pub file_name: PathBuf,
64    pub byte_start: u32,
65    pub byte_end: u32,
66    pub line_start: span::Row<span::OneIndexed>,
67    pub line_end: span::Row<span::OneIndexed>,
68    // Character offset.
69    pub column_start: span::Column<span::OneIndexed>,
70    pub column_end: span::Column<span::OneIndexed>,
71}
72
73#[derive(Debug, Clone)]
74#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
75pub struct CompilationOptions {
76    pub directory: PathBuf,
77    pub program: String,
78    pub arguments: Vec<String>,
79    pub output: PathBuf,
80}
81
82#[derive(Debug, Clone)]
83#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
84pub struct CratePreludeData {
85    pub crate_id: GlobalCrateId,
86    pub crate_root: String,
87    pub external_crates: Vec<ExternalCrateData>,
88    pub span: SpanData,
89}
90
91/// Data for external crates in the prelude of a crate.
92#[derive(Debug, Clone)]
93#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
94pub struct ExternalCrateData {
95    /// Source file where the external crate is declared.
96    pub file_name: String,
97    /// A crate-local crate index of an external crate. Local crate index is
98    /// always 0, so these should start from 1 and range should be contiguous,
99    /// e.g. from 1 to n for n external crates.
100    pub num: u32,
101    pub id: GlobalCrateId,
102}
103
104#[derive(Debug, Clone)]
105#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
106pub struct Import {
107    pub kind: ImportKind,
108    pub ref_id: Option<Id>,
109    pub span: SpanData,
110    pub alias_span: Option<SpanData>,
111    pub name: String,
112    pub value: String,
113    pub parent: Option<Id>,
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
117#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
118pub enum ImportKind {
119    ExternCrate,
120    Use,
121    GlobUse,
122}
123
124#[derive(Debug, Clone)]
125#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
126pub struct Def {
127    pub kind: DefKind,
128    pub id: Id,
129    pub span: SpanData,
130    pub name: String,
131    pub qualname: String,
132    pub value: String,
133    pub parent: Option<Id>,
134    pub children: Vec<Id>,
135    pub decl_id: Option<Id>,
136    pub docs: String,
137    pub sig: Option<Signature>,
138    pub attributes: Vec<Attribute>,
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
143pub enum DefKind {
144    // value = variant names
145    Enum,
146    // value = enum name + variant name + types
147    TupleVariant,
148    // value = enum name + name + fields
149    StructVariant,
150    // value = variant name + types
151    Tuple,
152    // value = name + fields
153    Struct,
154    Union,
155    // value = signature
156    Trait,
157    // value = type + generics
158    Function,
159    ForeignFunction,
160    // value = type + generics
161    Method,
162    // No id, no value.
163    Macro,
164    // value = file_name
165    Mod,
166    // value = aliased type
167    Type,
168    // value = type and init expression (for all variable kinds).
169    Local,
170    Static,
171    ForeignStatic,
172    Const,
173    Field,
174    // no value
175    ExternType,
176}
177
178#[derive(Debug, Clone)]
179#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
180pub struct Impl {
181    pub id: u32,
182    pub kind: ImplKind,
183    pub span: SpanData,
184    pub value: String,
185    pub parent: Option<Id>,
186    pub children: Vec<Id>,
187    pub docs: String,
188    pub sig: Option<Signature>,
189    pub attributes: Vec<Attribute>,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
193#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
194pub enum ImplKind {
195    // impl Foo { ... }
196    Inherent,
197    // impl Bar for Foo { ... }
198    Direct,
199    // impl Bar for &Foo { ... }
200    Indirect,
201    // impl<T: Baz> Bar for T { ... }
202    //   where Foo: Baz
203    Blanket,
204    // impl Bar for Baz { ... } or impl Baz { ... }, etc.
205    //   where Foo: Deref<Target = Baz>
206    // Args are name and id of Baz
207    Deref(String, Id),
208}
209
210#[derive(Debug, Clone)]
211#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
212pub struct Attribute {
213    pub value: String,
214    pub span: SpanData,
215}
216
217#[derive(Debug, Clone)]
218#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
219pub struct Ref {
220    pub kind: RefKind,
221    pub span: SpanData,
222    pub ref_id: Id,
223}
224
225#[derive(Debug, Clone, Copy, PartialEq, Eq)]
226#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
227pub enum RefKind {
228    Function,
229    Mod,
230    Type,
231    Variable,
232}
233
234#[derive(Debug, Clone)]
235#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
236pub struct MacroRef {
237    pub span: SpanData,
238    pub qualname: String,
239    pub callee_span: SpanData,
240}
241
242#[derive(Debug, Clone)]
243#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
244pub struct Relation {
245    pub span: SpanData,
246    pub kind: RelationKind,
247    pub from: Id,
248    pub to: Id,
249}
250
251#[derive(Debug, Clone, Copy, PartialEq, Eq)]
252#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
253pub enum RelationKind {
254    Impl { id: u32 },
255    SuperTrait,
256}
257
258#[derive(Debug, Clone)]
259#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
260pub struct Signature {
261    pub text: String,
262    pub defs: Vec<SigElement>,
263    pub refs: Vec<SigElement>,
264}
265
266#[derive(Debug, Clone)]
267#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
268pub struct SigElement {
269    pub id: Id,
270    pub start: usize,
271    pub end: usize,
272}