spikard 0.4.0-x86_64-linux

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 (138) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +1 -0
  3. data/README.md +659 -0
  4. data/ext/spikard_rb/Cargo.toml +17 -0
  5. data/ext/spikard_rb/extconf.rb +10 -0
  6. data/ext/spikard_rb/src/lib.rs +6 -0
  7. data/lib/spikard/app.rb +405 -0
  8. data/lib/spikard/background.rb +27 -0
  9. data/lib/spikard/config.rb +396 -0
  10. data/lib/spikard/converters.rb +13 -0
  11. data/lib/spikard/handler_wrapper.rb +113 -0
  12. data/lib/spikard/provide.rb +214 -0
  13. data/lib/spikard/response.rb +173 -0
  14. data/lib/spikard/schema.rb +243 -0
  15. data/lib/spikard/sse.rb +111 -0
  16. data/lib/spikard/streaming_response.rb +44 -0
  17. data/lib/spikard/testing.rb +221 -0
  18. data/lib/spikard/upload_file.rb +131 -0
  19. data/lib/spikard/version.rb +5 -0
  20. data/lib/spikard/websocket.rb +59 -0
  21. data/lib/spikard.rb +43 -0
  22. data/sig/spikard.rbs +366 -0
  23. data/vendor/bundle/ruby/3.4.0/gems/diff-lcs-1.6.2/mise.toml +5 -0
  24. data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
  25. data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
  26. data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +139 -0
  27. data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +561 -0
  28. data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
  29. data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
  30. data/vendor/crates/spikard-bindings-shared/src/error_response.rs +403 -0
  31. data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +274 -0
  32. data/vendor/crates/spikard-bindings-shared/src/lib.rs +25 -0
  33. data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +298 -0
  34. data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +637 -0
  35. data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +309 -0
  36. data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
  37. data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +355 -0
  38. data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +502 -0
  39. data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +389 -0
  40. data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +413 -0
  41. data/vendor/crates/spikard-core/Cargo.toml +40 -0
  42. data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
  43. data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
  44. data/vendor/crates/spikard-core/src/debug.rs +63 -0
  45. data/vendor/crates/spikard-core/src/di/container.rs +726 -0
  46. data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
  47. data/vendor/crates/spikard-core/src/di/error.rs +118 -0
  48. data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
  49. data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
  50. data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
  51. data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
  52. data/vendor/crates/spikard-core/src/di/value.rs +283 -0
  53. data/vendor/crates/spikard-core/src/errors.rs +39 -0
  54. data/vendor/crates/spikard-core/src/http.rs +153 -0
  55. data/vendor/crates/spikard-core/src/lib.rs +29 -0
  56. data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
  57. data/vendor/crates/spikard-core/src/metadata.rs +397 -0
  58. data/vendor/crates/spikard-core/src/parameters.rs +723 -0
  59. data/vendor/crates/spikard-core/src/problem.rs +310 -0
  60. data/vendor/crates/spikard-core/src/request_data.rs +189 -0
  61. data/vendor/crates/spikard-core/src/router.rs +249 -0
  62. data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
  63. data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
  64. data/vendor/crates/spikard-core/src/validation/error_mapper.rs +689 -0
  65. data/vendor/crates/spikard-core/src/validation/mod.rs +459 -0
  66. data/vendor/crates/spikard-http/Cargo.toml +58 -0
  67. data/vendor/crates/spikard-http/examples/sse-notifications.rs +147 -0
  68. data/vendor/crates/spikard-http/examples/websocket-chat.rs +91 -0
  69. data/vendor/crates/spikard-http/src/auth.rs +247 -0
  70. data/vendor/crates/spikard-http/src/background.rs +1562 -0
  71. data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
  72. data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
  73. data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
  74. data/vendor/crates/spikard-http/src/cors.rs +490 -0
  75. data/vendor/crates/spikard-http/src/debug.rs +63 -0
  76. data/vendor/crates/spikard-http/src/di_handler.rs +1878 -0
  77. data/vendor/crates/spikard-http/src/handler_response.rs +532 -0
  78. data/vendor/crates/spikard-http/src/handler_trait.rs +861 -0
  79. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
  80. data/vendor/crates/spikard-http/src/lib.rs +524 -0
  81. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
  82. data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
  83. data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
  84. data/vendor/crates/spikard-http/src/middleware/multipart.rs +930 -0
  85. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +541 -0
  86. data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
  87. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
  88. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -0
  89. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +867 -0
  90. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +678 -0
  91. data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
  92. data/vendor/crates/spikard-http/src/response.rs +399 -0
  93. data/vendor/crates/spikard-http/src/server/handler.rs +1557 -0
  94. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
  95. data/vendor/crates/spikard-http/src/server/mod.rs +806 -0
  96. data/vendor/crates/spikard-http/src/server/request_extraction.rs +630 -0
  97. data/vendor/crates/spikard-http/src/server/routing_factory.rs +497 -0
  98. data/vendor/crates/spikard-http/src/sse.rs +961 -0
  99. data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
  100. data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
  101. data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
  102. data/vendor/crates/spikard-http/src/testing.rs +377 -0
  103. data/vendor/crates/spikard-http/src/websocket.rs +831 -0
  104. data/vendor/crates/spikard-http/tests/background_behavior.rs +918 -0
  105. data/vendor/crates/spikard-http/tests/common/handlers.rs +308 -0
  106. data/vendor/crates/spikard-http/tests/common/mod.rs +21 -0
  107. data/vendor/crates/spikard-http/tests/di_integration.rs +202 -0
  108. data/vendor/crates/spikard-http/tests/doc_snippets.rs +4 -0
  109. data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1135 -0
  110. data/vendor/crates/spikard-http/tests/multipart_behavior.rs +688 -0
  111. data/vendor/crates/spikard-http/tests/server_config_builder.rs +324 -0
  112. data/vendor/crates/spikard-http/tests/sse_behavior.rs +728 -0
  113. data/vendor/crates/spikard-http/tests/websocket_behavior.rs +724 -0
  114. data/vendor/crates/spikard-rb/Cargo.toml +43 -0
  115. data/vendor/crates/spikard-rb/build.rs +199 -0
  116. data/vendor/crates/spikard-rb/src/background.rs +63 -0
  117. data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
  118. data/vendor/crates/spikard-rb/src/config/server_config.rs +283 -0
  119. data/vendor/crates/spikard-rb/src/conversion.rs +459 -0
  120. data/vendor/crates/spikard-rb/src/di/builder.rs +105 -0
  121. data/vendor/crates/spikard-rb/src/di/mod.rs +413 -0
  122. data/vendor/crates/spikard-rb/src/handler.rs +612 -0
  123. data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
  124. data/vendor/crates/spikard-rb/src/lib.rs +1857 -0
  125. data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -0
  126. data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
  127. data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +427 -0
  128. data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
  129. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +326 -0
  130. data/vendor/crates/spikard-rb/src/server.rs +283 -0
  131. data/vendor/crates/spikard-rb/src/sse.rs +231 -0
  132. data/vendor/crates/spikard-rb/src/testing/client.rs +404 -0
  133. data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
  134. data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -0
  135. data/vendor/crates/spikard-rb/src/testing/websocket.rs +221 -0
  136. data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
  137. data/vendor/crates/spikard-rb/tests/magnus_ffi_tests.rs +14 -0
  138. metadata +213 -0
