spikard 0.8.1 → 0.8.3
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/ext/spikard_rb/Cargo.lock +6 -6
- data/ext/spikard_rb/Cargo.toml +1 -1
- data/lib/spikard/grpc.rb +5 -5
- data/lib/spikard/version.rb +1 -1
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +9 -1
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +61 -23
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +16 -0
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +1 -1
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +22 -19
- data/vendor/crates/spikard-bindings-shared/src/grpc_metadata.rs +16 -14
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +15 -6
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +6 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +42 -36
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +6 -1
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +18 -6
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +28 -10
- data/vendor/crates/spikard-core/Cargo.toml +9 -1
- data/vendor/crates/spikard-core/src/bindings/response.rs +6 -9
- data/vendor/crates/spikard-core/src/debug.rs +2 -2
- data/vendor/crates/spikard-core/src/di/container.rs +1 -1
- data/vendor/crates/spikard-core/src/di/error.rs +1 -1
- data/vendor/crates/spikard-core/src/di/factory.rs +7 -3
- data/vendor/crates/spikard-core/src/di/graph.rs +1 -0
- data/vendor/crates/spikard-core/src/di/resolved.rs +23 -0
- data/vendor/crates/spikard-core/src/di/value.rs +1 -0
- data/vendor/crates/spikard-core/src/errors.rs +3 -0
- data/vendor/crates/spikard-core/src/http.rs +19 -18
- data/vendor/crates/spikard-core/src/lifecycle.rs +42 -18
- data/vendor/crates/spikard-core/src/metadata.rs +3 -14
- data/vendor/crates/spikard-core/src/parameters.rs +61 -35
- data/vendor/crates/spikard-core/src/problem.rs +18 -4
- data/vendor/crates/spikard-core/src/request_data.rs +9 -8
- data/vendor/crates/spikard-core/src/router.rs +20 -6
- data/vendor/crates/spikard-core/src/schema_registry.rs +23 -8
- data/vendor/crates/spikard-core/src/type_hints.rs +11 -5
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +29 -15
- data/vendor/crates/spikard-core/src/validation/mod.rs +45 -32
- data/vendor/crates/spikard-http/Cargo.toml +8 -1
- data/vendor/crates/spikard-http/src/grpc/mod.rs +1 -1
- data/vendor/crates/spikard-http/src/grpc/service.rs +11 -11
- data/vendor/crates/spikard-http/src/grpc/streaming.rs +5 -1
- data/vendor/crates/spikard-http/src/server/grpc_routing.rs +59 -20
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +179 -201
- data/vendor/crates/spikard-http/tests/common/grpc_helpers.rs +49 -60
- data/vendor/crates/spikard-http/tests/common/handlers.rs +5 -5
- data/vendor/crates/spikard-http/tests/common/mod.rs +7 -8
- data/vendor/crates/spikard-http/tests/common/test_builders.rs +14 -19
- data/vendor/crates/spikard-http/tests/grpc_error_handling_test.rs +68 -69
- data/vendor/crates/spikard-http/tests/grpc_integration_test.rs +1 -3
- data/vendor/crates/spikard-http/tests/grpc_metadata_test.rs +98 -84
- data/vendor/crates/spikard-http/tests/grpc_server_integration.rs +76 -57
- data/vendor/crates/spikard-rb/Cargo.toml +9 -1
- data/vendor/crates/spikard-rb/build.rs +1 -0
- data/vendor/crates/spikard-rb/src/grpc/handler.rs +30 -25
- data/vendor/crates/spikard-rb/src/lib.rs +59 -2
- data/vendor/crates/spikard-rb/src/lifecycle.rs +2 -2
- data/vendor/crates/spikard-rb-macros/Cargo.toml +9 -1
- data/vendor/crates/spikard-rb-macros/src/lib.rs +4 -5
- metadata +1 -1
|
@@ -134,7 +134,7 @@ impl std::fmt::Debug for FactoryDependency {
|
|
|
134
134
|
.field("dependencies", &self.dependencies)
|
|
135
135
|
.field("cacheable", &self.cacheable)
|
|
136
136
|
.field("singleton", &self.singleton)
|
|
137
|
-
.
|
|
137
|
+
.finish_non_exhaustive()
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
@@ -209,6 +209,7 @@ impl FactoryDependencyBuilder {
|
|
|
209
209
|
/// })
|
|
210
210
|
/// .build();
|
|
211
211
|
/// ```
|
|
212
|
+
#[must_use]
|
|
212
213
|
pub fn factory<F>(mut self, factory: F) -> Self
|
|
213
214
|
where
|
|
214
215
|
F: Fn(
|
|
@@ -245,6 +246,7 @@ impl FactoryDependencyBuilder {
|
|
|
245
246
|
/// })
|
|
246
247
|
/// .build();
|
|
247
248
|
/// ```
|
|
249
|
+
#[must_use]
|
|
248
250
|
pub fn depends_on(mut self, dependencies: Vec<String>) -> Self {
|
|
249
251
|
self.dependencies = dependencies;
|
|
250
252
|
self
|
|
@@ -272,7 +274,8 @@ impl FactoryDependencyBuilder {
|
|
|
272
274
|
/// .cacheable(true) // Same ID for all uses in one request
|
|
273
275
|
/// .build();
|
|
274
276
|
/// ```
|
|
275
|
-
|
|
277
|
+
#[must_use]
|
|
278
|
+
pub const fn cacheable(mut self, cacheable: bool) -> Self {
|
|
276
279
|
self.cacheable = cacheable;
|
|
277
280
|
self
|
|
278
281
|
}
|
|
@@ -300,7 +303,8 @@ impl FactoryDependencyBuilder {
|
|
|
300
303
|
/// .singleton(true) // Share across all requests
|
|
301
304
|
/// .build();
|
|
302
305
|
/// ```
|
|
303
|
-
|
|
306
|
+
#[must_use]
|
|
307
|
+
pub const fn singleton(mut self, singleton: bool) -> Self {
|
|
304
308
|
self.singleton = singleton;
|
|
305
309
|
self
|
|
306
310
|
}
|
|
@@ -133,6 +133,7 @@ impl DependencyGraph {
|
|
|
133
133
|
/// // Adding c -> [] would not
|
|
134
134
|
/// assert!(!graph.has_cycle_with("c", &[]));
|
|
135
135
|
/// ```
|
|
136
|
+
#[must_use]
|
|
136
137
|
pub fn has_cycle_with(&self, new_key: &str, new_deps: &[String]) -> bool {
|
|
137
138
|
let mut temp_graph = self.graph.clone();
|
|
138
139
|
temp_graph.insert(new_key.to_string(), new_deps.to_vec());
|
|
@@ -88,6 +88,9 @@ impl ResolvedDependencies {
|
|
|
88
88
|
/// let config = Arc::new("production".to_string());
|
|
89
89
|
/// resolved.insert("config".to_string(), config);
|
|
90
90
|
/// ```
|
|
91
|
+
///
|
|
92
|
+
/// # Panics
|
|
93
|
+
/// Panics if the lock is poisoned.
|
|
91
94
|
pub fn insert(&mut self, key: String, value: Arc<dyn Any + Send + Sync>) {
|
|
92
95
|
self.dependencies.lock().unwrap().insert(key, value);
|
|
93
96
|
}
|
|
@@ -126,6 +129,10 @@ impl ResolvedDependencies {
|
|
|
126
129
|
/// let missing: Option<Arc<i32>> = resolved.get("missing");
|
|
127
130
|
/// assert!(missing.is_none());
|
|
128
131
|
/// ```
|
|
132
|
+
///
|
|
133
|
+
/// # Panics
|
|
134
|
+
/// Panics if the lock is poisoned.
|
|
135
|
+
#[must_use]
|
|
129
136
|
pub fn get<T: Send + Sync + 'static>(&self, key: &str) -> Option<Arc<T>> {
|
|
130
137
|
self.dependencies
|
|
131
138
|
.lock()
|
|
@@ -155,6 +162,10 @@ impl ResolvedDependencies {
|
|
|
155
162
|
/// let any_ref = resolved.get_arc("data");
|
|
156
163
|
/// assert!(any_ref.is_some());
|
|
157
164
|
/// ```
|
|
165
|
+
///
|
|
166
|
+
/// # Panics
|
|
167
|
+
/// Panics if the lock is poisoned.
|
|
168
|
+
#[must_use]
|
|
158
169
|
pub fn get_arc(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>> {
|
|
159
170
|
self.dependencies.lock().unwrap().get(key).cloned()
|
|
160
171
|
}
|
|
@@ -177,6 +188,9 @@ impl ResolvedDependencies {
|
|
|
177
188
|
/// assert!(resolved.contains("exists"));
|
|
178
189
|
/// assert!(!resolved.contains("missing"));
|
|
179
190
|
/// ```
|
|
191
|
+
///
|
|
192
|
+
/// # Panics
|
|
193
|
+
/// Panics if the lock is poisoned.
|
|
180
194
|
#[must_use]
|
|
181
195
|
pub fn contains(&self, key: &str) -> bool {
|
|
182
196
|
self.dependencies.lock().unwrap().contains_key(key)
|
|
@@ -202,6 +216,9 @@ impl ResolvedDependencies {
|
|
|
202
216
|
/// assert!(keys.contains(&"config".to_string()));
|
|
203
217
|
/// assert!(keys.contains(&"db".to_string()));
|
|
204
218
|
/// ```
|
|
219
|
+
///
|
|
220
|
+
/// # Panics
|
|
221
|
+
/// Panics if the lock is poisoned.
|
|
205
222
|
#[must_use]
|
|
206
223
|
pub fn keys(&self) -> Vec<String> {
|
|
207
224
|
self.dependencies.lock().unwrap().keys().cloned().collect()
|
|
@@ -233,6 +250,9 @@ impl ResolvedDependencies {
|
|
|
233
250
|
/// resolved.cleanup().await;
|
|
234
251
|
/// # });
|
|
235
252
|
/// ```
|
|
253
|
+
///
|
|
254
|
+
/// # Panics
|
|
255
|
+
/// Panics if the lock is poisoned.
|
|
236
256
|
pub fn add_cleanup_task(&self, task: CleanupTask) {
|
|
237
257
|
self.cleanup_tasks.lock().unwrap().push(task);
|
|
238
258
|
}
|
|
@@ -275,6 +295,9 @@ impl ResolvedDependencies {
|
|
|
275
295
|
/// assert_eq!(*order.lock().unwrap(), vec![2, 1]);
|
|
276
296
|
/// # });
|
|
277
297
|
/// ```
|
|
298
|
+
///
|
|
299
|
+
/// # Panics
|
|
300
|
+
/// Panics if the lock is poisoned.
|
|
278
301
|
pub async fn cleanup(self) {
|
|
279
302
|
let tasks = {
|
|
280
303
|
let mut cleanup_tasks = self.cleanup_tasks.lock().unwrap();
|
|
@@ -134,6 +134,7 @@ impl<T: Clone + Send + Sync + 'static> std::fmt::Debug for ValueDependency<T> {
|
|
|
134
134
|
f.debug_struct("ValueDependency")
|
|
135
135
|
.field("key", &self.key)
|
|
136
136
|
.field("value_type", &std::any::type_name::<T>())
|
|
137
|
+
.field("value", &"<T>")
|
|
137
138
|
.finish()
|
|
138
139
|
}
|
|
139
140
|
}
|
|
@@ -31,6 +31,9 @@ impl StructuredError {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/// Catch panics and convert to a structured error so they don't cross FFI boundaries.
|
|
34
|
+
///
|
|
35
|
+
/// # Errors
|
|
36
|
+
/// Returns a structured error if a panic occurs during function execution.
|
|
34
37
|
pub fn shield_panic<T, F>(f: F) -> Result<T, StructuredError>
|
|
35
38
|
where
|
|
36
39
|
F: FnOnce() -> T + UnwindSafe,
|
|
@@ -15,16 +15,17 @@ pub enum Method {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
impl Method {
|
|
18
|
-
|
|
18
|
+
#[must_use]
|
|
19
|
+
pub const fn as_str(&self) -> &'static str {
|
|
19
20
|
match self {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
Self::Get => "GET",
|
|
22
|
+
Self::Post => "POST",
|
|
23
|
+
Self::Put => "PUT",
|
|
24
|
+
Self::Patch => "PATCH",
|
|
25
|
+
Self::Delete => "DELETE",
|
|
26
|
+
Self::Head => "HEAD",
|
|
27
|
+
Self::Options => "OPTIONS",
|
|
28
|
+
Self::Trace => "TRACE",
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
}
|
|
@@ -40,15 +41,15 @@ impl std::str::FromStr for Method {
|
|
|
40
41
|
|
|
41
42
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
42
43
|
match s.to_uppercase().as_str() {
|
|
43
|
-
"GET" => Ok(
|
|
44
|
-
"POST" => Ok(
|
|
45
|
-
"PUT" => Ok(
|
|
46
|
-
"PATCH" => Ok(
|
|
47
|
-
"DELETE" => Ok(
|
|
48
|
-
"HEAD" => Ok(
|
|
49
|
-
"OPTIONS" => Ok(
|
|
50
|
-
"TRACE" => Ok(
|
|
51
|
-
_ => Err(format!("Unknown HTTP method: {}"
|
|
44
|
+
"GET" => Ok(Self::Get),
|
|
45
|
+
"POST" => Ok(Self::Post),
|
|
46
|
+
"PUT" => Ok(Self::Put),
|
|
47
|
+
"PATCH" => Ok(Self::Patch),
|
|
48
|
+
"DELETE" => Ok(Self::Delete),
|
|
49
|
+
"HEAD" => Ok(Self::Head),
|
|
50
|
+
"OPTIONS" => Ok(Self::Options),
|
|
51
|
+
"TRACE" => Ok(Self::Trace),
|
|
52
|
+
_ => Err(format!("Unknown HTTP method: {s}")),
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
}
|
|
@@ -29,10 +29,10 @@ pub trait NativeLifecycleHook<Req, Resp>: Send + Sync {
|
|
|
29
29
|
fn name(&self) -> &str;
|
|
30
30
|
|
|
31
31
|
/// Execute hook with a request
|
|
32
|
-
fn execute_request<'a>(&
|
|
32
|
+
fn execute_request<'a>(&self, req: Req) -> RequestHookFutureSend<'a, Req, Resp>;
|
|
33
33
|
|
|
34
34
|
/// Execute hook with a response
|
|
35
|
-
fn execute_response<'a>(&
|
|
35
|
+
fn execute_response<'a>(&self, resp: Resp) -> ResponseHookFutureSend<'a, Resp>;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/// Trait for lifecycle hooks on local (wasm) targets (no Send requirements).
|
|
@@ -41,10 +41,10 @@ pub trait LocalLifecycleHook<Req, Resp> {
|
|
|
41
41
|
fn name(&self) -> &str;
|
|
42
42
|
|
|
43
43
|
/// Execute hook with a request
|
|
44
|
-
fn execute_request<'a>(&
|
|
44
|
+
fn execute_request<'a>(&self, req: Req) -> RequestHookFutureLocal<'a, Req, Resp>;
|
|
45
45
|
|
|
46
46
|
/// Execute hook with a response
|
|
47
|
-
fn execute_response<'a>(&
|
|
47
|
+
fn execute_response<'a>(&self, resp: Resp) -> ResponseHookFutureLocal<'a, Resp>;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
#[cfg(target_arch = "wasm32")]
|
|
@@ -97,17 +97,19 @@ impl<Req, Resp> std::fmt::Debug for LifecycleHooks<Req, Resp> {
|
|
|
97
97
|
|
|
98
98
|
impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
99
99
|
/// Create a new empty hooks container
|
|
100
|
+
#[must_use]
|
|
100
101
|
pub fn new() -> Self {
|
|
101
102
|
Self::default()
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
/// Builder constructor for ergonomic hook registration
|
|
106
|
+
#[must_use]
|
|
105
107
|
pub fn builder() -> LifecycleHooksBuilder<Req, Resp> {
|
|
106
108
|
LifecycleHooksBuilder::new()
|
|
107
109
|
}
|
|
108
110
|
|
|
109
111
|
/// Check if any hooks are registered
|
|
110
|
-
#[
|
|
112
|
+
#[must_use]
|
|
111
113
|
pub fn is_empty(&self) -> bool {
|
|
112
114
|
self.on_request.is_empty()
|
|
113
115
|
&& self.pre_validation.is_empty()
|
|
@@ -136,6 +138,8 @@ impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
|
136
138
|
self.on_error.push(hook);
|
|
137
139
|
}
|
|
138
140
|
|
|
141
|
+
/// # Errors
|
|
142
|
+
/// Returns an error string if a hook execution fails.
|
|
139
143
|
pub async fn execute_on_request(&self, mut req: Req) -> Result<HookResult<Req, Resp>, String> {
|
|
140
144
|
if self.on_request.is_empty() {
|
|
141
145
|
return Ok(HookResult::Continue(req));
|
|
@@ -151,6 +155,8 @@ impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
|
151
155
|
Ok(HookResult::Continue(req))
|
|
152
156
|
}
|
|
153
157
|
|
|
158
|
+
/// # Errors
|
|
159
|
+
/// Returns an error string if a hook execution fails.
|
|
154
160
|
pub async fn execute_pre_validation(&self, mut req: Req) -> Result<HookResult<Req, Resp>, String> {
|
|
155
161
|
if self.pre_validation.is_empty() {
|
|
156
162
|
return Ok(HookResult::Continue(req));
|
|
@@ -166,6 +172,8 @@ impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
|
166
172
|
Ok(HookResult::Continue(req))
|
|
167
173
|
}
|
|
168
174
|
|
|
175
|
+
/// # Errors
|
|
176
|
+
/// Returns an error string if a hook execution fails.
|
|
169
177
|
pub async fn execute_pre_handler(&self, mut req: Req) -> Result<HookResult<Req, Resp>, String> {
|
|
170
178
|
if self.pre_handler.is_empty() {
|
|
171
179
|
return Ok(HookResult::Continue(req));
|
|
@@ -181,6 +189,8 @@ impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
|
181
189
|
Ok(HookResult::Continue(req))
|
|
182
190
|
}
|
|
183
191
|
|
|
192
|
+
/// # Errors
|
|
193
|
+
/// Returns an error string if a hook execution fails.
|
|
184
194
|
pub async fn execute_on_response(&self, mut resp: Resp) -> Result<Resp, String> {
|
|
185
195
|
if self.on_response.is_empty() {
|
|
186
196
|
return Ok(resp);
|
|
@@ -188,14 +198,15 @@ impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
|
188
198
|
|
|
189
199
|
for hook in &self.on_response {
|
|
190
200
|
match hook.execute_response(resp).await? {
|
|
191
|
-
HookResult::Continue(r) => resp = r,
|
|
192
|
-
HookResult::ShortCircuit(r) => resp = r,
|
|
201
|
+
HookResult::Continue(r) | HookResult::ShortCircuit(r) => resp = r,
|
|
193
202
|
}
|
|
194
203
|
}
|
|
195
204
|
|
|
196
205
|
Ok(resp)
|
|
197
206
|
}
|
|
198
207
|
|
|
208
|
+
/// # Errors
|
|
209
|
+
/// Returns an error string if a hook execution fails.
|
|
199
210
|
pub async fn execute_on_error(&self, mut resp: Resp) -> Result<Resp, String> {
|
|
200
211
|
if self.on_error.is_empty() {
|
|
201
212
|
return Ok(resp);
|
|
@@ -203,8 +214,7 @@ impl<Req, Resp> LifecycleHooks<Req, Resp> {
|
|
|
203
214
|
|
|
204
215
|
for hook in &self.on_error {
|
|
205
216
|
match hook.execute_response(resp).await? {
|
|
206
|
-
HookResult::Continue(r) => resp = r,
|
|
207
|
-
HookResult::ShortCircuit(r) => resp = r,
|
|
217
|
+
HookResult::Continue(r) | HookResult::ShortCircuit(r) => resp = r,
|
|
208
218
|
}
|
|
209
219
|
}
|
|
210
220
|
|
|
@@ -237,11 +247,11 @@ where
|
|
|
237
247
|
&self.name
|
|
238
248
|
}
|
|
239
249
|
|
|
240
|
-
fn execute_request<'a>(&
|
|
250
|
+
fn execute_request<'a>(&self, req: Req) -> RequestHookFutureSend<'a, Req, Resp> {
|
|
241
251
|
Box::pin((self.func)(req))
|
|
242
252
|
}
|
|
243
253
|
|
|
244
|
-
fn execute_response<'a>(&
|
|
254
|
+
fn execute_response<'a>(&self, _resp: Resp) -> ResponseHookFutureSend<'a, Resp> {
|
|
245
255
|
Box::pin(async move { Err("Request hook called with response - this is a bug".to_string()) })
|
|
246
256
|
}
|
|
247
257
|
}
|
|
@@ -258,11 +268,11 @@ where
|
|
|
258
268
|
&self.name
|
|
259
269
|
}
|
|
260
270
|
|
|
261
|
-
fn execute_request<'a>(&
|
|
271
|
+
fn execute_request<'a>(&self, req: Req) -> RequestHookFutureLocal<'a, Req, Resp> {
|
|
262
272
|
Box::pin((self.func)(req))
|
|
263
273
|
}
|
|
264
274
|
|
|
265
|
-
fn execute_response<'a>(&
|
|
275
|
+
fn execute_response<'a>(&self, _resp: Resp) -> ResponseHookFutureLocal<'a, Resp> {
|
|
266
276
|
Box::pin(async move { Err("Request hook called with response - this is a bug".to_string()) })
|
|
267
277
|
}
|
|
268
278
|
}
|
|
@@ -279,11 +289,11 @@ where
|
|
|
279
289
|
&self.name
|
|
280
290
|
}
|
|
281
291
|
|
|
282
|
-
fn execute_request<'a>(&
|
|
292
|
+
fn execute_request<'a>(&self, _req: Req) -> RequestHookFutureSend<'a, Req, Resp> {
|
|
283
293
|
Box::pin(async move { Err("Response hook called with request - this is a bug".to_string()) })
|
|
284
294
|
}
|
|
285
295
|
|
|
286
|
-
fn execute_response<'a>(&
|
|
296
|
+
fn execute_response<'a>(&self, resp: Resp) -> ResponseHookFutureSend<'a, Resp> {
|
|
287
297
|
Box::pin((self.func)(resp))
|
|
288
298
|
}
|
|
289
299
|
}
|
|
@@ -300,52 +310,66 @@ where
|
|
|
300
310
|
&self.name
|
|
301
311
|
}
|
|
302
312
|
|
|
303
|
-
fn execute_request<'a>(&
|
|
313
|
+
fn execute_request<'a>(&self, _req: Req) -> RequestHookFutureLocal<'a, Req, Resp> {
|
|
304
314
|
Box::pin(async move { Err("Response hook called with request - this is a bug".to_string()) })
|
|
305
315
|
}
|
|
306
316
|
|
|
307
|
-
fn execute_response<'a>(&
|
|
317
|
+
fn execute_response<'a>(&self, resp: Resp) -> ResponseHookFutureLocal<'a, Resp> {
|
|
308
318
|
Box::pin((self.func)(resp))
|
|
309
319
|
}
|
|
310
320
|
}
|
|
311
321
|
|
|
312
|
-
/// Builder
|
|
322
|
+
/// Builder pattern for `LifecycleHooks`
|
|
313
323
|
pub struct LifecycleHooksBuilder<Req, Resp> {
|
|
314
324
|
hooks: LifecycleHooks<Req, Resp>,
|
|
315
325
|
}
|
|
316
326
|
|
|
317
327
|
impl<Req, Resp> LifecycleHooksBuilder<Req, Resp> {
|
|
328
|
+
/// Create a new builder
|
|
329
|
+
#[must_use]
|
|
318
330
|
pub fn new() -> Self {
|
|
319
331
|
Self {
|
|
320
332
|
hooks: LifecycleHooks::default(),
|
|
321
333
|
}
|
|
322
334
|
}
|
|
323
335
|
|
|
336
|
+
/// Add an `on_request` hook
|
|
337
|
+
#[must_use]
|
|
324
338
|
pub fn on_request(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
|
|
325
339
|
self.hooks.add_on_request(hook);
|
|
326
340
|
self
|
|
327
341
|
}
|
|
328
342
|
|
|
343
|
+
/// Add a `pre_validation` hook
|
|
344
|
+
#[must_use]
|
|
329
345
|
pub fn pre_validation(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
|
|
330
346
|
self.hooks.add_pre_validation(hook);
|
|
331
347
|
self
|
|
332
348
|
}
|
|
333
349
|
|
|
350
|
+
/// Add a `pre_handler` hook
|
|
351
|
+
#[must_use]
|
|
334
352
|
pub fn pre_handler(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
|
|
335
353
|
self.hooks.add_pre_handler(hook);
|
|
336
354
|
self
|
|
337
355
|
}
|
|
338
356
|
|
|
357
|
+
/// Add an `on_response` hook
|
|
358
|
+
#[must_use]
|
|
339
359
|
pub fn on_response(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
|
|
340
360
|
self.hooks.add_on_response(hook);
|
|
341
361
|
self
|
|
342
362
|
}
|
|
343
363
|
|
|
364
|
+
/// Add an `on_error` hook
|
|
365
|
+
#[must_use]
|
|
344
366
|
pub fn on_error(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
|
|
345
367
|
self.hooks.add_on_error(hook);
|
|
346
368
|
self
|
|
347
369
|
}
|
|
348
370
|
|
|
371
|
+
/// Build the `LifecycleHooks` instance
|
|
372
|
+
#[must_use]
|
|
349
373
|
pub fn build(self) -> LifecycleHooks<Req, Resp> {
|
|
350
374
|
self.hooks
|
|
351
375
|
}
|
|
@@ -181,11 +181,7 @@ pub fn parse_parameter_schema(schema: &Value) -> Result<Vec<ParameterMetadata>,
|
|
|
181
181
|
let required: Vec<String> = schema
|
|
182
182
|
.get("required")
|
|
183
183
|
.and_then(|r| r.as_array())
|
|
184
|
-
.map(|arr|
|
|
185
|
-
arr.iter()
|
|
186
|
-
.filter_map(|v| v.as_str().map(String::from))
|
|
187
|
-
.collect()
|
|
188
|
-
})
|
|
184
|
+
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
|
|
189
185
|
.unwrap_or_default();
|
|
190
186
|
|
|
191
187
|
for (param_name, param_schema) in props {
|
|
@@ -197,10 +193,7 @@ pub fn parse_parameter_schema(schema: &Value) -> Result<Vec<ParameterMetadata>,
|
|
|
197
193
|
.and_then(|s| s.parse().ok())
|
|
198
194
|
.unwrap_or(ParameterSource::Query);
|
|
199
195
|
|
|
200
|
-
let schema_type = param_schema
|
|
201
|
-
.get("type")
|
|
202
|
-
.and_then(|t| t.as_str())
|
|
203
|
-
.map(String::from);
|
|
196
|
+
let schema_type = param_schema.get("type").and_then(|t| t.as_str()).map(String::from);
|
|
204
197
|
|
|
205
198
|
params.push(ParameterMetadata {
|
|
206
199
|
name: param_name.clone(),
|
|
@@ -242,11 +235,7 @@ pub fn validate_metadata(metadata: &ExtractedRouteMetadata) -> Result<(), Vec<St
|
|
|
242
235
|
}
|
|
243
236
|
}
|
|
244
237
|
|
|
245
|
-
if errors.is_empty() {
|
|
246
|
-
Ok(())
|
|
247
|
-
} else {
|
|
248
|
-
Err(errors)
|
|
249
|
-
}
|
|
238
|
+
if errors.is_empty() { Ok(()) } else { Err(errors) }
|
|
250
239
|
}
|
|
251
240
|
|
|
252
241
|
/// Merge path parameters with parameter schema
|