1 + | // Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
|
2 + | /// The service builder for [`SimpleService`].
|
3 + | ///
|
4 + | /// Constructed via [`SimpleService::builder`].
|
5 + | pub struct SimpleServiceBuilder<Body, L, HttpPl, ModelPl> {
|
6 + | operation: Option<::aws_smithy_legacy_http_server::routing::Route<Body>>,
|
7 + | layer: L,
|
8 + | http_plugin: HttpPl,
|
9 + | model_plugin: ModelPl,
|
10 + | }
|
11 + |
|
12 + | impl<Body, L, HttpPl, ModelPl> SimpleServiceBuilder<Body, L, HttpPl, ModelPl> {
|
13 + | /// Sets the [`Operation`](crate::operation_shape::Operation) operation.
|
14 + | ///
|
15 + | /// This should be an async function satisfying the [`Handler`](::aws_smithy_legacy_http_server::operation::Handler) trait.
|
16 + | /// See the [operation module documentation](::aws_smithy_legacy_http_server::operation) for more information.
|
17 + | ///
|
18 + | /// # Example
|
19 + | ///
|
20 + | /// ```no_run
|
21 + | /// use simple_http0x::{SimpleService, SimpleServiceConfig};
|
22 + | ///
|
23 + | /// use simple_http0x::{input, output};
|
24 + | ///
|
25 + | /// async fn handler(input: input::OperationInput) -> output::OperationOutput {
|
26 + | /// todo!()
|
27 + | /// }
|
28 + | ///
|
29 + | /// let config = SimpleServiceConfig::builder().build();
|
30 + | /// let app = SimpleService::builder(config)
|
31 + | /// .operation(handler)
|
32 + | /// /* Set other handlers */
|
33 + | /// .build()
|
34 + | /// .unwrap();
|
35 + | /// # let app: SimpleService<::aws_smithy_legacy_http_server::routing::RoutingService<::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<::aws_smithy_legacy_http_server::routing::Route>, ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1>> = app;
|
36 + | /// ```
|
37 + | ///
|
38 + | pub fn operation<HandlerType, HandlerExtractors, UpgradeExtractors>(self, handler: HandlerType) -> Self
|
39 + | where
|
40 + | HandlerType: ::aws_smithy_legacy_http_server::operation::Handler<crate::operation_shape::Operation, HandlerExtractors>,
|
41 + |
|
42 + | ModelPl: ::aws_smithy_legacy_http_server::plugin::Plugin<
|
43 + | SimpleService<L>,
|
44 + | crate::operation_shape::Operation,
|
45 + | ::aws_smithy_legacy_http_server::operation::IntoService<crate::operation_shape::Operation, HandlerType>
|
46 + | >,
|
47 + | ::aws_smithy_legacy_http_server::operation::UpgradePlugin::<UpgradeExtractors>: ::aws_smithy_legacy_http_server::plugin::Plugin<
|
48 + | SimpleService<L>,
|
49 + | crate::operation_shape::Operation,
|
50 + | ModelPl::Output
|
51 + | >,
|
52 + | HttpPl: ::aws_smithy_legacy_http_server::plugin::Plugin<
|
53 + | SimpleService<L>,
|
54 + | crate::operation_shape::Operation,
|
55 + | <
|
56 + | ::aws_smithy_legacy_http_server::operation::UpgradePlugin::<UpgradeExtractors>
|
57 + | as ::aws_smithy_legacy_http_server::plugin::Plugin<
|
58 + | SimpleService<L>,
|
59 + | crate::operation_shape::Operation,
|
60 + | ModelPl::Output
|
61 + | >
|
62 + | >::Output
|
63 + | >,
|
64 + |
|
65 + | HttpPl::Output: ::tower::Service<::http::Request<Body>, Response = ::http::Response<::aws_smithy_legacy_http_server::body::BoxBody>, Error = ::std::convert::Infallible> + Clone + Send + 'static,
|
66 + | <HttpPl::Output as ::tower::Service<::http::Request<Body>>>::Future: Send + 'static,
|
67 + |
|
68 + | {
|
69 + | use ::aws_smithy_legacy_http_server::operation::OperationShapeExt;
|
70 + | use ::aws_smithy_legacy_http_server::plugin::Plugin;
|
71 + | let svc = crate::operation_shape::Operation::from_handler(handler);
|
72 + | let svc = self.model_plugin.apply(svc);
|
73 + | let svc =
|
74 + | ::aws_smithy_legacy_http_server::operation::UpgradePlugin::<UpgradeExtractors>::new()
|
75 + | .apply(svc);
|
76 + | let svc = self.http_plugin.apply(svc);
|
77 + | self.operation_custom(svc)
|
78 + | }
|
79 + |
|
80 + | /// Sets the [`Operation`](crate::operation_shape::Operation) operation.
|
81 + | ///
|
82 + | /// This should be an async function satisfying the [`Handler`](::aws_smithy_legacy_http_server::operation::Handler) trait.
|
83 + | /// See the [operation module documentation](::aws_smithy_legacy_http_server::operation) for more information.
|
84 + | ///
|
85 + | /// # Example
|
86 + | ///
|
87 + | /// ```no_run
|
88 + | /// use simple_http0x::{SimpleService, SimpleServiceConfig};
|
89 + | ///
|
90 + | /// use simple_http0x::{input, output};
|
91 + | ///
|
92 + | /// async fn handler(input: input::OperationInput) -> Result<output::OperationOutput, std::convert::Infallible> {
|
93 + | /// todo!()
|
94 + | /// }
|
95 + | ///
|
96 + | /// let config = SimpleServiceConfig::builder().build();
|
97 + | /// let svc = ::tower::util::service_fn(handler);
|
98 + | /// let app = SimpleService::builder(config)
|
99 + | /// .operation_service(svc)
|
100 + | /// /* Set other handlers */
|
101 + | /// .build()
|
102 + | /// .unwrap();
|
103 + | /// # let app: SimpleService<::aws_smithy_legacy_http_server::routing::RoutingService<::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<::aws_smithy_legacy_http_server::routing::Route>, ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1>> = app;
|
104 + | /// ```
|
105 + | ///
|
106 + | pub fn operation_service<S, ServiceExtractors, UpgradeExtractors>(self, service: S) -> Self
|
107 + | where
|
108 + | S: ::aws_smithy_legacy_http_server::operation::OperationService<crate::operation_shape::Operation, ServiceExtractors>,
|
109 + |
|
110 + | ModelPl: ::aws_smithy_legacy_http_server::plugin::Plugin<
|
111 + | SimpleService<L>,
|
112 + | crate::operation_shape::Operation,
|
113 + | ::aws_smithy_legacy_http_server::operation::Normalize<crate::operation_shape::Operation, S>
|
114 + | >,
|
115 + | ::aws_smithy_legacy_http_server::operation::UpgradePlugin::<UpgradeExtractors>: ::aws_smithy_legacy_http_server::plugin::Plugin<
|
116 + | SimpleService<L>,
|
117 + | crate::operation_shape::Operation,
|
118 + | ModelPl::Output
|
119 + | >,
|
120 + | HttpPl: ::aws_smithy_legacy_http_server::plugin::Plugin<
|
121 + | SimpleService<L>,
|
122 + | crate::operation_shape::Operation,
|
123 + | <
|
124 + | ::aws_smithy_legacy_http_server::operation::UpgradePlugin::<UpgradeExtractors>
|
125 + | as ::aws_smithy_legacy_http_server::plugin::Plugin<
|
126 + | SimpleService<L>,
|
127 + | crate::operation_shape::Operation,
|
128 + | ModelPl::Output
|
129 + | >
|
130 + | >::Output
|
131 + | >,
|
132 + |
|
133 + | HttpPl::Output: ::tower::Service<::http::Request<Body>, Response = ::http::Response<::aws_smithy_legacy_http_server::body::BoxBody>, Error = ::std::convert::Infallible> + Clone + Send + 'static,
|
134 + | <HttpPl::Output as ::tower::Service<::http::Request<Body>>>::Future: Send + 'static,
|
135 + |
|
136 + | {
|
137 + | use ::aws_smithy_legacy_http_server::operation::OperationShapeExt;
|
138 + | use ::aws_smithy_legacy_http_server::plugin::Plugin;
|
139 + | let svc = crate::operation_shape::Operation::from_service(service);
|
140 + | let svc = self.model_plugin.apply(svc);
|
141 + | let svc =
|
142 + | ::aws_smithy_legacy_http_server::operation::UpgradePlugin::<UpgradeExtractors>::new()
|
143 + | .apply(svc);
|
144 + | let svc = self.http_plugin.apply(svc);
|
145 + | self.operation_custom(svc)
|
146 + | }
|
147 + |
|
148 + | /// Sets the [`Operation`](crate::operation_shape::Operation) to a custom [`Service`](tower::Service).
|
149 + | /// not constrained by the Smithy contract.
|
150 + | fn operation_custom<S>(mut self, svc: S) -> Self
|
151 + | where
|
152 + | S: ::tower::Service<
|
153 + | ::http::Request<Body>,
|
154 + | Response = ::http::Response<::aws_smithy_legacy_http_server::body::BoxBody>,
|
155 + | Error = ::std::convert::Infallible,
|
156 + | > + Clone
|
157 + | + Send
|
158 + | + 'static,
|
159 + | S::Future: Send + 'static,
|
160 + | {
|
161 + | self.operation = Some(::aws_smithy_legacy_http_server::routing::Route::new(svc));
|
162 + | self
|
163 + | }
|
164 + | }
|
165 + |
|
166 + | impl<Body, L, HttpPl, ModelPl> SimpleServiceBuilder<Body, L, HttpPl, ModelPl> {
|
167 + | /// Constructs a [`SimpleService`] from the arguments provided to the builder.
|
168 + | ///
|
169 + | /// Forgetting to register a handler for one or more operations will result in an error.
|
170 + | ///
|
171 + | /// Check out [`SimpleServiceBuilder::build_unchecked`] if you'd prefer the service to return status code 500 when an
|
172 + | /// unspecified route is requested.
|
173 + | pub fn build(
|
174 + | self,
|
175 + | ) -> ::std::result::Result<
|
176 + | SimpleService<
|
177 + | ::aws_smithy_legacy_http_server::routing::RoutingService<
|
178 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<L::Service>,
|
179 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
180 + | >,
|
181 + | >,
|
182 + | MissingOperationsError,
|
183 + | >
|
184 + | where
|
185 + | L: ::tower::Layer<::aws_smithy_legacy_http_server::routing::Route<Body>>,
|
186 + | {
|
187 + | let router = {
|
188 + | use ::aws_smithy_legacy_http_server::operation::OperationShape;
|
189 + | let mut missing_operation_names = std::collections::HashMap::new();
|
190 + | if self.operation.is_none() {
|
191 + | missing_operation_names
|
192 + | .insert(crate::operation_shape::Operation::ID, ".operation()");
|
193 + | }
|
194 + | if !missing_operation_names.is_empty() {
|
195 + | return Err(MissingOperationsError {
|
196 + | operation_names2setter_methods: missing_operation_names,
|
197 + | });
|
198 + | }
|
199 + | let unexpected_error_msg = "this should never panic since we are supposed to check beforehand that a handler has been registered for this operation; please file a bug report under https://github.com/smithy-lang/smithy-rs/issues";
|
200 + |
|
201 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter::from_iter([(
|
202 + | request_specs::operation(),
|
203 + | self.operation.expect(unexpected_error_msg),
|
204 + | )])
|
205 + | };
|
206 + | let svc = ::aws_smithy_legacy_http_server::routing::RoutingService::new(router);
|
207 + | let svc = svc.map(|s| s.layer(self.layer));
|
208 + | Ok(SimpleService { svc })
|
209 + | }
|
210 + |
|
211 + | /// Constructs a [`SimpleService`] from the arguments provided to the builder.
|
212 + | /// Operations without a handler default to returning 500 Internal Server Error to the caller.
|
213 + | ///
|
214 + | /// Check out [`SimpleServiceBuilder::build`] if you'd prefer the builder to fail if one or more operations do
|
215 + | /// not have a registered handler.
|
216 + | pub fn build_unchecked(self) -> SimpleService<L::Service>
|
217 + | where
|
218 + | Body: Send + 'static,
|
219 + | L: ::tower::Layer<
|
220 + | ::aws_smithy_legacy_http_server::routing::RoutingService<
|
221 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<
|
222 + | ::aws_smithy_legacy_http_server::routing::Route<Body>,
|
223 + | >,
|
224 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
225 + | >,
|
226 + | >,
|
227 + | {
|
228 + | let router =
|
229 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter::from_iter([(
|
230 + | request_specs::operation(),
|
231 + | self.operation.unwrap_or_else(|| {
|
232 + | let svc = ::aws_smithy_legacy_http_server::operation::MissingFailure::<
|
233 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
234 + | >::default();
|
235 + | ::aws_smithy_legacy_http_server::routing::Route::new(svc)
|
236 + | }),
|
237 + | )]);
|
238 + | let svc = self
|
239 + | .layer
|
240 + | .layer(::aws_smithy_legacy_http_server::routing::RoutingService::new(router));
|
241 + | SimpleService { svc }
|
242 + | }
|
243 + | }
|
244 + |
|
245 + | /// The error encountered when calling the [`SimpleServiceBuilder::build`] method if one or more operation handlers are not
|
246 + | /// specified.
|
247 + | #[derive(Debug)]
|
248 + | pub struct MissingOperationsError {
|
249 + | operation_names2setter_methods:
|
250 + | std::collections::HashMap<::aws_smithy_legacy_http_server::shape_id::ShapeId, &'static str>,
|
251 + | }
|
252 + |
|
253 + | impl std::fmt::Display for MissingOperationsError {
|
254 + | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
255 + | write!(
|
256 + | f,
|
257 + | "You must specify a handler for all operations attached to `SimpleService`.\n\
|
258 + | We are missing handlers for the following operations:\n",
|
259 + | )?;
|
260 + | for operation_name in self.operation_names2setter_methods.keys() {
|
261 + | writeln!(f, "- {}", operation_name.absolute())?;
|
262 + | }
|
263 + |
|
264 + | writeln!(f, "\nUse the dedicated methods on `SimpleServiceBuilder` to register the missing handlers:")?;
|
265 + | for setter_name in self.operation_names2setter_methods.values() {
|
266 + | writeln!(f, "- {}", setter_name)?;
|
267 + | }
|
268 + | Ok(())
|
269 + | }
|
270 + | }
|
271 + |
|
272 + | impl std::error::Error for MissingOperationsError {}
|
273 + |
|
274 + | mod request_specs {
|
275 + | pub(super) fn operation() -> ::aws_smithy_legacy_http_server::routing::request_spec::RequestSpec
|
276 + | {
|
277 + | ::aws_smithy_legacy_http_server::routing::request_spec::RequestSpec::new(
|
278 + | ::http::Method::POST,
|
279 + | ::aws_smithy_legacy_http_server::routing::request_spec::UriSpec::new(
|
280 + | ::aws_smithy_legacy_http_server::routing::request_spec::PathAndQuerySpec::new(
|
281 + | ::aws_smithy_legacy_http_server::routing::request_spec::PathSpec::from_vector_unchecked(vec![
|
282 + | ::aws_smithy_legacy_http_server::routing::request_spec::PathSegment::Literal(String::from("operation")),
|
283 + | ]),
|
284 + | ::aws_smithy_legacy_http_server::routing::request_spec::QuerySpec::from_vector_unchecked(vec![
|
285 + | ])
|
286 + | )
|
287 + | ),
|
288 + | )
|
289 + | }
|
290 + | }
|
291 + |
|
292 + | #[allow(missing_docs)] // documentation missing in model
|
293 + | ///
|
294 + | /// See the [root](crate) documentation for more information.
|
295 + | #[derive(Clone)]
|
296 + | pub struct SimpleService<
|
297 + | S = ::aws_smithy_legacy_http_server::routing::RoutingService<
|
298 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<
|
299 + | ::aws_smithy_legacy_http_server::routing::Route<
|
300 + | ::aws_smithy_legacy_http_server::body::BoxBody,
|
301 + | >,
|
302 + | >,
|
303 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
304 + | >,
|
305 + | > {
|
306 + | // This is the router wrapped by layers.
|
307 + | svc: S,
|
308 + | }
|
309 + |
|
310 + | impl SimpleService<()> {
|
311 + | /// Constructs a builder for [`SimpleService`].
|
312 + | /// You must specify a configuration object holding any plugins and layers that should be applied
|
313 + | /// to the operations in this service.
|
314 + | pub fn builder<
|
315 + | Body,
|
316 + | L,
|
317 + | HttpPl: ::aws_smithy_legacy_http_server::plugin::HttpMarker,
|
318 + | ModelPl: ::aws_smithy_legacy_http_server::plugin::ModelMarker,
|
319 + | >(
|
320 + | config: SimpleServiceConfig<L, HttpPl, ModelPl>,
|
321 + | ) -> SimpleServiceBuilder<Body, L, HttpPl, ModelPl> {
|
322 + | SimpleServiceBuilder {
|
323 + | operation: None,
|
324 + | layer: config.layers,
|
325 + | http_plugin: config.http_plugins,
|
326 + | model_plugin: config.model_plugins,
|
327 + | }
|
328 + | }
|
329 + |
|
330 + | /// Constructs a builder for [`SimpleService`].
|
331 + | /// You must specify what plugins should be applied to the operations in this service.
|
332 + | ///
|
333 + | /// Use [`SimpleService::builder_without_plugins`] if you don't need to apply plugins.
|
334 + | ///
|
335 + | /// Check out [`HttpPlugins`](::aws_smithy_legacy_http_server::plugin::HttpPlugins) and
|
336 + | /// [`ModelPlugins`](::aws_smithy_legacy_http_server::plugin::ModelPlugins) if you need to apply
|
337 + | /// multiple plugins.
|
338 + | #[deprecated(
|
339 + | since = "0.57.0",
|
340 + | note = "please use the `builder` constructor and register plugins on the `SimpleServiceConfig` object instead; see https://github.com/smithy-lang/smithy-rs/discussions/3096"
|
341 + | )]
|
342 + | pub fn builder_with_plugins<
|
343 + | Body,
|
344 + | HttpPl: ::aws_smithy_legacy_http_server::plugin::HttpMarker,
|
345 + | ModelPl: ::aws_smithy_legacy_http_server::plugin::ModelMarker,
|
346 + | >(
|
347 + | http_plugin: HttpPl,
|
348 + | model_plugin: ModelPl,
|
349 + | ) -> SimpleServiceBuilder<Body, ::tower::layer::util::Identity, HttpPl, ModelPl> {
|
350 + | SimpleServiceBuilder {
|
351 + | operation: None,
|
352 + | layer: ::tower::layer::util::Identity::new(),
|
353 + | http_plugin,
|
354 + | model_plugin,
|
355 + | }
|
356 + | }
|
357 + |
|
358 + | /// Constructs a builder for [`SimpleService`].
|
359 + | ///
|
360 + | /// Use [`SimpleService::builder_with_plugins`] if you need to specify plugins.
|
361 + | #[deprecated(
|
362 + | since = "0.57.0",
|
363 + | note = "please use the `builder` constructor instead; see https://github.com/smithy-lang/smithy-rs/discussions/3096"
|
364 + | )]
|
365 + | pub fn builder_without_plugins<Body>() -> SimpleServiceBuilder<
|
366 + | Body,
|
367 + | ::tower::layer::util::Identity,
|
368 + | ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
369 + | ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
370 + | > {
|
371 + | Self::builder_with_plugins(
|
372 + | ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
373 + | ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
374 + | )
|
375 + | }
|
376 + | }
|
377 + |
|
378 + | impl<S> SimpleService<S> {
|
379 + | /// Converts [`SimpleService`] into a [`MakeService`](tower::make::MakeService).
|
380 + | pub fn into_make_service(
|
381 + | self,
|
382 + | ) -> ::aws_smithy_legacy_http_server::routing::IntoMakeService<Self> {
|
383 + | ::aws_smithy_legacy_http_server::routing::IntoMakeService::new(self)
|
384 + | }
|
385 + |
|
386 + | /// Converts [`SimpleService`] into a [`MakeService`](tower::make::MakeService) with [`ConnectInfo`](::aws_smithy_legacy_http_server::request::connect_info::ConnectInfo).
|
387 + | pub fn into_make_service_with_connect_info<C>(
|
388 + | self,
|
389 + | ) -> ::aws_smithy_legacy_http_server::routing::IntoMakeServiceWithConnectInfo<Self, C> {
|
390 + | ::aws_smithy_legacy_http_server::routing::IntoMakeServiceWithConnectInfo::new(self)
|
391 + | }
|
392 + | }
|
393 + |
|
394 + | impl<S>
|
395 + | SimpleService<
|
396 + | ::aws_smithy_legacy_http_server::routing::RoutingService<
|
397 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<S>,
|
398 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
399 + | >,
|
400 + | >
|
401 + | {
|
402 + | /// Applies a [`Layer`](::tower::Layer) uniformly to all routes.
|
403 + | #[deprecated(
|
404 + | since = "0.57.0",
|
405 + | note = "please add layers to the `SimpleServiceConfig` object instead; see https://github.com/smithy-lang/smithy-rs/discussions/3096"
|
406 + | )]
|
407 + | pub fn layer<L>(
|
408 + | self,
|
409 + | layer: &L,
|
410 + | ) -> SimpleService<
|
411 + | ::aws_smithy_legacy_http_server::routing::RoutingService<
|
412 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<L::Service>,
|
413 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
414 + | >,
|
415 + | >
|
416 + | where
|
417 + | L: ::tower::Layer<S>,
|
418 + | {
|
419 + | SimpleService {
|
420 + | svc: self.svc.map(|s| s.layer(layer)),
|
421 + | }
|
422 + | }
|
423 + |
|
424 + | /// Applies [`Route::new`](::aws_smithy_legacy_http_server::routing::Route::new) to all routes.
|
425 + | ///
|
426 + | /// This has the effect of erasing all types accumulated via layers.
|
427 + | pub fn boxed<B>(
|
428 + | self,
|
429 + | ) -> SimpleService<
|
430 + | ::aws_smithy_legacy_http_server::routing::RoutingService<
|
431 + | ::aws_smithy_legacy_http_server::protocol::rest::router::RestRouter<
|
432 + | ::aws_smithy_legacy_http_server::routing::Route<B>,
|
433 + | >,
|
434 + | ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1,
|
435 + | >,
|
436 + | >
|
437 + | where
|
438 + | S: ::tower::Service<
|
439 + | ::http::Request<B>,
|
440 + | Response = ::http::Response<::aws_smithy_legacy_http_server::body::BoxBody>,
|
441 + | Error = std::convert::Infallible,
|
442 + | >,
|
443 + | S: Clone + Send + 'static,
|
444 + | S::Future: Send + 'static,
|
445 + | {
|
446 + | self.layer(&::tower::layer::layer_fn(
|
447 + | ::aws_smithy_legacy_http_server::routing::Route::new,
|
448 + | ))
|
449 + | }
|
450 + | }
|
451 + |
|
452 + | impl<S, R> ::tower::Service<R> for SimpleService<S>
|
453 + | where
|
454 + | S: ::tower::Service<R>,
|
455 + | {
|
456 + | type Response = S::Response;
|
457 + | type Error = S::Error;
|
458 + | type Future = S::Future;
|
459 + |
|
460 + | fn poll_ready(
|
461 + | &mut self,
|
462 + | cx: &mut std::task::Context,
|
463 + | ) -> std::task::Poll<::std::result::Result<(), Self::Error>> {
|
464 + | self.svc.poll_ready(cx)
|
465 + | }
|
466 + |
|
467 + | fn call(&mut self, request: R) -> Self::Future {
|
468 + | self.svc.call(request)
|
469 + | }
|
470 + | }
|
471 + |
|
472 + | /// An enumeration of all [operations](https://smithy.io/2.0/spec/service-types.html#operation) in SimpleService.
|
473 + | #[allow(clippy::enum_variant_names)]
|
474 + | #[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
475 + | pub enum Operation {
|
476 + | Operation,
|
477 + | }
|
478 + |
|
479 + | impl Operation {
|
480 + | /// Returns the [operations](https://smithy.io/2.0/spec/service-types.html#operation) [`ShapeId`](::aws_smithy_legacy_http_server::shape_id::ShapeId).
|
481 + | pub fn shape_id(&self) -> ::aws_smithy_legacy_http_server::shape_id::ShapeId {
|
482 + | match self {
|
483 + | Operation::Operation => ::aws_smithy_legacy_http_server::shape_id::ShapeId::new(
|
484 + | "com.amazonaws.simple#Operation",
|
485 + | "com.amazonaws.simple",
|
486 + | "Operation",
|
487 + | ),
|
488 + | }
|
489 + | }
|
490 + | }
|
491 + | impl<L>
|
492 + | ::aws_smithy_legacy_http_server::service::ContainsOperation<crate::operation_shape::Operation>
|
493 + | for SimpleService<L>
|
494 + | {
|
495 + | const VALUE: Operation = Operation::Operation;
|
496 + | }
|
497 + |
|
498 + | impl<S> ::aws_smithy_legacy_http_server::service::ServiceShape for SimpleService<S> {
|
499 + | const ID: ::aws_smithy_legacy_http_server::shape_id::ShapeId =
|
500 + | ::aws_smithy_legacy_http_server::shape_id::ShapeId::new(
|
501 + | "com.amazonaws.simple#SimpleService",
|
502 + | "com.amazonaws.simple",
|
503 + | "SimpleService",
|
504 + | );
|
505 + |
|
506 + | const VERSION: Option<&'static str> = Some("");
|
507 + |
|
508 + | type Protocol = ::aws_smithy_legacy_http_server::protocol::rest_json_1::RestJson1;
|
509 + |
|
510 + | type Operations = Operation;
|
511 + | }
|
512 + | /// Configuration for the [`SimpleService`]. This is the central place where to register and
|
513 + | /// configure [`::tower::Layer`]s, HTTP plugins, and model plugins.
|
514 + | ///
|
515 + | /// ```rust,no_run
|
516 + | /// # use simple_http0x::SimpleServiceConfig;
|
517 + | /// # use ::aws_smithy_legacy_http_server::plugin::IdentityPlugin;
|
518 + | /// # use ::tower::layer::util::Identity;
|
519 + | /// # let authentication_plugin = IdentityPlugin;
|
520 + | /// # let authorization_plugin = IdentityPlugin;
|
521 + | /// # let server_request_id_provider_layer = Identity::new();
|
522 + | /// let config = SimpleServiceConfig::builder()
|
523 + | /// // Layers get executed first...
|
524 + | /// .layer(server_request_id_provider_layer)
|
525 + | /// // ...then HTTP plugins...
|
526 + | /// .http_plugin(authentication_plugin)
|
527 + | /// // ...and right after deserialization, model plugins.
|
528 + | /// .model_plugin(authorization_plugin)
|
529 + | /// .build();
|
530 + | /// ```
|
531 + | ///
|
532 + | /// See the [`plugin`] system for details.
|
533 + | ///
|
534 + | /// [`plugin`]: ::aws_smithy_legacy_http_server::plugin
|
535 + | #[derive(::std::fmt::Debug)]
|
536 + | pub struct SimpleServiceConfig<L, H, M> {
|
537 + | layers: L,
|
538 + | http_plugins: H,
|
539 + | model_plugins: M,
|
540 + | }
|
541 + |
|
542 + | impl SimpleServiceConfig<(), (), ()> {
|
543 + | /// Returns a builder to construct the configuration.
|
544 + | pub fn builder() -> SimpleServiceConfigBuilder<
|
545 + | ::tower::layer::util::Identity,
|
546 + | ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
547 + | ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
548 + | > {
|
549 + | SimpleServiceConfigBuilder {
|
550 + | layers: ::tower::layer::util::Identity::new(),
|
551 + | http_plugins: ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
552 + | model_plugins: ::aws_smithy_legacy_http_server::plugin::IdentityPlugin,
|
553 + | }
|
554 + | }
|
555 + | }
|
556 + |
|
557 + | /// Builder returned by [`SimpleServiceConfig::builder()`].
|
558 + | #[derive(::std::fmt::Debug)]
|
559 + | pub struct SimpleServiceConfigBuilder<L, H, M> {
|
560 + | pub(crate) layers: L,
|
561 + | pub(crate) http_plugins: H,
|
562 + | pub(crate) model_plugins: M,
|
563 + | }
|
564 + |
|
565 + | impl<L, H, M> SimpleServiceConfigBuilder<L, H, M> {
|
566 + | /// Add a [`::tower::Layer`] to the service.
|
567 + | pub fn layer<NewLayer>(
|
568 + | self,
|
569 + | layer: NewLayer,
|
570 + | ) -> SimpleServiceConfigBuilder<::tower::layer::util::Stack<NewLayer, L>, H, M> {
|
571 + | SimpleServiceConfigBuilder {
|
572 + | layers: ::tower::layer::util::Stack::new(layer, self.layers),
|
573 + | http_plugins: self.http_plugins,
|
574 + | model_plugins: self.model_plugins,
|
575 + | }
|
576 + | }
|
577 + |
|
578 + | /// Add a HTTP [plugin] to the service.
|
579 + | ///
|
580 + | /// [plugin]: ::aws_smithy_legacy_http_server::plugin
|
581 + | // We eagerly require `NewPlugin: HttpMarker`, despite not really needing it, because compiler
|
582 + | // errors get _substantially_ better if the user makes a mistake.
|
583 + | pub fn http_plugin<NewPlugin: ::aws_smithy_legacy_http_server::plugin::HttpMarker>(
|
584 + | self,
|
585 + | http_plugin: NewPlugin,
|
586 + | ) -> SimpleServiceConfigBuilder<
|
587 + | L,
|
588 + | ::aws_smithy_legacy_http_server::plugin::PluginStack<NewPlugin, H>,
|
589 + | M,
|
590 + | > {
|
591 + | SimpleServiceConfigBuilder {
|
592 + | layers: self.layers,
|
593 + | http_plugins: ::aws_smithy_legacy_http_server::plugin::PluginStack::new(
|
594 + | http_plugin,
|
595 + | self.http_plugins,
|
596 + | ),
|
597 + | model_plugins: self.model_plugins,
|
598 + | }
|
599 + | }
|
600 + |
|
601 + | /// Add a model [plugin] to the service.
|
602 + | ///
|
603 + | /// [plugin]: ::aws_smithy_legacy_http_server::plugin
|
604 + | // We eagerly require `NewPlugin: ModelMarker`, despite not really needing it, because compiler
|
605 + | // errors get _substantially_ better if the user makes a mistake.
|
606 + | pub fn model_plugin<NewPlugin: ::aws_smithy_legacy_http_server::plugin::ModelMarker>(
|
607 + | self,
|
608 + | model_plugin: NewPlugin,
|
609 + | ) -> SimpleServiceConfigBuilder<
|
610 + | L,
|
611 + | H,
|
612 + | ::aws_smithy_legacy_http_server::plugin::PluginStack<NewPlugin, M>,
|
613 + | > {
|
614 + | SimpleServiceConfigBuilder {
|
615 + | layers: self.layers,
|
616 + | http_plugins: self.http_plugins,
|
617 + | model_plugins: ::aws_smithy_legacy_http_server::plugin::PluginStack::new(
|
618 + | model_plugin,
|
619 + | self.model_plugins,
|
620 + | ),
|
621 + | }
|
622 + | }
|
623 + |
|
624 + | /// Build the configuration.
|
625 + | pub fn build(self) -> super::SimpleServiceConfig<L, H, M> {
|
626 + | super::SimpleServiceConfig {
|
627 + | layers: self.layers,
|
628 + | http_plugins: self.http_plugins,
|
629 + | model_plugins: self.model_plugins,
|
630 + | }
|
631 + | }
|
632 + | }
|
633 + | /// A macro to help with scoping [plugins](crate::server::plugin) to a subset of all operations.
|
634 + | ///
|
635 + | /// In contrast to [`crate::server::scope`](crate::server::scope), this macro has knowledge
|
636 + | /// of the service and any operations _not_ specified will be placed in the opposing group.
|
637 + | ///
|
638 + | /// # Example
|
639 + | ///
|
640 + | /// ```rust
|
641 + | /// scope! {
|
642 + | /// /// Includes [`Operation`], excluding all other operations.
|
643 + | /// struct ScopeA {
|
644 + | /// includes: [Operation]
|
645 + | /// }
|
646 + | /// }
|
647 + | ///
|
648 + | /// scope! {
|
649 + | /// /// Excludes [`Operation`], excluding all other operations.
|
650 + | /// struct ScopeB {
|
651 + | /// excludes: [Operation]
|
652 + | /// }
|
653 + | /// }
|
654 + | ///
|
655 + | /// # use simple_http0x::server::plugin::{Plugin, Scoped};
|
656 + | /// # use simple_http0x::scope;
|
657 + | /// # struct MockPlugin;
|
658 + | /// # impl<S, Op, T> Plugin<S, Op, T> for MockPlugin { type Output = u32; fn apply(&self, input: T) -> u32 { 3 } }
|
659 + | /// # let scoped_a = Scoped::new::<ScopeA>(MockPlugin);
|
660 + | /// # let scoped_b = Scoped::new::<ScopeB>(MockPlugin);
|
661 + | /// # let a = Plugin::<(), simple_http0x::operation_shape::Operation, u64>::apply(&scoped_a, 6);
|
662 + | /// # let b = Plugin::<(), simple_http0x::operation_shape::Operation, u64>::apply(&scoped_b, 6);
|
663 + | /// # assert_eq!(a, 3_u32);
|
664 + | /// # assert_eq!(b, 6_u64);
|
665 + | /// ```
|
666 + | #[macro_export]
|
667 + | macro_rules! scope {
|
668 + | // Completed, render impls
|
669 + | (@ $ name: ident, $ contains: ident () ($($ temp: ident)*) ($($ not_member: ident)*)) => {
|
670 + | $(
|
671 + | impl $ crate::server::plugin::scoped::Membership<$ temp> for $ name {
|
672 + | type Contains = $ crate::server::plugin::scoped::$ contains;
|
673 + | }
|
674 + | )*
|
675 + | $(
|
676 + | impl $ crate::server::plugin::scoped::Membership<$ not_member> for $ name {
|
677 + | type Contains = $ crate::server::plugin::scoped::$ contains;
|
678 + | }
|
679 + | )*
|
680 + | };
|
681 + | // All `not_member`s exhausted, move `temp` into `not_member`
|
682 + | (@ $ name: ident, $ contains: ident ($($ member: ident)*) ($($ temp: ident)*) ()) => {
|
683 + | scope! { @ $ name, $ contains ($($ member)*) () ($($ temp)*) }
|
684 + | };
|
685 + |
|
686 + | // Operation match found, pop from both `member` and `not_member`
|
687 + | (@ $ name: ident, $ contains: ident (Operation $($ member: ident)*) ($($ temp: ident)*) (Operation $($ not_member: ident)*)) => {
|
688 + | scope! { @ $ name, $ contains ($($ member)*) ($($ temp)*) ($($ not_member)*) }
|
689 + | };
|
690 + | // Operation match not found, pop from `not_member` into `temp` stack
|
691 + | (@ $ name: ident, $ contains: ident (Operation $($ member: ident)*) ($($ temp: ident)*) ($ other: ident $($ not_member: ident)*)) => {
|
692 + | scope! { @ $ name, $ contains (Operation $($ member)*) ($ other $($ temp)*) ($($ not_member)*) }
|
693 + | };
|
694 + |
|
695 + | (
|
696 + | $(#[$ attrs:meta])*
|
697 + | $ vis:vis struct $ name:ident {
|
698 + | includes: [$($ include:ident),*]
|
699 + | }
|
700 + | ) => {
|
701 + | use $ crate::operation_shape::*;
|
702 + | $ crate::server::scope! {
|
703 + | $(#[$ attrs])*
|
704 + | $ vis struct $ name {
|
705 + | includes: [$($ include),*],
|
706 + | excludes: []
|
707 + | }
|
708 + | }
|
709 + | scope! { @ $ name, False ($($ include)*) () (Operation) }
|
710 + | };
|
711 + | (
|
712 + | $(#[$ attrs:meta])*
|
713 + | $ vis:vis struct $ name:ident {
|
714 + | excludes: [$($ exclude:ident),*]
|
715 + | }
|
716 + | ) => {
|
717 + | use $ crate::operation_shape::*;
|
718 + |
|
719 + | $ crate::server::scope! {
|
720 + | $(#[$ attrs])*
|
721 + | $ vis struct $ name {
|
722 + | includes: [],
|
723 + | excludes: [$($ exclude),*]
|
724 + | }
|
725 + | }
|
726 + | scope! { @ $ name, True ($($ exclude)*) () (Operation) }
|
727 + | };
|
728 + | }
|