spikard 0.2.5 → 0.3.0

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 (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +34 -1
  3. data/ext/spikard_rb/Cargo.toml +3 -3
  4. data/lib/spikard/app.rb +61 -49
  5. data/lib/spikard/converters.rb +3 -75
  6. data/lib/spikard/handler_wrapper.rb +6 -9
  7. data/lib/spikard/provide.rb +14 -28
  8. data/lib/spikard/response.rb +75 -11
  9. data/lib/spikard/streaming_response.rb +24 -1
  10. data/lib/spikard/testing.rb +1 -1
  11. data/lib/spikard/version.rb +1 -1
  12. data/sig/spikard.rbs +14 -3
  13. data/vendor/bundle/ruby/3.3.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
  14. metadata +3 -80
  15. data/vendor/crates/spikard-core/Cargo.toml +0 -40
  16. data/vendor/crates/spikard-core/src/bindings/mod.rs +0 -3
  17. data/vendor/crates/spikard-core/src/bindings/response.rs +0 -133
  18. data/vendor/crates/spikard-core/src/debug.rs +0 -63
  19. data/vendor/crates/spikard-core/src/di/container.rs +0 -726
  20. data/vendor/crates/spikard-core/src/di/dependency.rs +0 -273
  21. data/vendor/crates/spikard-core/src/di/error.rs +0 -118
  22. data/vendor/crates/spikard-core/src/di/factory.rs +0 -538
  23. data/vendor/crates/spikard-core/src/di/graph.rs +0 -545
  24. data/vendor/crates/spikard-core/src/di/mod.rs +0 -192
  25. data/vendor/crates/spikard-core/src/di/resolved.rs +0 -411
  26. data/vendor/crates/spikard-core/src/di/value.rs +0 -283
  27. data/vendor/crates/spikard-core/src/http.rs +0 -153
  28. data/vendor/crates/spikard-core/src/lib.rs +0 -28
  29. data/vendor/crates/spikard-core/src/lifecycle.rs +0 -422
  30. data/vendor/crates/spikard-core/src/parameters.rs +0 -719
  31. data/vendor/crates/spikard-core/src/problem.rs +0 -310
  32. data/vendor/crates/spikard-core/src/request_data.rs +0 -189
  33. data/vendor/crates/spikard-core/src/router.rs +0 -249
  34. data/vendor/crates/spikard-core/src/schema_registry.rs +0 -183
  35. data/vendor/crates/spikard-core/src/type_hints.rs +0 -304
  36. data/vendor/crates/spikard-core/src/validation.rs +0 -699
  37. data/vendor/crates/spikard-http/Cargo.toml +0 -58
  38. data/vendor/crates/spikard-http/src/auth.rs +0 -247
  39. data/vendor/crates/spikard-http/src/background.rs +0 -249
  40. data/vendor/crates/spikard-http/src/bindings/mod.rs +0 -3
  41. data/vendor/crates/spikard-http/src/bindings/response.rs +0 -1
  42. data/vendor/crates/spikard-http/src/body_metadata.rs +0 -8
  43. data/vendor/crates/spikard-http/src/cors.rs +0 -490
  44. data/vendor/crates/spikard-http/src/debug.rs +0 -63
  45. data/vendor/crates/spikard-http/src/di_handler.rs +0 -423
  46. data/vendor/crates/spikard-http/src/handler_response.rs +0 -190
  47. data/vendor/crates/spikard-http/src/handler_trait.rs +0 -228
  48. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +0 -284
  49. data/vendor/crates/spikard-http/src/lib.rs +0 -529
  50. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +0 -149
  51. data/vendor/crates/spikard-http/src/lifecycle.rs +0 -428
  52. data/vendor/crates/spikard-http/src/middleware/mod.rs +0 -285
  53. data/vendor/crates/spikard-http/src/middleware/multipart.rs +0 -86
  54. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +0 -147
  55. data/vendor/crates/spikard-http/src/middleware/validation.rs +0 -287
  56. data/vendor/crates/spikard-http/src/openapi/mod.rs +0 -309
  57. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +0 -190
  58. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +0 -308
  59. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +0 -195
  60. data/vendor/crates/spikard-http/src/parameters.rs +0 -1
  61. data/vendor/crates/spikard-http/src/problem.rs +0 -1
  62. data/vendor/crates/spikard-http/src/query_parser.rs +0 -369
  63. data/vendor/crates/spikard-http/src/response.rs +0 -399
  64. data/vendor/crates/spikard-http/src/router.rs +0 -1
  65. data/vendor/crates/spikard-http/src/schema_registry.rs +0 -1
  66. data/vendor/crates/spikard-http/src/server/handler.rs +0 -80
  67. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +0 -98
  68. data/vendor/crates/spikard-http/src/server/mod.rs +0 -805
  69. data/vendor/crates/spikard-http/src/server/request_extraction.rs +0 -119
  70. data/vendor/crates/spikard-http/src/sse.rs +0 -447
  71. data/vendor/crates/spikard-http/src/testing/form.rs +0 -14
  72. data/vendor/crates/spikard-http/src/testing/multipart.rs +0 -60
  73. data/vendor/crates/spikard-http/src/testing/test_client.rs +0 -285
  74. data/vendor/crates/spikard-http/src/testing.rs +0 -377
  75. data/vendor/crates/spikard-http/src/type_hints.rs +0 -1
  76. data/vendor/crates/spikard-http/src/validation.rs +0 -1
  77. data/vendor/crates/spikard-http/src/websocket.rs +0 -324
  78. data/vendor/crates/spikard-rb/Cargo.toml +0 -42
  79. data/vendor/crates/spikard-rb/build.rs +0 -8
  80. data/vendor/crates/spikard-rb/src/background.rs +0 -63
  81. data/vendor/crates/spikard-rb/src/config.rs +0 -294
  82. data/vendor/crates/spikard-rb/src/conversion.rs +0 -392
  83. data/vendor/crates/spikard-rb/src/di.rs +0 -409
  84. data/vendor/crates/spikard-rb/src/handler.rs +0 -534
  85. data/vendor/crates/spikard-rb/src/lib.rs +0 -2020
  86. data/vendor/crates/spikard-rb/src/lifecycle.rs +0 -267
  87. data/vendor/crates/spikard-rb/src/server.rs +0 -283
  88. data/vendor/crates/spikard-rb/src/sse.rs +0 -231
  89. data/vendor/crates/spikard-rb/src/test_client.rs +0 -404
  90. data/vendor/crates/spikard-rb/src/test_sse.rs +0 -143
  91. data/vendor/crates/spikard-rb/src/test_websocket.rs +0 -221
  92. data/vendor/crates/spikard-rb/src/websocket.rs +0 -233
@@ -1,304 +0,0 @@
1
- //! Path parameter type hint parsing
2
- //!
3
- //! Supports FastAPI-style type syntax in route paths:
4
- //! - `/items/{id:uuid}` → auto-generates UUID validation
5
- //! - `/users/{user_id:int}` → auto-generates integer type
6
- //! - `/files/{path:path}` → wildcard path capture
7
- //!
8
- //! Explicit parameter schemas override auto-generated ones.
9
-
10
- use regex::Regex;
11
- use serde_json::{Value, json};
12
- use std::collections::HashMap;
13
- use std::sync::OnceLock;
14
-
15
- /// Regex for matching type hints in path parameters
16
- static TYPE_HINT_REGEX: OnceLock<Regex> = OnceLock::new();
17
-
18
- /// Regex for matching wildcard path parameters
19
- static PATH_TYPE_REGEX: OnceLock<Regex> = OnceLock::new();
20
-
21
- /// Get the type hint regex (compiled once)
22
- fn type_hint_regex() -> &'static Regex {
23
- TYPE_HINT_REGEX.get_or_init(|| Regex::new(r"\{([^:}]+):([^}]+)\}").unwrap())
24
- }
25
-
26
- /// Get the path type regex (compiled once)
27
- fn path_type_regex() -> &'static Regex {
28
- PATH_TYPE_REGEX.get_or_init(|| Regex::new(r"\{([^:}]+):path\}").unwrap())
29
- }
30
-
31
- /// Parse type hints from a route path
32
- ///
33
- /// # Examples
34
- ///
35
- /// ```
36
- /// use spikard_core::type_hints::parse_type_hints;
37
- ///
38
- /// let hints = parse_type_hints("/items/{id:uuid}/tags/{tag_id:int}");
39
- /// assert_eq!(hints.get("id"), Some(&"uuid".to_string()));
40
- /// assert_eq!(hints.get("tag_id"), Some(&"int".to_string()));
41
- /// ```
42
- pub fn parse_type_hints(route_path: &str) -> HashMap<String, String> {
43
- let mut hints = HashMap::new();
44
- let re = type_hint_regex();
45
-
46
- for cap in re.captures_iter(route_path) {
47
- let param_name = cap.get(1).unwrap().as_str().to_string();
48
- let type_hint = cap.get(2).unwrap().as_str().to_string();
49
- hints.insert(param_name, type_hint);
50
- }
51
-
52
- hints
53
- }
54
-
55
- /// Strip type hints from path for Axum compatibility
56
- ///
57
- /// Converts FastAPI-style syntax to Axum syntax:
58
- /// - `/items/{id:uuid}` → `/items/{id}`
59
- /// - `/files/{path:path}` → `/files/{*path}` (wildcard for Axum v0.7)
60
- ///
61
- /// # Examples
62
- ///
63
- /// ```
64
- /// use spikard_core::type_hints::strip_type_hints;
65
- ///
66
- /// assert_eq!(strip_type_hints("/items/{id:uuid}"), "/items/{id}");
67
- /// assert_eq!(strip_type_hints("/files/{path:path}"), "/files/{*path}");
68
- /// ```
69
- pub fn strip_type_hints(route_path: &str) -> String {
70
- let path_re = path_type_regex();
71
- let route_path = path_re.replace_all(route_path, "{*$1}");
72
-
73
- let re = type_hint_regex();
74
- re.replace_all(&route_path, "{$1}").to_string()
75
- }
76
-
77
- /// Generate JSON Schema from a type hint
78
- ///
79
- /// # Supported Types
80
- ///
81
- /// - `uuid` → `{"type": "string", "format": "uuid"}`
82
- /// - `int` / `integer` → `{"type": "integer"}`
83
- /// - `str` / `string` → `{"type": "string"}`
84
- /// - `float` / `number` → `{"type": "number"}`
85
- /// - `bool` / `boolean` → `{"type": "boolean"}`
86
- /// - `date` → `{"type": "string", "format": "date"}`
87
- /// - `datetime` → `{"type": "string", "format": "date-time"}`
88
- /// - `path` → `{"type": "string"}` (wildcard capture)
89
- pub fn type_hint_to_schema(type_hint: &str) -> Value {
90
- match type_hint {
91
- "uuid" => json!({
92
- "type": "string",
93
- "format": "uuid"
94
- }),
95
- "int" | "integer" => json!({
96
- "type": "integer"
97
- }),
98
- "str" | "string" => json!({
99
- "type": "string"
100
- }),
101
- "float" | "number" => json!({
102
- "type": "number"
103
- }),
104
- "bool" | "boolean" => json!({
105
- "type": "boolean"
106
- }),
107
- "date" => json!({
108
- "type": "string",
109
- "format": "date"
110
- }),
111
- "datetime" | "date-time" => json!({
112
- "type": "string",
113
- "format": "date-time"
114
- }),
115
- "path" => json!({
116
- "type": "string"
117
- }),
118
- _ => json!({
119
- "type": "string"
120
- }),
121
- }
122
- }
123
-
124
- /// Auto-generate parameter schema from type hints in route path
125
- ///
126
- /// Creates a JSON Schema with path parameters based on type hints.
127
- /// Returns None if no type hints are found.
128
- ///
129
- /// # Examples
130
- ///
131
- /// ```
132
- /// use spikard_core::type_hints::auto_generate_parameter_schema;
133
- /// use serde_json::json;
134
- ///
135
- /// let schema = auto_generate_parameter_schema("/items/{id:uuid}");
136
- /// assert_eq!(schema, Some(json!({
137
- /// "type": "object",
138
- /// "properties": {
139
- /// "id": {
140
- /// "type": "string",
141
- /// "format": "uuid",
142
- /// "source": "path"
143
- /// }
144
- /// },
145
- /// "required": ["id"]
146
- /// })));
147
- /// ```
148
- pub fn auto_generate_parameter_schema(route_path: &str) -> Option<Value> {
149
- let type_hints = parse_type_hints(route_path);
150
-
151
- if type_hints.is_empty() {
152
- return None;
153
- }
154
-
155
- let mut properties = serde_json::Map::new();
156
- let mut required = Vec::new();
157
-
158
- for (param_name, type_hint) in type_hints {
159
- let mut param_schema = type_hint_to_schema(&type_hint);
160
-
161
- if let Some(obj) = param_schema.as_object_mut() {
162
- obj.insert("source".to_string(), json!("path"));
163
- }
164
-
165
- properties.insert(param_name.clone(), param_schema);
166
- required.push(json!(param_name));
167
- }
168
-
169
- Some(json!({
170
- "type": "object",
171
- "properties": properties,
172
- "required": required
173
- }))
174
- }
175
-
176
- /// Merge auto-generated schema with explicit schema
177
- ///
178
- /// Explicit schema takes precedence. Only auto-generates schemas for
179
- /// parameters not explicitly defined.
180
- ///
181
- /// # Examples
182
- ///
183
- /// ```
184
- /// use spikard_core::type_hints::merge_parameter_schemas;
185
- /// use serde_json::json;
186
- ///
187
- /// let auto_schema = json!({
188
- /// "type": "object",
189
- /// "properties": {
190
- /// "id": {"type": "string", "format": "uuid", "source": "path"},
191
- /// "count": {"type": "integer", "source": "path"}
192
- /// },
193
- /// "required": ["id", "count"]
194
- /// });
195
- ///
196
- /// let explicit_schema = json!({
197
- /// "type": "object",
198
- /// "properties": {
199
- /// "count": {"type": "integer", "minimum": 1, "maximum": 100, "source": "path"}
200
- /// },
201
- /// "required": ["count"]
202
- /// });
203
- ///
204
- /// let merged = merge_parameter_schemas(auto_schema, explicit_schema);
205
- /// // Result: auto-generated id + explicit count with constraints
206
- /// ```
207
- pub fn merge_parameter_schemas(auto_schema: Value, explicit_schema: Value) -> Value {
208
- let mut result = auto_schema.clone();
209
-
210
- let auto_props = result.get_mut("properties").and_then(|v| v.as_object_mut());
211
- let explicit_props = explicit_schema.get("properties").and_then(|v| v.as_object());
212
-
213
- if let (Some(auto_props), Some(explicit_props)) = (auto_props, explicit_props) {
214
- for (key, value) in explicit_props {
215
- auto_props.insert(key.clone(), value.clone());
216
- }
217
- }
218
-
219
- if let Some(explicit_required) = explicit_schema.get("required").and_then(|v| v.as_array())
220
- && let Some(auto_required) = result.get_mut("required").and_then(|v| v.as_array_mut())
221
- {
222
- for req in explicit_required {
223
- if !auto_required.contains(req) {
224
- auto_required.push(req.clone());
225
- }
226
- }
227
- }
228
-
229
- result
230
- }
231
-
232
- #[cfg(test)]
233
- mod tests {
234
- use super::*;
235
-
236
- #[test]
237
- fn test_parse_type_hints() {
238
- let hints = parse_type_hints("/items/{id:uuid}");
239
- assert_eq!(hints.get("id"), Some(&"uuid".to_string()));
240
-
241
- let hints = parse_type_hints("/users/{user_id:int}/posts/{post_id:int}");
242
- assert_eq!(hints.get("user_id"), Some(&"int".to_string()));
243
- assert_eq!(hints.get("post_id"), Some(&"int".to_string()));
244
- }
245
-
246
- #[test]
247
- fn test_strip_type_hints() {
248
- assert_eq!(strip_type_hints("/items/{id:uuid}"), "/items/{id}");
249
- assert_eq!(strip_type_hints("/files/{path:path}"), "/files/{*path}");
250
- assert_eq!(
251
- strip_type_hints("/users/{user_id:int}/posts/{post_id:int}"),
252
- "/users/{user_id}/posts/{post_id}"
253
- );
254
- }
255
-
256
- #[test]
257
- fn test_type_hint_to_schema() {
258
- let schema = type_hint_to_schema("uuid");
259
- assert_eq!(schema["type"], "string");
260
- assert_eq!(schema["format"], "uuid");
261
-
262
- let schema = type_hint_to_schema("int");
263
- assert_eq!(schema["type"], "integer");
264
- }
265
-
266
- #[test]
267
- fn test_auto_generate_parameter_schema() {
268
- let schema = auto_generate_parameter_schema("/items/{id:uuid}").unwrap();
269
- assert_eq!(schema["type"], "object");
270
- assert_eq!(schema["properties"]["id"]["type"], "string");
271
- assert_eq!(schema["properties"]["id"]["format"], "uuid");
272
- assert_eq!(schema["required"], json!(["id"]));
273
- }
274
-
275
- #[test]
276
- fn test_no_type_hints() {
277
- let schema = auto_generate_parameter_schema("/items/{id}");
278
- assert!(schema.is_none());
279
- }
280
-
281
- #[test]
282
- fn test_merge_schemas() {
283
- let auto_schema = json!({
284
- "type": "object",
285
- "properties": {
286
- "id": {"type": "string", "format": "uuid", "source": "path"}
287
- },
288
- "required": ["id"]
289
- });
290
-
291
- let explicit_schema = json!({
292
- "type": "object",
293
- "properties": {
294
- "count": {"type": "integer", "minimum": 1, "source": "path"}
295
- },
296
- "required": ["count"]
297
- });
298
-
299
- let merged = merge_parameter_schemas(auto_schema, explicit_schema);
300
- assert!(merged["properties"]["id"].is_object());
301
- assert!(merged["properties"]["count"].is_object());
302
- assert_eq!(merged["properties"]["count"]["minimum"], 1);
303
- }
304
- }