Skip to main content

code_moniker_core/lang/rs/
mod.rs

1use tree_sitter::{Language, Parser, Tree};
2
3use crate::core::code_graph::CodeGraph;
4use crate::core::moniker::Moniker;
5use crate::core::shape::Shape;
6
7use crate::lang::{ExtractionContext, KindSpec, LangExtractor, ParsedDocument};
8
9pub mod cargo_manifest;
10mod kinds;
11mod sdk_pipeline;
12
13pub fn parse(source: &str) -> Tree {
14	let mut parser = Parser::new();
15	let language: Language = tree_sitter_rust::LANGUAGE.into();
16	parser.set_language(&language).unwrap_or_else(|err| {
17		panic!("failed to load tree-sitter Rust grammar: {err}");
18	});
19	parser.parse(source, None).unwrap_or_else(|| {
20		panic!("tree-sitter parse returned None on a non-cancelled call");
21	})
22}
23
24#[derive(Clone, Debug, Default)]
25pub struct Presets {}
26
27pub fn extract(
28	uri: &str,
29	source: &str,
30	anchor: &Moniker,
31	deep: bool,
32	presets: &Presets,
33) -> CodeGraph {
34	<Lang as LangExtractor>::extract(uri, source, anchor, deep, presets)
35}
36
37pub fn extract_sdk(
38	uri: &str,
39	source: &str,
40	anchor: &Moniker,
41	deep: bool,
42	presets: &Presets,
43) -> CodeGraph {
44	<Lang as LangExtractor>::extract(uri, source, anchor, deep, presets)
45}
46
47pub fn is_common_std_method(name: &str) -> bool {
48	sdk_pipeline::is_common_std_method(name.as_bytes())
49}
50
51pub struct Lang;
52
53const DEF_KINDS: &[&str] = &[
54	"struct",
55	"enum",
56	"enum_constant",
57	"trait",
58	"impl",
59	"fn",
60	"macro",
61	"method",
62	"test",
63	"const",
64	"static",
65	"field",
66	"path",
67	"type",
68];
69
70const DEF_KIND_SPECS: &[KindSpec] = &[
71	KindSpec::new("impl", Shape::Namespace, 10, "impl"),
72	KindSpec::new("struct", Shape::Type, 20, "struct"),
73	KindSpec::new("enum", Shape::Type, 21, "enum"),
74	KindSpec::new("trait", Shape::Type, 22, "trait"),
75	KindSpec::new("type", Shape::Type, 23, "type"),
76	KindSpec::new("fn", Shape::Callable, 40, "fn"),
77	KindSpec::new("macro", Shape::Callable, 41, "macro"),
78	KindSpec::new("method", Shape::Callable, 42, "method"),
79	KindSpec::new("test", Shape::Callable, 43, "test"),
80	KindSpec::new("enum_constant", Shape::Value, 60, "enum_constant"),
81	KindSpec::new("const", Shape::Value, 61, "const"),
82	KindSpec::new("static", Shape::Value, 62, "static"),
83	KindSpec::new("field", Shape::Value, 63, "field"),
84	KindSpec::new("path", Shape::Value, 64, "path"),
85];
86
87impl crate::lang::LangExtractor for Lang {
88	type Presets = Presets;
89	const LANG_TAG: &'static str = "rs";
90	const ALLOWED_KINDS: &'static [&'static str] = DEF_KINDS;
91	const KIND_SPECS: &'static [KindSpec] = DEF_KIND_SPECS;
92	const ALLOWED_VISIBILITIES: &'static [&'static str] = &["public", "private", "module"];
93
94	fn parse(_uri: &str, source: &str) -> ParsedDocument {
95		ParsedDocument::new(parse(source))
96	}
97
98	fn file_root(uri: &str, anchor: &Moniker) -> Option<Moniker> {
99		Some(sdk_pipeline::compute_module_moniker(anchor, uri))
100	}
101
102	fn extract_parsed(
103		context: ExtractionContext<'_, Self::Presets>,
104		document: &ParsedDocument,
105	) -> CodeGraph {
106		sdk_pipeline::extract(
107			context.uri,
108			context.source,
109			document,
110			context.anchor,
111			context.deep,
112			context.presets,
113		)
114	}
115}