spikard 0.2.5 → 0.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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +659 -626
  4. data/ext/spikard_rb/Cargo.toml +17 -17
  5. data/ext/spikard_rb/extconf.rb +10 -10
  6. data/ext/spikard_rb/src/lib.rs +6 -6
  7. data/lib/spikard/app.rb +386 -374
  8. data/lib/spikard/background.rb +27 -27
  9. data/lib/spikard/config.rb +396 -396
  10. data/lib/spikard/converters.rb +13 -85
  11. data/lib/spikard/handler_wrapper.rb +113 -116
  12. data/lib/spikard/provide.rb +214 -228
  13. data/lib/spikard/response.rb +173 -109
  14. data/lib/spikard/schema.rb +243 -243
  15. data/lib/spikard/sse.rb +111 -111
  16. data/lib/spikard/streaming_response.rb +44 -21
  17. data/lib/spikard/testing.rb +221 -221
  18. data/lib/spikard/upload_file.rb +131 -131
  19. data/lib/spikard/version.rb +5 -5
  20. data/lib/spikard/websocket.rb +59 -59
  21. data/lib/spikard.rb +43 -43
  22. data/sig/spikard.rbs +360 -349
  23. data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
  24. metadata +5 -85
  25. data/vendor/crates/spikard-core/Cargo.toml +0 -40
  26. data/vendor/crates/spikard-core/src/bindings/mod.rs +0 -3
  27. data/vendor/crates/spikard-core/src/bindings/response.rs +0 -133
  28. data/vendor/crates/spikard-core/src/debug.rs +0 -63
  29. data/vendor/crates/spikard-core/src/di/container.rs +0 -726
  30. data/vendor/crates/spikard-core/src/di/dependency.rs +0 -273
  31. data/vendor/crates/spikard-core/src/di/error.rs +0 -118
  32. data/vendor/crates/spikard-core/src/di/factory.rs +0 -538
  33. data/vendor/crates/spikard-core/src/di/graph.rs +0 -545
  34. data/vendor/crates/spikard-core/src/di/mod.rs +0 -192
  35. data/vendor/crates/spikard-core/src/di/resolved.rs +0 -411
  36. data/vendor/crates/spikard-core/src/di/value.rs +0 -283
  37. data/vendor/crates/spikard-core/src/http.rs +0 -153
  38. data/vendor/crates/spikard-core/src/lib.rs +0 -28
  39. data/vendor/crates/spikard-core/src/lifecycle.rs +0 -422
  40. data/vendor/crates/spikard-core/src/parameters.rs +0 -719
  41. data/vendor/crates/spikard-core/src/problem.rs +0 -310
  42. data/vendor/crates/spikard-core/src/request_data.rs +0 -189
  43. data/vendor/crates/spikard-core/src/router.rs +0 -249
  44. data/vendor/crates/spikard-core/src/schema_registry.rs +0 -183
  45. data/vendor/crates/spikard-core/src/type_hints.rs +0 -304
  46. data/vendor/crates/spikard-core/src/validation.rs +0 -699
  47. data/vendor/crates/spikard-http/Cargo.toml +0 -58
  48. data/vendor/crates/spikard-http/src/auth.rs +0 -247
  49. data/vendor/crates/spikard-http/src/background.rs +0 -249
  50. data/vendor/crates/spikard-http/src/bindings/mod.rs +0 -3
  51. data/vendor/crates/spikard-http/src/bindings/response.rs +0 -1
  52. data/vendor/crates/spikard-http/src/body_metadata.rs +0 -8
  53. data/vendor/crates/spikard-http/src/cors.rs +0 -490
  54. data/vendor/crates/spikard-http/src/debug.rs +0 -63
  55. data/vendor/crates/spikard-http/src/di_handler.rs +0 -423
  56. data/vendor/crates/spikard-http/src/handler_response.rs +0 -190
  57. data/vendor/crates/spikard-http/src/handler_trait.rs +0 -228
  58. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +0 -284
  59. data/vendor/crates/spikard-http/src/lib.rs +0 -529
  60. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +0 -149
  61. data/vendor/crates/spikard-http/src/lifecycle.rs +0 -428
  62. data/vendor/crates/spikard-http/src/middleware/mod.rs +0 -285
  63. data/vendor/crates/spikard-http/src/middleware/multipart.rs +0 -86
  64. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +0 -147
  65. data/vendor/crates/spikard-http/src/middleware/validation.rs +0 -287
  66. data/vendor/crates/spikard-http/src/openapi/mod.rs +0 -309
  67. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +0 -190
  68. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +0 -308
  69. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +0 -195
  70. data/vendor/crates/spikard-http/src/parameters.rs +0 -1
  71. data/vendor/crates/spikard-http/src/problem.rs +0 -1
  72. data/vendor/crates/spikard-http/src/query_parser.rs +0 -369
  73. data/vendor/crates/spikard-http/src/response.rs +0 -399
  74. data/vendor/crates/spikard-http/src/router.rs +0 -1
  75. data/vendor/crates/spikard-http/src/schema_registry.rs +0 -1
  76. data/vendor/crates/spikard-http/src/server/handler.rs +0 -80
  77. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +0 -98
  78. data/vendor/crates/spikard-http/src/server/mod.rs +0 -805
  79. data/vendor/crates/spikard-http/src/server/request_extraction.rs +0 -119
  80. data/vendor/crates/spikard-http/src/sse.rs +0 -447
  81. data/vendor/crates/spikard-http/src/testing/form.rs +0 -14
  82. data/vendor/crates/spikard-http/src/testing/multipart.rs +0 -60
  83. data/vendor/crates/spikard-http/src/testing/test_client.rs +0 -285
  84. data/vendor/crates/spikard-http/src/testing.rs +0 -377
  85. data/vendor/crates/spikard-http/src/type_hints.rs +0 -1
  86. data/vendor/crates/spikard-http/src/validation.rs +0 -1
  87. data/vendor/crates/spikard-http/src/websocket.rs +0 -324
  88. data/vendor/crates/spikard-rb/Cargo.toml +0 -42
  89. data/vendor/crates/spikard-rb/build.rs +0 -8
  90. data/vendor/crates/spikard-rb/src/background.rs +0 -63
  91. data/vendor/crates/spikard-rb/src/config.rs +0 -294
  92. data/vendor/crates/spikard-rb/src/conversion.rs +0 -392
  93. data/vendor/crates/spikard-rb/src/di.rs +0 -409
  94. data/vendor/crates/spikard-rb/src/handler.rs +0 -534
  95. data/vendor/crates/spikard-rb/src/lib.rs +0 -2020
  96. data/vendor/crates/spikard-rb/src/lifecycle.rs +0 -267
  97. data/vendor/crates/spikard-rb/src/server.rs +0 -283
  98. data/vendor/crates/spikard-rb/src/sse.rs +0 -231
  99. data/vendor/crates/spikard-rb/src/test_client.rs +0 -404
  100. data/vendor/crates/spikard-rb/src/test_sse.rs +0 -143
  101. data/vendor/crates/spikard-rb/src/test_websocket.rs +0 -221
  102. data/vendor/crates/spikard-rb/src/websocket.rs +0 -233
  103. /data/vendor/bundle/ruby/{3.3.0 → 3.4.0}/gems/diff-lcs-1.6.2/mise.toml +0 -0
@@ -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
- }