@@ -0,0 +1,413 @@
1
+ //! Ruby dependency injection implementations
2
+ //!
3
+ //! This module provides Ruby-specific implementations of the Dependency trait,
4
+ //! bridging Ruby values and Procs to the Rust DI system.
5
+
6
+ #![allow(dead_code)]
7
+
8
+ pub mod builder;
9
+
10
+ pub use builder::build_dependency_container;
11
+
12
+ use http::Request;
13
+ use magnus::prelude::*;
14
+ use magnus::value::{InnerValue, Opaque};
15
+ use magnus::{Error, RHash, Ruby, TryConvert, Value};
16
+ use serde_json::Value as JsonValue;
17
+ use spikard_core::di::{Dependency, DependencyError, ResolvedDependencies};
18
+ use spikard_core::request_data::RequestData;
19
+ use std::any::Any;
20
+ use std::pin::Pin;
21
+ use std::sync::Arc;
22
+
23
+ /// Ruby value dependency
24
+ ///
25
+ /// Wraps a Ruby object as a static dependency value
26
+ pub struct RubyValueDependency {
27
+ key: String,
28
+ value: Opaque<Value>,
29
+ }
30
+
31
+ impl RubyValueDependency {
32
+ pub fn new(key: String, value: Value) -> Self {
33
+ Self {
34
+ key,
35
+ value: Opaque::from(value),
36
+ }
37
+ }
38
+ }
39
+
40
+ impl Dependency for RubyValueDependency {
41
+ fn key(&self) -> &str {
42
+ &self.key
43
+ }
44
+
45
+ fn depends_on(&self) -> Vec<String> {
46
+ Vec::new() // Value dependencies have no dependencies
47
+ }
48
+
49
+ fn resolve(
50
+ &self,
51
+ _request: &Request<()>,
52
+ _request_data: &RequestData,
53
+ _resolved: &ResolvedDependencies,
54
+ ) -> Pin<Box<dyn std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, DependencyError>> + Send + '_>>
55
+ {
56
+ Box::pin(async move {
57
+ // Get the Ruby value
58
+ let ruby = Ruby::get().map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
59
+
60
+ let value = self.value.get_inner_with(&ruby);
61
+
62
+ // Convert to JSON and back to make it Send + Sync
63
+ let json_value = ruby_value_to_json(&ruby, value)
64
+ .map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
65
+
66
+ Ok(Arc::new(json_value) as Arc<dyn Any + Send + Sync>)
67
+ })
68
+ }
69
+
70
+ fn singleton(&self) -> bool {
71
+ true // Value dependencies are always singletons
72
+ }
73
+
74
+ fn cacheable(&self) -> bool {
75
+ true
76
+ }
77
+ }
78
+
79
+ /// Ruby factory dependency
80
+ ///
81
+ /// Wraps a Ruby Proc as a factory dependency
82
+ pub struct RubyFactoryDependency {
83
+ key: String,
84
+ factory: Opaque<Value>,
85
+ depends_on: Vec<String>,
86
+ singleton: bool,
87
+ cacheable: bool,
88
+ }
89
+
90
+ impl RubyFactoryDependency {
91
+ pub fn new(key: String, factory: Value, depends_on: Vec<String>, singleton: bool, cacheable: bool) -> Self {
92
+ Self {
93
+ key,
94
+ factory: Opaque::from(factory),
95
+ depends_on,
96
+ singleton,
97
+ cacheable,
98
+ }
99
+ }
100
+ }
101
+
102
+ impl Dependency for RubyFactoryDependency {
103
+ fn key(&self) -> &str {
104
+ &self.key
105
+ }
106
+
107
+ fn depends_on(&self) -> Vec<String> {
108
+ self.depends_on.clone()
109
+ }
110
+
111
+ fn resolve(
112
+ &self,
113
+ _request: &Request<()>,
114
+ _request_data: &RequestData,
115
+ resolved: &ResolvedDependencies,
116
+ ) -> Pin<Box<dyn std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, DependencyError>> + Send + '_>>
117
+ {
118
+ // Clone data needed in async block
119
+ let factory = self.factory;
120
+ let depends_on = self.depends_on.clone();
121
+ let key = self.key.clone();
122
+ let is_singleton = self.singleton;
123
+ let resolved_clone = resolved.clone();
124
+
125
+ // Extract resolved dependencies now (before async)
126
+ // Need to handle both JsonValue and RubyValueWrapper types
127
+ let resolved_deps: Vec<(String, JsonValue)> = depends_on
128
+ .iter()
129
+ .filter_map(|dep_key| {
130
+ // Try JsonValue first
131
+ if let Some(json_value) = resolved.get::<JsonValue>(dep_key) {
132
+ return Some((dep_key.clone(), (*json_value).clone()));
133
+ }
134
+ // Try RubyValueWrapper (for singletons)
135
+ if let Some(wrapper) = resolved.get::<RubyValueWrapper>(dep_key) {
136
+ // Convert wrapper to JSON synchronously
137
+ if let Ok(ruby) = Ruby::get()
138
+ && let Ok(json) = wrapper.to_json(&ruby)
139
+ {
140
+ return Some((dep_key.clone(), json));
141
+ }
142
+ }
143
+ None
144
+ })
145
+ .collect();
146
+
147
+ Box::pin(async move {
148
+ let ruby = Ruby::get().map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
149
+
150
+ // Build positional arguments array from resolved dependencies
151
+ // Dependencies must be passed in the order specified by depends_on
152
+ // Important: preserve the order from depends_on, not from resolved_deps iteration
153
+ let args: Result<Vec<Value>, DependencyError> = depends_on
154
+ .iter()
155
+ .filter_map(|dep_key| {
156
+ // Find this dependency in resolved_deps
157
+ resolved_deps.iter().find(|(k, _)| k == dep_key).map(|(_, v)| v)
158
+ })
159
+ .map(|dep_value| {
160
+ json_to_ruby(&ruby, dep_value).map_err(|e| DependencyError::ResolutionFailed {
161
+ message: format!("Failed to convert dependency value: {}", e),
162
+ })
163
+ })
164
+ .collect();
165
+ let args = args?;
166
+
167
+ // Call the factory Proc with positional arguments
168
+ let factory_value = factory.get_inner_with(&ruby);
169
+
170
+ // Check if factory responds to call
171
+ if !factory_value
172
+ .respond_to("call", false)
173
+ .map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?
174
+ {
175
+ return Err(DependencyError::ResolutionFailed {
176
+ message: format!("Dependency factory for '{}' is not callable", key),
177
+ });
178
+ }
179
+
180
+ // Call factory with positional arguments
181
+ // Use a Ruby helper to call with splatted arguments
182
+ let result: Value = if !args.is_empty() {
183
+ // Create a Ruby array of arguments
184
+ let args_array = ruby.ary_new();
185
+ for arg in &args {
186
+ args_array.push(*arg).map_err(|e| DependencyError::ResolutionFailed {
187
+ message: format!("Failed to push arg to array: {}", e),
188
+ })?;
189
+ }
190
+
191
+ // Use Ruby's send with * to splat arguments
192
+ // Equivalent to: factory_value.call(*args_array)
193
+ let splat_lambda = ruby
194
+ .eval::<Value>("lambda { |proc, args| proc.call(*args) }")
195
+ .map_err(|e| DependencyError::ResolutionFailed {
196
+ message: format!("Failed to create splat lambda: {}", e),
197
+ })?;
198
+
199
+ splat_lambda.funcall("call", (factory_value, args_array))
200
+ } else {
201
+ factory_value.funcall("call", ())
202
+ }
203
+ .map_err(|e| DependencyError::ResolutionFailed {
204
+ message: format!("Failed to call factory for '{}': {}", key, e),
205
+ })?;
206
+
207
+ // Check if result is an array with cleanup callback (Ruby pattern: [resource, cleanup_proc])
208
+ let (value_to_convert, cleanup_callback) = if result.is_kind_of(ruby.class_array()) {
209
+ let array = magnus::RArray::from_value(result).ok_or_else(|| DependencyError::ResolutionFailed {
210
+ message: format!("Failed to convert result to array for '{}'", key),
211
+ })?;
212
+
213
+ let len = array.len();
214
+ if len == 2 {
215
+ // Extract the resource (first element)
216
+ let resource: Value = array.entry(0).map_err(|e| DependencyError::ResolutionFailed {
217
+ message: format!("Failed to extract resource from array for '{}': {}", key, e),
218
+ })?;
219
+
220
+ // Extract cleanup callback (second element)
221
+ let cleanup: Value = array.entry(1).map_err(|e| DependencyError::ResolutionFailed {
222
+ message: format!("Failed to extract cleanup callback for '{}': {}", key, e),
223
+ })?;
224
+
225
+ (resource, Some(cleanup))
226
+ } else {
227
+ // Not a cleanup pattern, use the array as-is
228
+ (result, None)
229
+ }
230
+ } else {
231
+ // Not an array, use the value as-is
232
+ (result, None)
233
+ };
234
+
235
+ // Register cleanup callback if present
236
+ if let Some(cleanup_proc) = cleanup_callback {
237
+ let cleanup_opaque = Opaque::from(cleanup_proc);
238
+
239
+ resolved_clone.add_cleanup_task(Box::new(move || {
240
+ Box::pin(async move {
241
+ // Get Ruby runtime and call cleanup proc
242
+ if let Ok(ruby) = Ruby::get() {
243
+ let proc = cleanup_opaque.get_inner_with(&ruby);
244
+ // Call the cleanup proc - ignore errors during cleanup
245
+ let _ = proc.funcall::<_, _, Value>("call", ());
246
+ }
247
+ })
248
+ }));
249
+ }
250
+
251
+ // For singleton dependencies, store Ruby value wrapper to preserve mutations
252
+ // For non-singleton, convert to JSON immediately (no need to preserve mutations)
253
+ if is_singleton {
254
+ let wrapper = RubyValueWrapper::new(value_to_convert);
255
+ return Ok(Arc::new(wrapper) as Arc<dyn Any + Send + Sync>);
256
+ }
257
+
258
+ // Convert result to JSON for non-singleton dependencies
259
+ let json_value = ruby_value_to_json(&ruby, value_to_convert)
260
+ .map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
261
+
262
+ Ok(Arc::new(json_value) as Arc<dyn Any + Send + Sync>)
263
+ })
264
+ }
265
+
266
+ fn singleton(&self) -> bool {
267
+ self.singleton
268
+ }
269
+
270
+ fn cacheable(&self) -> bool {
271
+ self.cacheable
272
+ }
273
+ }
274
+
275
+ /// Wrapper around a Ruby Value that preserves object identity for singleton mutations
276
+ ///
277
+ /// This stores the Ruby object itself rather than a JSON snapshot, allowing
278
+ /// singleton dependencies to maintain mutable state across requests.
279
+ #[derive(Clone)]
280
+ pub struct RubyValueWrapper {
281
+ /// Thread-safe wrapper around Ruby Value
282
+ /// Opaque<Value> is Send + Sync per magnus design
283
+ value: Opaque<Value>,
284
+ }
285
+
286
+ impl RubyValueWrapper {
287
+ /// Create a new wrapper around a Ruby value
288
+ pub fn new(value: Value) -> Self {
289
+ Self {
290
+ value: Opaque::from(value),
291
+ }
292
+ }
293
+
294
+ /// Get the raw Ruby value directly
295
+ ///
296
+ /// This preserves object identity for singletons, allowing mutations
297
+ /// to persist across requests.
298
+ pub fn get_value(&self, ruby: &Ruby) -> Value {
299
+ self.value.get_inner_with(ruby)
300
+ }
301
+
302
+ /// Convert the wrapped Ruby value to JSON
303
+ ///
304
+ /// This is called fresh each time to capture any mutations to the object.
305
+ /// For singletons, this means we see updated counter values, etc.
306
+ pub fn to_json(&self, ruby: &Ruby) -> Result<JsonValue, Error> {
307
+ let value = self.value.get_inner_with(ruby);
308
+ ruby_value_to_json(ruby, value)
309
+ }
310
+ }
311
+
312
+ // Safety: Opaque<Value> is designed to be Send + Sync by magnus
313
+ // It holds a stable pointer that's safe to share across threads
314
+ unsafe impl Send for RubyValueWrapper {}
315
+ unsafe impl Sync for RubyValueWrapper {}
316
+
317
+ /// Convert Ruby Value to serde_json::Value
318
+ fn ruby_value_to_json(ruby: &Ruby, value: Value) -> Result<JsonValue, Error> {
319
+ if value.is_nil() {
320
+ return Ok(JsonValue::Null);
321
+ }
322
+
323
+ let json_module: Value = ruby
324
+ .class_object()
325
+ .const_get("JSON")
326
+ .map_err(|_| Error::new(ruby.exception_runtime_error(), "JSON module not available"))?;
327
+
328
+ let json_string: String = json_module.funcall("generate", (value,))?;
329
+ serde_json::from_str(&json_string).map_err(|err| {
330
+ Error::new(
331
+ ruby.exception_runtime_error(),
332
+ format!("Failed to convert Ruby value to JSON: {err}"),
333
+ )
334
+ })
335
+ }
336
+
337
+ /// Convert serde_json::Value to Ruby Value
338
+ pub fn json_to_ruby(ruby: &Ruby, value: &JsonValue) -> Result<Value, Error> {
339
+ match value {
340
+ JsonValue::Null => Ok(ruby.qnil().as_value()),
341
+ JsonValue::Bool(b) => Ok(if *b {
342
+ ruby.qtrue().as_value()
343
+ } else {
344
+ ruby.qfalse().as_value()
345
+ }),
346
+ JsonValue::Number(num) => {
347
+ if let Some(i) = num.as_i64() {
348
+ Ok(ruby.integer_from_i64(i).as_value())
349
+ } else if let Some(f) = num.as_f64() {
350
+ Ok(ruby.float_from_f64(f).as_value())
351
+ } else {
352
+ Ok(ruby.qnil().as_value())
353
+ }
354
+ }
355
+ JsonValue::String(str_val) => Ok(ruby.str_new(str_val).as_value()),
356
+ JsonValue::Array(items) => {
357
+ let array = ruby.ary_new();
358
+ for item in items {
359
+ array.push(json_to_ruby(ruby, item)?)?;
360
+ }
361
+ Ok(array.as_value())
362
+ }
363
+ JsonValue::Object(map) => {
364
+ let hash = ruby.hash_new();
365
+ for (key, item) in map {
366
+ hash.aset(ruby.str_new(key), json_to_ruby(ruby, item)?)?;
367
+ }
368
+ Ok(hash.as_value())
369
+ }
370
+ }
371
+ }
372
+
373
+ /// Helper to extract keyword arguments from Ruby options hash
374
+ pub fn extract_di_options(ruby: &Ruby, options: Value) -> Result<(Vec<String>, bool, bool), Error> {
375
+ if options.is_nil() {
376
+ return Ok((Vec::new(), false, true));
377
+ }
378
+
379
+ let hash = RHash::try_convert(options)?;
380
+
381
+ // Extract depends_on
382
+ let depends_on = if let Some(deps_value) = get_kw(ruby, hash, "depends_on") {
383
+ if deps_value.is_nil() {
384
+ Vec::new()
385
+ } else {
386
+ Vec::<String>::try_convert(deps_value)?
387
+ }
388
+ } else {
389
+ Vec::new()
390
+ };
391
+
392
+ // Extract singleton (default false)
393
+ let singleton = if let Some(singleton_value) = get_kw(ruby, hash, "singleton") {
394
+ bool::try_convert(singleton_value).unwrap_or(false)
395
+ } else {
396
+ false
397
+ };
398
+
399
+ // Extract cacheable (default true)
400
+ let cacheable = if let Some(cacheable_value) = get_kw(ruby, hash, "cacheable") {
401
+ bool::try_convert(cacheable_value).unwrap_or(true)
402
+ } else {
403
+ true
404
+ };
405
+
406
+ Ok((depends_on, singleton, cacheable))
407
+ }
408
+
409
+ /// Get keyword argument from Ruby hash (tries both symbol and string keys)
410
+ fn get_kw(ruby: &Ruby, hash: RHash, name: &str) -> Option<Value> {
411
+ let sym = ruby.intern(name);
412
+ hash.get(sym).or_else(|| hash.get(name))
413
+ }