tree_sitter_language_pack 1.3.0 → 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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/src/lib.rs +50 -13
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73a601edb6bb285bf825a5b588c6fb9ae58fa8f1a0bda7c4d537cdfc9f72a912
4
- data.tar.gz: 331e45aafcaf276595cfa679aa5de2524204b5ba4bcb138d4c1b423471576964
3
+ metadata.gz: 6db995bcd67cec0642ed17f3ce6da23f5a8f4dc71118f2cc06cdbc3ce6e5b8b5
4
+ data.tar.gz: c2fee6f228184924cf2b7a908aec4d9e7dba3a6e607d0dfbb40de6be0efa296a
5
5
  SHA512:
6
- metadata.gz: e197f722769c8cbb45aabbe38d713bfb234036c74d523d521041814916e5518ef6f4acd5c6b66ab0f1317404d7fc608b5043915aa93331c876769363674627d8
7
- data.tar.gz: b3efc14816133eec048d9a988fba3b0620a991a3d3c9426eb6968bdce14925d6eeaa20bfb4394d0e561f828f7661e720d344cff09ec3bd421dc2697aa0fcc752
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,55 @@ fn parse_string(ruby: &Ruby, language: String, source: String) -> Result<TreeWra
84
84
  Ok(TreeWrapper(Mutex::new(tree)))
85
85
  }
86
86
 
87
- /// Unified process method that accepts a JSON config string and returns a JSON result string.
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<String, Error> {
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::to_string(&result)
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)
101
136
  }
102
137
 
103
138
  /// Extract patterns from source code using a JSON configuration.
@@ -106,16 +141,17 @@ fn process(ruby: &Ruby, source: String, config_json: String) -> Result<String, E
106
141
  /// - `language` (string): the language name
107
142
  /// - `patterns` (object): named patterns to run, each with a `query` field
108
143
  ///
109
- /// Returns a JSON string with extraction results.
110
- fn extract(ruby: &Ruby, source: String, config_json: String) -> Result<String, Error> {
144
+ /// Returns a native Ruby Hash with extraction results.
145
+ fn extract(ruby: &Ruby, source: String, config_json: String) -> Result<RbValue, Error> {
111
146
  let config: tree_sitter_language_pack::ExtractionConfig = serde_json::from_str(&config_json)
112
147
  .map_err(|e| Error::new(ruby.exception_runtime_error(), format!("invalid config JSON: {e}")))?;
113
148
 
114
149
  let result = tree_sitter_language_pack::extract_patterns(&source, &config)
115
150
  .map_err(|e| Error::new(ruby.exception_runtime_error(), format!("{e}")))?;
116
151
 
117
- serde_json::to_string(&result)
118
- .map_err(|e| Error::new(ruby.exception_runtime_error(), format!("serialization failed: {e}")))
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)
119
155
  }
120
156
 
121
157
  /// Validate extraction patterns without running them.
@@ -124,16 +160,17 @@ fn extract(ruby: &Ruby, source: String, config_json: String) -> Result<String, E
124
160
  /// - `language` (string): the language name
125
161
  /// - `patterns` (object): named patterns to validate
126
162
  ///
127
- /// Returns a JSON string with validation results.
128
- fn validate_extraction(ruby: &Ruby, config_json: String) -> Result<String, Error> {
163
+ /// Returns a native Ruby Hash with validation results.
164
+ fn validate_extraction(ruby: &Ruby, config_json: String) -> Result<RbValue, Error> {
129
165
  let config: tree_sitter_language_pack::ExtractionConfig = serde_json::from_str(&config_json)
130
166
  .map_err(|e| Error::new(ruby.exception_runtime_error(), format!("invalid config JSON: {e}")))?;
131
167
 
132
168
  let result = tree_sitter_language_pack::validate_extraction(&config)
133
169
  .map_err(|e| Error::new(ruby.exception_runtime_error(), format!("{e}")))?;
134
170
 
135
- serde_json::to_string(&result)
136
- .map_err(|e| Error::new(ruby.exception_runtime_error(), format!("serialization failed: {e}")))
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)
137
174
  }
138
175
 
139
176
  /// Initialize the language pack with configuration.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree_sitter_language_pack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kreuzberg.dev