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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! `Modifier` and supplemental components.
//!
//! The purpose of `Modifier` is to insert some processes before and after
//! applying `Handler` in a certain scope.
//!
//! # Examples
//!
//! ```
//! use std::sync::atomic::{AtomicUsize, Ordering};
//! use tsukuyomi::{App, Input, handler};
//! use tsukuyomi::modifier::{Modifier, BeforeHandle, AfterHandle};
//!
//! #[derive(Default)]
//! struct RequestCounter(AtomicUsize);
//!
//! impl Modifier for RequestCounter {
//!     fn before_handle(&self, _: &mut Input) -> BeforeHandle {
//!        self.0.fetch_add(1, Ordering::SeqCst);
//!        BeforeHandle::ready(Ok(None))
//!     }
//! }
//!
//! # fn main() -> tsukuyomi::AppResult<()> {
//! let app = App::builder()
//!     .route(("/", handler::wrap_ready(|_| "Hello")))
//!     .modifier(RequestCounter::default())
//!     .finish()?;
//! # Ok(())
//! # }
//! ```

use futures::{self, Future, Poll};
use std::fmt;

use error::Error;
use input::{self, Input};
use output::Output;

/// A trait representing a `Modifier`.
///
/// See the module level documentation for details.
pub trait Modifier {
    /// Performs the process before calling the handler.
    ///
    /// By default, this method does nothing.
    #[allow(unused_variables)]
    fn before_handle(&self, input: &mut Input) -> BeforeHandle {
        BeforeHandle::ready(Ok(None))
    }

    /// Modifies the returned value from a handler.
    ///
    /// By default, this method does nothing and immediately return the provided `Output`.
    #[allow(unused_variables)]
    fn after_handle(&self, input: &mut Input, result: Result<Output, Error>) -> AfterHandle {
        AfterHandle::ready(result)
    }
}

// ==== BeforeHandle ====

/// The type representing a return value from `Modifier::before_handle`.
#[derive(Debug)]
pub struct BeforeHandle(BeforeHandleState);

// MEMO:
// The internal type should be replaced with `Option<Result<Output, Error>>`.
// Currently, it is represented as `Result<T, E>` due to the restriction of `futures`.
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
enum BeforeHandleState {
    Ready(Option<Result<Option<Output>, Error>>),
    Polling(Box<dyn FnMut(&mut Input) -> Poll<Option<Output>, Error> + Send + 'static>),
}

#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for BeforeHandleState {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::BeforeHandleState::*;
        match *self {
            Ready(ref res) => f.debug_tuple("Ready").field(res).finish(),
            Polling(..) => f.debug_tuple("Polling").finish(),
        }
    }
}

impl<E> From<Result<Option<Output>, E>> for BeforeHandle
where
    Error: From<E>,
{
    fn from(result: Result<Option<Output>, E>) -> BeforeHandle {
        BeforeHandle::ready(result.map_err(Into::into))
    }
}

impl BeforeHandle {
    /// Creates a `BeforeHandle` from an immediately value.
    pub fn ready(result: Result<Option<Output>, Error>) -> BeforeHandle {
        BeforeHandle(BeforeHandleState::Ready(Some(result)))
    }

    /// Creates a `BeforeHandle` from a closure repsenting an asynchronous computation.
    pub fn polling(f: impl FnMut(&mut Input) -> Poll<Option<Output>, Error> + Send + 'static) -> BeforeHandle {
        BeforeHandle(BeforeHandleState::Polling(Box::new(f)))
    }

    /// Creates a `BeforeHandle` from a future.
    #[inline(always)]
    pub fn wrap_future(mut future: impl Future<Item = Option<Output>, Error = Error> + Send + 'static) -> BeforeHandle {
        BeforeHandle::polling(move |input| input::with_set_current(input, || future.poll()))
    }

    pub(crate) fn poll_ready(&mut self, input: &mut Input) -> Poll<Option<Output>, Error> {
        use self::BeforeHandleState::*;
        match self.0 {
            Ready(ref mut res) => res.take()
                .expect("BeforeHandle has already polled")
                .map(futures::Async::Ready),
            Polling(ref mut f) => f(input),
        }
    }
}

// ==== AfterHandle ====

/// The type representing a return value from `Modifier::after_handle`.
#[derive(Debug)]
pub struct AfterHandle(AfterHandleState);

#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
enum AfterHandleState {
    Ready(Option<Result<Output, Error>>),
    Polling(Box<dyn FnMut(&mut Input) -> Poll<Output, Error> + Send + 'static>),
}

#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for AfterHandleState {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::AfterHandleState::*;
        match *self {
            Ready(ref res) => f.debug_tuple("Ready").field(res).finish(),
            Polling(..) => f.debug_tuple("Polling").finish(),
        }
    }
}

impl<T, E> From<Result<T, E>> for AfterHandle
where
    T: Into<Output>,
    Error: From<E>,
{
    fn from(result: Result<T, E>) -> AfterHandle {
        AfterHandle::ready(result.map(Into::into).map_err(Into::into))
    }
}

impl AfterHandle {
    /// Creates an `AfterHandle` from an immediately value.
    pub fn ready(result: Result<Output, Error>) -> AfterHandle {
        AfterHandle(AfterHandleState::Ready(Some(result)))
    }

    /// Creates an `AfterHandle` from a closure repsenting an asynchronous computation.
    pub fn polling(f: impl FnMut(&mut Input) -> Poll<Output, Error> + Send + 'static) -> AfterHandle {
        AfterHandle(AfterHandleState::Polling(Box::new(f)))
    }

    /// Creates an `AfterHandle` from a `Future`.
    #[inline(always)]
    pub fn wrap_future(mut future: impl Future<Item = Output, Error = Error> + Send + 'static) -> AfterHandle {
        AfterHandle::polling(move |input| input::with_set_current(input, || future.poll()))
    }

    pub(crate) fn poll_ready(&mut self, input: &mut Input) -> Poll<Output, Error> {
        use self::AfterHandleState::*;
        match self.0 {
            Ready(ref mut res) => res.take()
                .expect("AfterHandle has already polled")
                .map(futures::Async::Ready),
            Polling(ref mut f) => (*f)(input),
        }
    }
}