ajj/routes/method.rs
1use crate::{routes::RouteFuture, BoxedIntoRoute, HandlerArgs, Route};
2
3/// A method, which may be ready to handle requests or may need to be
4/// initialized with some state.
5///
6/// Analagous to axum's `MethodEndpoint`
7pub(crate) enum Method<S> {
8 /// A method that needs to be initialized with some state.
9 Needs(BoxedIntoRoute<S>),
10 /// A method that is ready to handle requests.
11 Ready(Route),
12}
13
14impl<S> Clone for Method<S> {
15 fn clone(&self) -> Self {
16 match self {
17 Self::Needs(handler) => Self::Needs(handler.clone()),
18 Self::Ready(route) => Self::Ready(route.clone()),
19 }
20 }
21}
22
23impl<S> Method<S> {
24 /// Call the method with the given state and request.
25 pub(crate) fn call_with_state(&self, args: HandlerArgs, state: S) -> RouteFuture {
26 match self {
27 Self::Ready(route) => route.clone().oneshot_inner_owned(args),
28 Self::Needs(handler) => handler
29 .clone()
30 .0
31 .into_route(state)
32 .oneshot_inner_owned(args),
33 }
34 }
35}
36
37impl<S> Method<S>
38where
39 S: Clone,
40{
41 /// Add state to a method, converting
42 pub(crate) fn with_state<S2>(self, state: &S) -> Method<S2> {
43 match self {
44 Self::Ready(route) => Method::Ready(route),
45 Self::Needs(handler) => Method::Ready(handler.0.into_route(state.clone())),
46 }
47 }
48}
49
50// Some code is this file is reproduced under the terms of the MIT license. It
51// originates from the `axum` crate. The original source code can be found at
52// the following URL, and the original license is included below.
53//
54// https://github.com/tokio-rs/axum/blob/f84105ae8b078109987b089c47febc3b544e6b80/axum/src/routing/mod.rs#L119
55//
56// The MIT License (MIT)
57//
58// Copyright (c) 2019 Axum Contributors
59//
60// Permission is hereby granted, free of charge, to any
61// person obtaining a copy of this software and associated
62// documentation files (the "Software"), to deal in the
63// Software without restriction, including without
64// limitation the rights to use, copy, modify, merge,
65// publish, distribute, sublicense, and/or sell copies of
66// the Software, and to permit persons to whom the Software
67// is furnished to do so, subject to the following
68// conditions:
69//
70// The above copyright notice and this permission notice
71// shall be included in all copies or substantial portions
72// of the Software.
73//
74// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
75// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
76// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
77// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
78// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
79// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
80// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
81// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
82// DEALINGS IN THE SOFTWARE.