@tishlang/tish-format 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +49 -0
- package/LICENSE +13 -0
- package/README.md +138 -0
- package/bin/tish-format +0 -0
- package/crates/js_to_tish/Cargo.toml +11 -0
- package/crates/js_to_tish/README.md +18 -0
- package/crates/js_to_tish/src/error.rs +55 -0
- package/crates/js_to_tish/src/lib.rs +11 -0
- package/crates/js_to_tish/src/span_util.rs +35 -0
- package/crates/js_to_tish/src/transform/expr.rs +610 -0
- package/crates/js_to_tish/src/transform/stmt.rs +503 -0
- package/crates/js_to_tish/src/transform.rs +60 -0
- package/crates/tish/Cargo.toml +54 -0
- package/crates/tish/src/cargo_native_registry.rs +32 -0
- package/crates/tish/src/cli_help.rs +565 -0
- package/crates/tish/src/main.rs +781 -0
- package/crates/tish/src/repl_completion.rs +200 -0
- package/crates/tish/tests/cargo_example_compile.rs +67 -0
- package/crates/tish/tests/fixtures/cargo_example_project/Cargo.toml +3 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/Cargo.toml +11 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/src/lib.rs +12 -0
- package/crates/tish/tests/fixtures/cargo_example_project/package.json +10 -0
- package/crates/tish/tests/fixtures/cargo_example_project/src/main.tish +3 -0
- package/crates/tish/tests/integration_test.rs +1095 -0
- package/crates/tish/tests/run_optimize_stdout_parity.rs +50 -0
- package/crates/tish/tests/shortcircuit.rs +65 -0
- package/crates/tish_ast/Cargo.toml +9 -0
- package/crates/tish_ast/src/ast.rs +620 -0
- package/crates/tish_ast/src/lib.rs +5 -0
- package/crates/tish_build_utils/Cargo.toml +11 -0
- package/crates/tish_build_utils/src/lib.rs +577 -0
- package/crates/tish_builtins/Cargo.toml +20 -0
- package/crates/tish_builtins/src/array.rs +441 -0
- package/crates/tish_builtins/src/construct.rs +159 -0
- package/crates/tish_builtins/src/globals.rs +213 -0
- package/crates/tish_builtins/src/helpers.rs +35 -0
- package/crates/tish_builtins/src/lib.rs +16 -0
- package/crates/tish_builtins/src/math.rs +89 -0
- package/crates/tish_builtins/src/object.rs +36 -0
- package/crates/tish_builtins/src/string.rs +647 -0
- package/crates/tish_builtins/src/symbol.rs +83 -0
- package/crates/tish_bytecode/Cargo.toml +17 -0
- package/crates/tish_bytecode/src/chunk.rs +96 -0
- package/crates/tish_bytecode/src/compiler.rs +1760 -0
- package/crates/tish_bytecode/src/encoding.rs +100 -0
- package/crates/tish_bytecode/src/lib.rs +19 -0
- package/crates/tish_bytecode/src/opcode.rs +142 -0
- package/crates/tish_bytecode/src/peephole.rs +189 -0
- package/crates/tish_bytecode/src/serialize.rs +163 -0
- package/crates/tish_bytecode/tests/break_continue_bytecode.rs +44 -0
- package/crates/tish_bytecode/tests/constant_folding.rs +84 -0
- package/crates/tish_bytecode/tests/sort_optimization.rs +31 -0
- package/crates/tish_compile/Cargo.toml +26 -0
- package/crates/tish_compile/src/codegen.rs +5332 -0
- package/crates/tish_compile/src/infer.rs +292 -0
- package/crates/tish_compile/src/lib.rs +164 -0
- package/crates/tish_compile/src/resolve.rs +1388 -0
- package/crates/tish_compile/src/types.rs +501 -0
- package/crates/tish_compile_js/Cargo.toml +18 -0
- package/crates/tish_compile_js/examples/jsx_vdom_smoke.tish +8 -0
- package/crates/tish_compile_js/src/codegen.rs +871 -0
- package/crates/tish_compile_js/src/error.rs +20 -0
- package/crates/tish_compile_js/src/lib.rs +26 -0
- package/crates/tish_compile_js/src/tests_jsx.rs +350 -0
- package/crates/tish_compiler_wasm/Cargo.toml +21 -0
- package/crates/tish_compiler_wasm/src/lib.rs +57 -0
- package/crates/tish_compiler_wasm/src/resolve_virtual.rs +473 -0
- package/crates/tish_core/Cargo.toml +26 -0
- package/crates/tish_core/src/console_style.rs +160 -0
- package/crates/tish_core/src/json.rs +387 -0
- package/crates/tish_core/src/lib.rs +17 -0
- package/crates/tish_core/src/macros.rs +36 -0
- package/crates/tish_core/src/uri.rs +118 -0
- package/crates/tish_core/src/value.rs +696 -0
- package/crates/tish_core/src/vmref.rs +178 -0
- package/crates/tish_cranelift/Cargo.toml +19 -0
- package/crates/tish_cranelift/src/lib.rs +43 -0
- package/crates/tish_cranelift/src/link.rs +117 -0
- package/crates/tish_cranelift/src/lower.rs +85 -0
- package/crates/tish_cranelift_runtime/Cargo.toml +25 -0
- package/crates/tish_cranelift_runtime/src/lib.rs +45 -0
- package/crates/tish_eval/Cargo.toml +45 -0
- package/crates/tish_eval/src/eval.rs +3717 -0
- package/crates/tish_eval/src/http.rs +188 -0
- package/crates/tish_eval/src/lib.rs +99 -0
- package/crates/tish_eval/src/natives.rs +399 -0
- package/crates/tish_eval/src/promise.rs +179 -0
- package/crates/tish_eval/src/regex.rs +299 -0
- package/crates/tish_eval/src/timers.rs +120 -0
- package/crates/tish_eval/src/value.rs +318 -0
- package/crates/tish_eval/src/value_convert.rs +111 -0
- package/crates/tish_fmt/Cargo.toml +16 -0
- package/crates/tish_fmt/src/bin/tish-fmt.rs +41 -0
- package/crates/tish_fmt/src/lib.rs +2101 -0
- package/crates/tish_jsx_web/Cargo.toml +9 -0
- package/crates/tish_jsx_web/README.md +5 -0
- package/crates/tish_jsx_web/src/lib.rs +2 -0
- package/crates/tish_lexer/Cargo.toml +9 -0
- package/crates/tish_lexer/src/lib.rs +716 -0
- package/crates/tish_lexer/src/token.rs +163 -0
- package/crates/tish_lint/Cargo.toml +18 -0
- package/crates/tish_lint/src/bin/tish-lint.rs +195 -0
- package/crates/tish_lint/src/lib.rs +289 -0
- package/crates/tish_llvm/Cargo.toml +13 -0
- package/crates/tish_llvm/src/lib.rs +115 -0
- package/crates/tish_lsp/Cargo.toml +25 -0
- package/crates/tish_lsp/README.md +26 -0
- package/crates/tish_lsp/src/builtin_goto.rs +362 -0
- package/crates/tish_lsp/src/import_goto.rs +562 -0
- package/crates/tish_lsp/src/main.rs +1046 -0
- package/crates/tish_native/Cargo.toml +16 -0
- package/crates/tish_native/src/build.rs +427 -0
- package/crates/tish_native/src/config.rs +48 -0
- package/crates/tish_native/src/lib.rs +416 -0
- package/crates/tish_opt/Cargo.toml +13 -0
- package/crates/tish_opt/src/lib.rs +943 -0
- package/crates/tish_parser/Cargo.toml +11 -0
- package/crates/tish_parser/src/lib.rs +332 -0
- package/crates/tish_parser/src/parser.rs +2304 -0
- package/crates/tish_pg/Cargo.toml +34 -0
- package/crates/tish_pg/README.md +38 -0
- package/crates/tish_pg/src/error.rs +52 -0
- package/crates/tish_pg/src/lib.rs +955 -0
- package/crates/tish_resolve/Cargo.toml +13 -0
- package/crates/tish_resolve/src/lib.rs +3561 -0
- package/crates/tish_resolve/src/pos.rs +141 -0
- package/crates/tish_runtime/Cargo.toml +96 -0
- package/crates/tish_runtime/src/http.rs +1298 -0
- package/crates/tish_runtime/src/http_fetch.rs +471 -0
- package/crates/tish_runtime/src/http_hyper.rs +418 -0
- package/crates/tish_runtime/src/http_prefork.rs +189 -0
- package/crates/tish_runtime/src/lib.rs +1192 -0
- package/crates/tish_runtime/src/native_promise.rs +15 -0
- package/crates/tish_runtime/src/promise.rs +248 -0
- package/crates/tish_runtime/src/promise_io.rs +38 -0
- package/crates/tish_runtime/src/timers.rs +166 -0
- package/crates/tish_runtime/src/ws.rs +761 -0
- package/crates/tish_runtime/tests/fetch_readable_stream.rs +102 -0
- package/crates/tish_ui/Cargo.toml +17 -0
- package/crates/tish_ui/src/jsx.rs +682 -0
- package/crates/tish_ui/src/lib.rs +20 -0
- package/crates/tish_ui/src/runtime/hooks.rs +569 -0
- package/crates/tish_ui/src/runtime/mod.rs +180 -0
- package/crates/tish_vm/Cargo.toml +47 -0
- package/crates/tish_vm/src/lib.rs +39 -0
- package/crates/tish_vm/src/vm.rs +2192 -0
- package/crates/tish_vm/tests/fixtures/or_string_cmd.tish +2 -0
- package/crates/tish_vm/tests/lexical_scope_declare.rs +34 -0
- package/crates/tish_vm/tests/peephole_jump_chain_logical_or.rs +150 -0
- package/crates/tish_wasm/Cargo.toml +15 -0
- package/crates/tish_wasm/src/lib.rs +424 -0
- package/crates/tish_wasm_runtime/Cargo.toml +37 -0
- package/crates/tish_wasm_runtime/src/gpu.rs +413 -0
- package/crates/tish_wasm_runtime/src/lib.rs +42 -0
- package/crates/tishlang_cargo_bindgen/Cargo.toml +26 -0
- package/crates/tishlang_cargo_bindgen/src/classify.rs +263 -0
- package/crates/tishlang_cargo_bindgen/src/discover.rs +125 -0
- package/crates/tishlang_cargo_bindgen/src/infer.rs +382 -0
- package/crates/tishlang_cargo_bindgen/src/lib.rs +349 -0
- package/crates/tishlang_cargo_bindgen/src/main.rs +167 -0
- package/crates/tishlang_cargo_bindgen/src/metadata.rs +117 -0
- package/justfile +268 -0
- package/package.json +1 -1
- package/platform/darwin-arm64/tish-fmt +0 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
//! Classify a `pub fn` from syn for glue emission (driven by signature shape, not crate name).
|
|
2
|
+
|
|
3
|
+
use syn::{FnArg, GenericArgument, ItemFn, PathArguments, ReturnType, Type, TypeReference};
|
|
4
|
+
|
|
5
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
6
|
+
pub enum SignatureClass {
|
|
7
|
+
/// `fn foo(args: &[Value]) -> Value` (or `tishlang_runtime::Value`).
|
|
8
|
+
TishValueAbi,
|
|
9
|
+
/// First parameter `&T` (or `&mut T`), `T: Serialize` (or `?Sized + Serialize`), returns `Result<String, _>`-like.
|
|
10
|
+
SerializeRefToResultString,
|
|
11
|
+
/// First parameter `&str` (or `& 'a str`), returns `Result<_, _>`, and has `Deserialize` bound on a type param.
|
|
12
|
+
DeserializeStrToResult,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
pub fn classify_public_fn(item: &ItemFn) -> Option<SignatureClass> {
|
|
16
|
+
if matches!(classify_tish_abi(item), Some(SignatureClass::TishValueAbi)) {
|
|
17
|
+
return Some(SignatureClass::TishValueAbi);
|
|
18
|
+
}
|
|
19
|
+
if is_deserialize_str_result(item) {
|
|
20
|
+
return Some(SignatureClass::DeserializeStrToResult);
|
|
21
|
+
}
|
|
22
|
+
if is_serialize_ref_to_result_string(item) {
|
|
23
|
+
return Some(SignatureClass::SerializeRefToResultString);
|
|
24
|
+
}
|
|
25
|
+
None
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fn classify_tish_abi(item: &ItemFn) -> Option<SignatureClass> {
|
|
29
|
+
let sig = &item.sig;
|
|
30
|
+
let mut value_args = 0;
|
|
31
|
+
for arg in &sig.inputs {
|
|
32
|
+
let FnArg::Typed(t) = arg else {
|
|
33
|
+
continue;
|
|
34
|
+
};
|
|
35
|
+
if is_slice_value(&t.ty) {
|
|
36
|
+
value_args += 1;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if value_args != 1 || sig.inputs.len() != 1 {
|
|
40
|
+
return None;
|
|
41
|
+
}
|
|
42
|
+
let Some(ret_ty) = return_type_inner(&sig.output) else {
|
|
43
|
+
return None;
|
|
44
|
+
};
|
|
45
|
+
if !is_value_type(ret_ty) {
|
|
46
|
+
return None;
|
|
47
|
+
}
|
|
48
|
+
Some(SignatureClass::TishValueAbi)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn return_type_inner(ret: &ReturnType) -> Option<&Type> {
|
|
52
|
+
match ret {
|
|
53
|
+
ReturnType::Default => None,
|
|
54
|
+
ReturnType::Type(_, ty) => Some(ty),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fn is_slice_value(ty: &Type) -> bool {
|
|
59
|
+
let Some(inner) = strip_reference(ty) else {
|
|
60
|
+
return false;
|
|
61
|
+
};
|
|
62
|
+
let Type::Slice(s) = inner else {
|
|
63
|
+
return false;
|
|
64
|
+
};
|
|
65
|
+
is_value_type(&s.elem)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn strip_reference(ty: &Type) -> Option<&Type> {
|
|
69
|
+
match ty {
|
|
70
|
+
Type::Reference(TypeReference { elem, .. }) => Some(elem.as_ref()),
|
|
71
|
+
_ => None,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn is_value_type(ty: &Type) -> bool {
|
|
76
|
+
let Type::Path(p) = ty else {
|
|
77
|
+
return false;
|
|
78
|
+
};
|
|
79
|
+
let seg = p.path.segments.last();
|
|
80
|
+
let Some(seg) = seg else {
|
|
81
|
+
return false;
|
|
82
|
+
};
|
|
83
|
+
if seg.ident != "Value" {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
// Accept `Value`, `tishlang_runtime::Value`, `tishlang_core::Value`
|
|
87
|
+
if p.path.segments.len() == 1 {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
let prev = &p.path.segments[p.path.segments.len() - 2];
|
|
91
|
+
prev.ident == "tishlang_runtime" || prev.ident == "tishlang_core"
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn is_str_ref(ty: &Type) -> bool {
|
|
95
|
+
match ty {
|
|
96
|
+
Type::Reference(TypeReference { elem, .. }) => matches!(
|
|
97
|
+
elem.as_ref(),
|
|
98
|
+
Type::Path(p) if p.path.is_ident("str")
|
|
99
|
+
),
|
|
100
|
+
_ => false,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn is_deserialize_str_result(item: &ItemFn) -> bool {
|
|
105
|
+
let sig = &item.sig;
|
|
106
|
+
if sig.inputs.len() != 1 {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
let FnArg::Typed(arg) = sig.inputs.first().unwrap() else {
|
|
110
|
+
return false;
|
|
111
|
+
};
|
|
112
|
+
if !is_str_ref(&arg.ty) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
let Some(ret) = return_type_inner(&sig.output) else {
|
|
116
|
+
return false;
|
|
117
|
+
};
|
|
118
|
+
if result_ok_type(ret).is_none() {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
has_deserialize_bound(item)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
fn has_deserialize_bound(item: &ItemFn) -> bool {
|
|
125
|
+
for p in &item.sig.generics.params {
|
|
126
|
+
if let syn::GenericParam::Type(t) = p {
|
|
127
|
+
for b in &t.bounds {
|
|
128
|
+
if bound_name_is(b, "Deserialize") {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if let Some(wc) = &item.sig.generics.where_clause {
|
|
135
|
+
for pred in &wc.predicates {
|
|
136
|
+
if let syn::WherePredicate::Type(t) = pred {
|
|
137
|
+
for b in &t.bounds {
|
|
138
|
+
if bound_name_is(b, "Deserialize") {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
false
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn bound_name_is(b: &syn::TypeParamBound, want: &str) -> bool {
|
|
149
|
+
let syn::TypeParamBound::Trait(t) = b else {
|
|
150
|
+
return false;
|
|
151
|
+
};
|
|
152
|
+
let path = &t.path;
|
|
153
|
+
path.segments.last().is_some_and(|s| s.ident == want)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
fn is_serialize_ref_to_result_string(item: &ItemFn) -> bool {
|
|
157
|
+
let sig = &item.sig;
|
|
158
|
+
if sig.inputs.len() != 1 {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
let FnArg::Typed(arg) = sig.inputs.first().unwrap() else {
|
|
162
|
+
return false;
|
|
163
|
+
};
|
|
164
|
+
if strip_reference(&arg.ty).is_none() {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
let Some(ret) = return_type_inner(&sig.output) else {
|
|
168
|
+
return false;
|
|
169
|
+
};
|
|
170
|
+
let Some(ok_ty) = result_ok_type(ret) else {
|
|
171
|
+
return false;
|
|
172
|
+
};
|
|
173
|
+
if !type_is_string_or_str(ok_ty) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
has_serialize_bound(item)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
fn has_serialize_bound(item: &ItemFn) -> bool {
|
|
180
|
+
for p in &item.sig.generics.params {
|
|
181
|
+
if let syn::GenericParam::Type(t) = p {
|
|
182
|
+
for b in &t.bounds {
|
|
183
|
+
if bound_name_is(b, "Serialize") {
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if let Some(wc) = &item.sig.generics.where_clause {
|
|
190
|
+
for pred in &wc.predicates {
|
|
191
|
+
if let syn::WherePredicate::Type(t) = pred {
|
|
192
|
+
for b in &t.bounds {
|
|
193
|
+
if bound_name_is(b, "Serialize") {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
false
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
fn result_ok_type(ty: &Type) -> Option<&Type> {
|
|
204
|
+
let Type::Path(p) = ty else {
|
|
205
|
+
return None;
|
|
206
|
+
};
|
|
207
|
+
let seg = p.path.segments.last()?;
|
|
208
|
+
if seg.ident != "Result" {
|
|
209
|
+
return None;
|
|
210
|
+
}
|
|
211
|
+
let PathArguments::AngleBracketed(ab) = &seg.arguments else {
|
|
212
|
+
return None;
|
|
213
|
+
};
|
|
214
|
+
let first = ab.args.first()?;
|
|
215
|
+
let GenericArgument::Type(t) = first else {
|
|
216
|
+
return None;
|
|
217
|
+
};
|
|
218
|
+
Some(t)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
fn type_is_string_or_str(ty: &Type) -> bool {
|
|
222
|
+
match ty {
|
|
223
|
+
Type::Path(p) => {
|
|
224
|
+
if p.path.is_ident("String") {
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
p.path.segments.len() == 1 && p.path.segments[0].ident == "str"
|
|
228
|
+
}
|
|
229
|
+
_ => false,
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
#[cfg(test)]
|
|
234
|
+
mod tests {
|
|
235
|
+
use super::*;
|
|
236
|
+
use syn::parse_quote;
|
|
237
|
+
|
|
238
|
+
#[test]
|
|
239
|
+
fn classify_serde_to_string_shape() {
|
|
240
|
+
let item: ItemFn = parse_quote! {
|
|
241
|
+
pub fn to_string<T: ?Sized + Serialize>(value: &T) -> Result<String, ()> {
|
|
242
|
+
unimplemented!()
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
assert_eq!(
|
|
246
|
+
classify_public_fn(&item),
|
|
247
|
+
Some(SignatureClass::SerializeRefToResultString)
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#[test]
|
|
252
|
+
fn classify_from_str_shape() {
|
|
253
|
+
let item: ItemFn = parse_quote! {
|
|
254
|
+
pub fn from_str<'a, T: Deserialize<'a>>(s: &'a str) -> Result<T, ()> {
|
|
255
|
+
unimplemented!()
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
assert_eq!(
|
|
259
|
+
classify_public_fn(&item),
|
|
260
|
+
Some(SignatureClass::DeserializeStrToResult)
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
//! Walk dependency `src/**/*.rs` and collect `pub fn` items by name.
|
|
2
|
+
|
|
3
|
+
use std::collections::HashMap;
|
|
4
|
+
use std::fs;
|
|
5
|
+
use std::path::{Path, PathBuf};
|
|
6
|
+
|
|
7
|
+
use syn::{Item, ItemFn, Visibility};
|
|
8
|
+
use walkdir::WalkDir;
|
|
9
|
+
|
|
10
|
+
fn is_pub(vis: &Visibility) -> bool {
|
|
11
|
+
matches!(vis, Visibility::Public(_))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/// Map export name (Rust ident) to the function AST (must be unique).
|
|
15
|
+
pub fn discover_public_functions(crate_root: &Path) -> Result<HashMap<String, ItemFn>, String> {
|
|
16
|
+
let src = crate_root.join("src");
|
|
17
|
+
if !src.is_dir() {
|
|
18
|
+
return Err(format!("no src/ under {}", crate_root.display()));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let mut map: HashMap<String, (PathBuf, ItemFn)> = HashMap::new();
|
|
22
|
+
|
|
23
|
+
for entry in WalkDir::new(&src)
|
|
24
|
+
.into_iter()
|
|
25
|
+
.filter_map(|e| e.ok())
|
|
26
|
+
.filter(|e| e.path().extension().map(|x| x == "rs").unwrap_or(false))
|
|
27
|
+
{
|
|
28
|
+
let path = entry.path();
|
|
29
|
+
let text =
|
|
30
|
+
fs::read_to_string(path).map_err(|e| format!("read {}: {}", path.display(), e))?;
|
|
31
|
+
let file =
|
|
32
|
+
syn::parse_file(&text).map_err(|e| format!("parse {}: {}", path.display(), e))?;
|
|
33
|
+
|
|
34
|
+
for item in file.items {
|
|
35
|
+
if let Item::Fn(f) = item {
|
|
36
|
+
if !is_pub(&f.vis) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
let name = f.sig.ident.to_string();
|
|
40
|
+
if let Some((prev_path, _)) = map.get(&name) {
|
|
41
|
+
return Err(format!(
|
|
42
|
+
"ambiguous public fn `{}`: found in {} and {}",
|
|
43
|
+
name,
|
|
44
|
+
prev_path.display(),
|
|
45
|
+
path.display()
|
|
46
|
+
));
|
|
47
|
+
}
|
|
48
|
+
map.insert(name, (path.to_path_buf(), f));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Ok(map.into_iter().map(|(k, (_, v))| (k, v)).collect())
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// On-disk location of a top-level `pub fn {fn_name}` under `crate_root/src` (LSP line/column, 0-based).
|
|
57
|
+
///
|
|
58
|
+
/// Requires `proc-macro2` built with `span-locations` (this crate enables it) so spans from
|
|
59
|
+
/// `syn::parse_file` carry line/column.
|
|
60
|
+
pub fn rust_public_fn_location(
|
|
61
|
+
crate_root: &Path,
|
|
62
|
+
fn_name: &str,
|
|
63
|
+
) -> Result<(PathBuf, u32, u32), String> {
|
|
64
|
+
let src = crate_root.join("src");
|
|
65
|
+
if !src.is_dir() {
|
|
66
|
+
return Err(format!("no src/ under {}", crate_root.display()));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for entry in WalkDir::new(&src)
|
|
70
|
+
.into_iter()
|
|
71
|
+
.filter_map(|e| e.ok())
|
|
72
|
+
.filter(|e| e.path().extension().map(|x| x == "rs").unwrap_or(false))
|
|
73
|
+
{
|
|
74
|
+
let path = entry.path();
|
|
75
|
+
let text =
|
|
76
|
+
fs::read_to_string(path).map_err(|e| format!("read {}: {}", path.display(), e))?;
|
|
77
|
+
let file =
|
|
78
|
+
syn::parse_file(&text).map_err(|e| format!("parse {}: {}", path.display(), e))?;
|
|
79
|
+
|
|
80
|
+
for item in file.items {
|
|
81
|
+
if let Item::Fn(f) = item {
|
|
82
|
+
if !is_pub(&f.vis) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if f.sig.ident != fn_name {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
let lc = f.sig.ident.span().start();
|
|
89
|
+
let line = u32::try_from(lc.line)
|
|
90
|
+
.map_err(|_| "span line out of range".to_string())?
|
|
91
|
+
.saturating_sub(1);
|
|
92
|
+
let col =
|
|
93
|
+
u32::try_from(lc.column).map_err(|_| "span column out of range".to_string())?;
|
|
94
|
+
return Ok((path.to_path_buf(), line, col));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
Err(format!(
|
|
100
|
+
"no public fn `{}` found under {}/src",
|
|
101
|
+
fn_name,
|
|
102
|
+
crate_root.display()
|
|
103
|
+
))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[cfg(test)]
|
|
107
|
+
mod tests {
|
|
108
|
+
use super::*;
|
|
109
|
+
use tempfile::tempdir;
|
|
110
|
+
|
|
111
|
+
#[test]
|
|
112
|
+
fn rust_public_fn_location_finds_fn() {
|
|
113
|
+
let tmp = tempdir().unwrap();
|
|
114
|
+
let src = tmp.path().join("src");
|
|
115
|
+
fs::create_dir_all(&src).unwrap();
|
|
116
|
+
fs::write(
|
|
117
|
+
src.join("lib.rs"),
|
|
118
|
+
"// comment\npub fn hello_tish_export() -> i32 { 0 }\n",
|
|
119
|
+
)
|
|
120
|
+
.unwrap();
|
|
121
|
+
let (path, line, _col) = rust_public_fn_location(tmp.path(), "hello_tish_export").unwrap();
|
|
122
|
+
assert!(path.ends_with("lib.rs"));
|
|
123
|
+
assert_eq!(line, 1);
|
|
124
|
+
}
|
|
125
|
+
}
|