1use std::path::Path;
6
7use serde::{Deserialize, Serialize};
8
9pub mod c;
10pub mod go;
11pub mod java;
12pub mod python;
13pub mod ruby;
14pub mod syntax;
15
16#[derive(
17 Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum,
18)]
19#[serde(rename_all = "lowercase")]
20pub enum Lang {
21 #[default]
22 Ts,
23 Python,
24 C,
25 Go,
26 Ruby,
27 Java,
28}
29
30impl Lang {
31 pub fn name(self) -> &'static str {
32 match self {
33 Self::Ts => "TypeScript/JavaScript",
34 Self::Python => "Python",
35 Self::C => "C",
36 Self::Go => "Go",
37 Self::Ruby => "Ruby",
38 Self::Java => "Java",
39 }
40 }
41
42 pub fn detect(root: &Path) -> Self {
44 let has = |name: &str| root.join(name).exists();
45 if has("go.mod") {
46 return Self::Go;
47 }
48 if has("pom.xml") || has("build.gradle") || has("build.gradle.kts") {
49 return Self::Java;
50 }
51 if has("pyproject.toml") || has("setup.py") || has("setup.cfg") {
52 return Self::Python;
53 }
54 if has("Gemfile") {
55 return Self::Ruby;
56 }
57 if has("package.json") {
58 return Self::Ts;
59 }
60 let mut counts = [0usize; 6];
61 for entry in ignore::WalkBuilder::new(root)
62 .hidden(true)
63 .require_git(false)
64 .build()
65 .flatten()
66 {
67 let ext = entry
68 .path()
69 .extension()
70 .and_then(|e| e.to_str())
71 .unwrap_or("");
72 let i = match ext {
73 "ts" | "tsx" | "js" | "mjs" | "cjs" | "jsx" => 0,
74 "py" => 1,
75 "c" | "h" => 2,
76 "go" => 3,
77 "rb" => 4,
78 "java" => 5,
79 _ => continue,
80 };
81 counts[i] += 1;
82 }
83 let best = (0..6).max_by_key(|&i| counts[i]).unwrap_or(0);
84 [
85 Self::Ts,
86 Self::Python,
87 Self::C,
88 Self::Go,
89 Self::Ruby,
90 Self::Java,
91 ][best]
92 }
93}
94
95pub enum Resolution {
97 File(usize),
98 External(String),
99 Stdlib,
100 Unresolved,
101}
102
103pub fn discover(lang: Lang, root: &Path) -> anyhow::Result<crate::ts::discover::Workspace> {
104 match lang {
105 Lang::Python => python::discover(root),
106 Lang::C => c::discover(root),
107 Lang::Go => go::discover(root),
108 Lang::Ruby => ruby::discover(root),
109 Lang::Java => java::discover(root),
110 _ => anyhow::bail!("{} is not supported yet", lang.name()),
111 }
112}
113
114pub fn scan(lang: Lang, _path: &str, text: &str) -> crate::ts::scan::FileFacts {
115 match lang {
116 Lang::Python => python::scan(text),
117 Lang::C => c::scan(text),
118 Lang::Go => go::scan(text),
119 Lang::Ruby => ruby::scan(text),
120 Lang::Java => java::scan(text),
121 _ => crate::ts::scan::FileFacts::default(),
122 }
123}
124
125pub fn resolve(
126 lang: Lang,
127 from: &str,
128 spec: &str,
129 index: &std::collections::HashMap<&str, usize>,
130 ws: &crate::ts::discover::Workspace,
131) -> Resolution {
132 match lang {
133 Lang::Python => match python::resolve(from, spec, index, ws) {
134 python::Resolution::File(i) => Resolution::File(i),
135 python::Resolution::External(n) => Resolution::External(n),
136 python::Resolution::Stdlib => Resolution::Stdlib,
137 python::Resolution::Unresolved => Resolution::Unresolved,
138 },
139 Lang::C => c::resolve(from, spec, index),
140 Lang::Go => go::resolve(spec, index, ws),
141 Lang::Ruby => ruby::resolve(from, spec, index, ws),
142 Lang::Java => java::resolve(spec, index, ws),
143 _ => Resolution::Unresolved,
144 }
145}
146
147pub fn exports(
148 lang: Lang,
149 path: &str,
150 text: &str,
151) -> (
152 Vec<crate::ts::exports::ExportedFn>,
153 Vec<crate::ts::exports::ExportedClass>,
154) {
155 match lang {
156 Lang::Python => python::exports(path, text),
157 Lang::C => (c::exports(path, text), Vec::new()),
158 Lang::Go => (go::exports(path, text), Vec::new()),
159 Lang::Ruby => (ruby::exports(path, text), Vec::new()),
160 Lang::Java => (java::exports(path, text), Vec::new()),
161 _ => (Vec::new(), Vec::new()),
162 }
163}
164
165pub fn lookup(lang: Lang, name: &str) -> Option<crate::ts::deps::CrateMatch> {
167 match lang {
168 Lang::C => c::lookup(name),
169 Lang::Go => go::lookup(name),
170 Lang::Ruby => ruby::lookup(name),
171 Lang::Java => java::lookup(name),
172 _ => None,
173 }
174}