1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use std::{
    marker::PhantomData,
    task::{Context, Poll},
};

use tower::Service;

use super::OperationShape;

/// A utility trait used to provide an even interface for all operation services.
///
/// This serves to take [`Service`]s of the form `Service<(Op::Input, Ext0, Ext1, ...)>` to the canonical
/// representation of `Service<(Input, (Ext0, Ext1, ...))>` inline with
/// [`IntoService`](super::IntoService).
///
/// See [`operation`](crate::operation) documentation for more info.
pub trait OperationService<Op, Exts>: Service<Self::Normalized, Response = Op::Output, Error = Op::Error>
where
    Op: OperationShape,
{
    type Normalized;

    // Normalize the request type.
    fn normalize(input: Op::Input, exts: Exts) -> Self::Normalized;
}

// `Service<Op::Input>`
impl<Op, S> OperationService<Op, ()> for S
where
    Op: OperationShape,
    S: Service<Op::Input, Response = Op::Output, Error = Op::Error>,
{
    type Normalized = Op::Input;

    fn normalize(input: Op::Input, _exts: ()) -> Self::Normalized {
        input
    }
}

// `Service<(Op::Input, Ext0)>`
impl<Op, Ext0, S> OperationService<Op, (Ext0,)> for S
where
    Op: OperationShape,
    S: Service<(Op::Input, Ext0), Response = Op::Output, Error = Op::Error>,
{
    type Normalized = (Op::Input, Ext0);

    fn normalize(input: Op::Input, exts: (Ext0,)) -> Self::Normalized {
        (input, exts.0)
    }
}

// `Service<(Op::Input, Ext0, Ext1)>`
impl<Op, Ext0, Ext1, S> OperationService<Op, (Ext0, Ext1)> for S
where
    Op: OperationShape,
    S: Service<(Op::Input, Ext0, Ext1), Response = Op::Output, Error = Op::Error>,
{
    type Normalized = (Op::Input, Ext0, Ext1);

    fn normalize(input: Op::Input, exts: (Ext0, Ext1)) -> Self::Normalized {
        (input, exts.0, exts.1)
    }
}

/// An extension trait of [`OperationService`].
pub trait OperationServiceExt<Op, Exts>: OperationService<Op, Exts>
where
    Op: OperationShape,
{
    /// Convert the [`OperationService`] into a canonicalized [`Service`].
    fn normalize(self) -> Normalize<Op, Self>
    where
        Self: Sized,
    {
        Normalize {
            inner: self,
            _operation: PhantomData,
        }
    }
}

impl<F, Op, Exts> OperationServiceExt<Op, Exts> for F
where
    Op: OperationShape,
    F: OperationService<Op, Exts>,
{
}

/// A [`Service`] normalizing the request type of a [`OperationService`].
#[derive(Debug)]
pub struct Normalize<Op, S> {
    pub(crate) inner: S,
    pub(crate) _operation: PhantomData<Op>,
}

impl<Op, S> Clone for Normalize<Op, S>
where
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _operation: PhantomData,
        }
    }
}

impl<Op, S, Exts> Service<(Op::Input, Exts)> for Normalize<Op, S>
where
    Op: OperationShape,
    S: OperationService<Op, Exts>,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = <S as Service<S::Normalized>>::Future;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, (input, exts): (Op::Input, Exts)) -> Self::Future {
        let req = S::normalize(input, exts);
        self.inner.call(req)
    }
}