tree_sitter_language_pack 1.2.1 → 1.3.1
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.
- checksums.yaml +4 -4
- data/src/lib.rs +80 -5
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6db995bcd67cec0642ed17f3ce6da23f5a8f4dc71118f2cc06cdbc3ce6e5b8b5
|
|
4
|
+
data.tar.gz: c2fee6f228184924cf2b7a908aec4d9e7dba3a6e607d0dfbb40de6be0efa296a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2c924f922d81454f9b02727e9f4883d31d80de10b4823cdff445f1b590fb60cc4851b86d57612a64ee4a52f65ff7ec962b16b85fe4ac55453d178afe34ae4169
|
|
7
|
+
data.tar.gz: e3323b3ed5f2f61fd4942ab6157d34ef998228d0e668b5b01873e2c57f8ab5e9ff5566df2b8ab76023f2a9e529a44ee255521ca9bb799a1ebff81460435f7427
|
data/src/lib.rs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
use magnus::{Error, Ruby, function, method, prelude::*};
|
|
1
|
+
use magnus::{Error, IntoValue, Ruby, Value as RbValue, function, method, prelude::*};
|
|
2
2
|
use std::sync::Mutex;
|
|
3
3
|
|
|
4
4
|
/// Wraps a tree-sitter Tree for safe sharing across the Ruby boundary.
|
|
@@ -84,20 +84,93 @@ fn parse_string(ruby: &Ruby, language: String, source: String) -> Result<TreeWra
|
|
|
84
84
|
Ok(TreeWrapper(Mutex::new(tree)))
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
///
|
|
87
|
+
/// Convert a serde_json::Value to a native Ruby object (Hash, Array, String, Integer, Float, true/false, nil).
|
|
88
|
+
fn json_value_to_ruby(ruby: &Ruby, value: &serde_json::Value) -> Result<RbValue, Error> {
|
|
89
|
+
Ok(match value {
|
|
90
|
+
serde_json::Value::Null => ruby.qnil().as_value(),
|
|
91
|
+
serde_json::Value::Bool(b) => (*b).into_value_with(ruby),
|
|
92
|
+
serde_json::Value::Number(n) => {
|
|
93
|
+
if let Some(i) = n.as_i64() {
|
|
94
|
+
i.into_value_with(ruby)
|
|
95
|
+
} else if let Some(u) = n.as_u64() {
|
|
96
|
+
u.into_value_with(ruby)
|
|
97
|
+
} else if let Some(f) = n.as_f64() {
|
|
98
|
+
f.into_value_with(ruby)
|
|
99
|
+
} else {
|
|
100
|
+
ruby.qnil().as_value()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
serde_json::Value::String(s) => s.as_str().into_value_with(ruby),
|
|
104
|
+
serde_json::Value::Array(arr) => {
|
|
105
|
+
let ary = ruby.ary_new_capa(arr.len());
|
|
106
|
+
for item in arr {
|
|
107
|
+
ary.push(json_value_to_ruby(ruby, item)?)?;
|
|
108
|
+
}
|
|
109
|
+
ary.as_value()
|
|
110
|
+
}
|
|
111
|
+
serde_json::Value::Object(map) => {
|
|
112
|
+
let hash = ruby.hash_new();
|
|
113
|
+
for (k, v) in map {
|
|
114
|
+
hash.aset(k.as_str(), json_value_to_ruby(ruby, v)?)?;
|
|
115
|
+
}
|
|
116
|
+
hash.as_value()
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// Unified process method that accepts a JSON config string and returns a native Ruby Hash.
|
|
88
122
|
///
|
|
89
123
|
/// The config JSON must contain at least `"language"`. Optional fields:
|
|
90
124
|
/// - `structure`, `imports`, `exports`, `comments`, `docstrings`, `symbols`, `diagnostics` (booleans, default true)
|
|
91
125
|
/// - `chunk_max_size` (integer or null, default null meaning no chunking)
|
|
92
|
-
fn process(ruby: &Ruby, source: String, config_json: String) -> Result<
|
|
126
|
+
fn process(ruby: &Ruby, source: String, config_json: String) -> Result<RbValue, Error> {
|
|
93
127
|
let core_config: tree_sitter_language_pack::ProcessConfig = serde_json::from_str(&config_json)
|
|
94
128
|
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("invalid config JSON: {e}")))?;
|
|
95
129
|
|
|
96
130
|
let result = tree_sitter_language_pack::process(&source, &core_config)
|
|
97
131
|
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("{e}")))?;
|
|
98
132
|
|
|
99
|
-
serde_json::
|
|
100
|
-
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("serialization failed: {e}")))
|
|
133
|
+
let json_value = serde_json::to_value(&result)
|
|
134
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("serialization failed: {e}")))?;
|
|
135
|
+
json_value_to_ruby(ruby, &json_value)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Extract patterns from source code using a JSON configuration.
|
|
139
|
+
///
|
|
140
|
+
/// The config JSON must contain:
|
|
141
|
+
/// - `language` (string): the language name
|
|
142
|
+
/// - `patterns` (object): named patterns to run, each with a `query` field
|
|
143
|
+
///
|
|
144
|
+
/// Returns a native Ruby Hash with extraction results.
|
|
145
|
+
fn extract(ruby: &Ruby, source: String, config_json: String) -> Result<RbValue, Error> {
|
|
146
|
+
let config: tree_sitter_language_pack::ExtractionConfig = serde_json::from_str(&config_json)
|
|
147
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("invalid config JSON: {e}")))?;
|
|
148
|
+
|
|
149
|
+
let result = tree_sitter_language_pack::extract_patterns(&source, &config)
|
|
150
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("{e}")))?;
|
|
151
|
+
|
|
152
|
+
let json_value = serde_json::to_value(&result)
|
|
153
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("serialization failed: {e}")))?;
|
|
154
|
+
json_value_to_ruby(ruby, &json_value)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/// Validate extraction patterns without running them.
|
|
158
|
+
///
|
|
159
|
+
/// The config JSON must contain:
|
|
160
|
+
/// - `language` (string): the language name
|
|
161
|
+
/// - `patterns` (object): named patterns to validate
|
|
162
|
+
///
|
|
163
|
+
/// Returns a native Ruby Hash with validation results.
|
|
164
|
+
fn validate_extraction(ruby: &Ruby, config_json: String) -> Result<RbValue, Error> {
|
|
165
|
+
let config: tree_sitter_language_pack::ExtractionConfig = serde_json::from_str(&config_json)
|
|
166
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("invalid config JSON: {e}")))?;
|
|
167
|
+
|
|
168
|
+
let result = tree_sitter_language_pack::validate_extraction(&config)
|
|
169
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("{e}")))?;
|
|
170
|
+
|
|
171
|
+
let json_value = serde_json::to_value(&result)
|
|
172
|
+
.map_err(|e| Error::new(ruby.exception_runtime_error(), format!("serialization failed: {e}")))?;
|
|
173
|
+
json_value_to_ruby(ruby, &json_value)
|
|
101
174
|
}
|
|
102
175
|
|
|
103
176
|
/// Initialize the language pack with configuration.
|
|
@@ -187,6 +260,8 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
187
260
|
module.define_module_function("get_language_ptr", function!(get_language_ptr, 1))?;
|
|
188
261
|
module.define_module_function("parse_string", function!(parse_string, 2))?;
|
|
189
262
|
module.define_module_function("process", function!(process, 2))?;
|
|
263
|
+
module.define_module_function("extract", function!(extract, 2))?;
|
|
264
|
+
module.define_module_function("validate_extraction", function!(validate_extraction, 1))?;
|
|
190
265
|
|
|
191
266
|
// Download API functions
|
|
192
267
|
module.define_module_function("init", function!(rb_init, 1))?;
|