1pub mod audit;
19pub mod driver_hash;
20pub mod format;
21pub(crate) mod paths;
22pub mod reader;
23pub mod ty_arena;
24pub mod workspace;
25pub mod writer;
26
27pub use format::*;
28pub use paths::{canonicalize_best_effort, canonicalize_cached, normalize_path};
29pub use reader::{IndexReader, MacroDefView, ResolvedDefinition};
30pub use ty_arena::{
31 AssocBindingIter, AssocBindingView, AssocTyIter, AssocTyView, DefTyView, FieldIter, FieldView, FnSigView, GenericArgIter, GenericArgView, GenericParamIter, GenericParamView,
32 ImplIter, ImplView, LangItemIter, LangItemView, RawAssocTy, RawDefTy, RawField, RawFnSig, RawGenericArg, RawGenericParam, RawImpl, RawLangItem, RawTy, RawTyBound, RawTyDef,
33 RawUse, RawWherePred, TyArenaView, TyBoundIter, TyBoundView, TyDefView, TyInterner, TyKindView, TyListIter, U32ListIter, UseIter, UseView, WherePredIter, WherePredView,
34};
35pub use workspace::*;
36pub use writer::{IndexWriter, RawDefinition, RawMacroDef, RawNote, RawReference, RawType};
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41 use std::io::Cursor;
42
43 #[test]
44 fn test_roundtrip_basic() {
45 let mut writer = IndexWriter::new("test_crate", "0.1.0");
46
47 writer.add_definition(RawDefinition {
49 def_id: 100,
50 kind: DefKind::Fn,
51 visibility: 0,
52 is_macro_expansion: false,
53 name: "calculate".to_string(),
54 path: "test_crate::math::calculate".to_string(),
55 signature: Some("fn(i32, i32) -> i32".to_string()),
56 file: "src/math.rs".to_string(),
57 start_line: 10,
58 start_col: 1,
59 end_line: 10,
60 end_col: 30,
61 });
62
63 writer.add_definition(RawDefinition {
64 def_id: 101,
65 kind: DefKind::Struct,
66 visibility: 0,
67 is_macro_expansion: false,
68 name: "Calculator".to_string(),
69 path: "test_crate::math::Calculator".to_string(),
70 signature: None,
71 file: "src/math.rs".to_string(),
72 start_line: 5,
73 start_col: 1,
74 end_line: 8,
75 end_col: 2,
76 });
77
78 writer.add_reference(RawReference {
80 file: "src/main.rs".to_string(),
81 start_line: 15,
82 start_col: 18,
83 end_line: 15,
84 end_col: 27,
85 target_def_id: Some(100),
86 target_path: "test_crate::math::calculate".to_string(),
87 target_unit: None,
88 target_file: Some("src/math.rs".to_string()),
89 target_line: 10,
90 target_col: 1,
91 kind: DefKind::Fn,
92 is_write: false,
93 is_macro_expansion: false,
94 });
95
96 writer.add_type(RawType { file: "src/main.rs".to_string(), start_line: 15, start_col: 18, end_line: 15, end_col: 35, type_name: "i32".to_string() });
98
99 writer.add_type(RawType {
100 file: "src/main.rs".to_string(),
101 start_line: 15,
102 start_col: 5,
103 end_line: 15,
104 end_col: 36,
105 type_name: "std::result::Result<i32, String>".to_string(),
106 });
107
108 let mut buffer = Cursor::new(Vec::new());
109 writer.write_to(&mut buffer).expect("write failed");
110 let bytes = buffer.into_inner();
111
112 let reader = IndexReader::from_buffer(&bytes).expect("reader parse failed");
113 assert_eq!(reader.crate_name(), "test_crate");
114 assert_eq!(reader.crate_version(), "0.1.0");
115
116 let def = reader.find_definition_by_path("test_crate::math::calculate").expect("def not found");
118 assert_eq!(reader.get_str(def.name_offset), "calculate");
119 assert_eq!(reader.get_str(def.sig_offset), "fn(i32, i32) -> i32");
120
121 let target = reader.find_definition_at("src/main.rs", 15, 20).expect("ref definition not found");
123 assert_eq!(target.name, "calculate");
124 assert_eq!(target.path, "test_crate::math::calculate");
125 assert_eq!(target.file, "src/math.rs");
126 assert_eq!(target.start_line, 10);
127 assert_eq!(target.signature, Some("fn(i32, i32) -> i32"));
128
129 let refs = reader.find_references("test_crate::math::calculate");
131 assert_eq!(refs.len(), 1);
132 assert_eq!(reader.get_file(refs[0].span.file_id), "src/main.rs");
133 assert_eq!(refs[0].span.start_line, 15);
134
135 let ty_inner = reader.type_at("src/main.rs", 15, 22).expect("type not found");
137 assert_eq!(ty_inner, "i32");
138
139 let ty_outer = reader.type_at("src/main.rs", 15, 10).expect("type not found");
140 assert_eq!(ty_outer, "std::result::Result<i32, String>");
141 }
142
143 #[test]
144 fn test_string_deduplication() {
145 let mut writer = IndexWriter::new("dedup_crate", "0.1.0");
146 let off1 = writer.intern_string("std::option::Option<T>");
147 let off2 = writer.intern_string("std::option::Option<T>");
148 let off3 = writer.intern_string("std::option::Option<T>");
149 assert_eq!(off1, off2);
150 assert_eq!(off2, off3);
151 }
152
153 #[test]
154 fn test_structural_validation_guards() {
155 let small_buf = vec![0u8; 64];
157 assert!(IndexReader::from_buffer(&small_buf).is_err());
158
159 let mut bad_magic = vec![0u8; std::mem::size_of::<Header>()];
161 bad_magic[0..4].copy_from_slice(b"NOPE");
162 assert!(IndexReader::from_buffer(&bad_magic).is_err());
163
164 let mut writer = IndexWriter::new("test", "0.1.0");
166 let mut buf = std::io::Cursor::new(Vec::new());
167 writer.write_to(&mut buf).unwrap();
168 let mut bytes = buf.into_inner();
169
170 let bad_offset = (bytes.len() as u64) + 10000;
172 bytes[32..40].copy_from_slice(&bad_offset.to_le_bytes());
174 assert!(IndexReader::from_buffer(&bytes).is_err());
175 }
176}