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::KindSpec;
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	extract_sdk(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	sdk_pipeline::extract(uri, source, anchor, deep, presets)
45}
46
47pub struct Lang;
48
49const DEF_KINDS: &[&str] = &[
50	"struct",
51	"enum",
52	"enum_constant",
53	"trait",
54	"impl",
55	"fn",
56	"macro",
57	"method",
58	"test",
59	"const",
60	"static",
61	"path",
62	"type",
63];
64
65const DEF_KIND_SPECS: &[KindSpec] = &[
66	KindSpec::new("impl", Shape::Namespace, 10, "impl"),
67	KindSpec::new("struct", Shape::Type, 20, "struct"),
68	KindSpec::new("enum", Shape::Type, 21, "enum"),
69	KindSpec::new("trait", Shape::Type, 22, "trait"),
70	KindSpec::new("type", Shape::Type, 23, "type"),
71	KindSpec::new("fn", Shape::Callable, 40, "fn"),
72	KindSpec::new("macro", Shape::Callable, 41, "macro"),
73	KindSpec::new("method", Shape::Callable, 42, "method"),
74	KindSpec::new("test", Shape::Callable, 43, "test"),
75	KindSpec::new("enum_constant", Shape::Value, 60, "enum_constant"),
76	KindSpec::new("const", Shape::Value, 61, "const"),
77	KindSpec::new("static", Shape::Value, 62, "static"),
78	KindSpec::new("path", Shape::Value, 63, "path"),
79];
80
81impl crate::lang::LangExtractor for Lang {
82	type Presets = Presets;
83	const LANG_TAG: &'static str = "rs";
84	const ALLOWED_KINDS: &'static [&'static str] = DEF_KINDS;
85	const KIND_SPECS: &'static [KindSpec] = DEF_KIND_SPECS;
86	const ALLOWED_VISIBILITIES: &'static [&'static str] = &["public", "private", "module"];
87
88	fn extract(
89		uri: &str,
90		source: &str,
91		anchor: &Moniker,
92		deep: bool,
93		presets: &Self::Presets,
94	) -> CodeGraph {
95		extract(uri, source, anchor, deep, presets)
96	}
97}