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
use response::{Serializer, SerializerContext};

use bytes::Bytes;
use http;
use http::header::HeaderValue;
use serde::Serialize;

/// Context available when serializing the response.
#[derive(Debug)]
pub struct Context<'a, S: Serializer + 'a> {
    request: &'a http::Request<()>,
    serializer: &'a S,
    default_format: Option<&'a S::Format>,
    content_type: Option<&'a HeaderValue>,
    resource_mod: Option<&'a str>,
    resource_name: Option<&'a str>,
    handler_name: Option<&'a str>,
    template: Option<&'a str>,
}

impl<'a, S> Context<'a, S>
where
    S: Serializer,
{
    /// Create a new response context.
    pub fn new(request: &'a http::Request<()>, serializer: &'a S) -> Context<'a, S>
    {
        Context {
            request,
            serializer,
            default_format: None,
            content_type: None,
            resource_mod: None,
            resource_name: None,
            handler_name: None,
            template: None,
        }
    }

    /// Returns a reference to the request
    pub fn request(&self) -> &http::Request<()> {
        self.request
    }

    #[doc(hidden)]
    pub fn set_resource_mod(&mut self, value: &'a str) {
        self.resource_mod = Some(value);
    }

    #[doc(hidden)]
    pub fn set_resource_name(&mut self, value: &'a str) {
        self.resource_name = Some(value);
    }

    #[doc(hidden)]
    pub fn set_handler_name(&mut self, value: &'a str) {
        self.handler_name = Some(value);
    }

    #[doc(hidden)]
    pub fn serializer_context(&self) -> SerializerContext {
        let mut ret = SerializerContext::new(self.request);
        ret.set_resource_mod(self.resource_mod);
        ret.set_resource_name(self.resource_name);
        ret.set_handler_name(self.handler_name);

        if let Some(template) = self.template {
            ret.set_template(template);
        }

        ret
    }

    #[doc(hidden)]
    pub fn set_default_format(&mut self, value: &'a S::Format) {
        self.default_format = Some(value);
    }

    #[doc(hidden)]
    pub fn set_content_type(&mut self, value: &'a HeaderValue) {
        self.content_type = Some(value);
    }

    /// Returns the `template` value set for the response.
    pub fn template(&self) -> Option<&str> {
        self.template
    }

    #[doc(hidden)]
    pub fn set_template(&mut self, value: &'a str) {
        self.template = Some(value);
    }

    /// Serialize a value.
    ///
    /// This uses the default content type for the action.
    ///
    /// Returns an error when a default content type is not set.
    pub fn serialize<T>(&self, value: &T, context: &SerializerContext)
        -> Result<Bytes, ::Error>
    where
        T: Serialize,
    {
        let format = match self.default_format {
            Some(format) => format,
            None => {
                warn!("no default serialization format associated with action");
                return Err(::error::ErrorKind::internal().into());
            }
        };

        self.serializer.serialize(value, format, context)
    }

    /// Serialize a value as the specified content type.
    pub fn serialize_as<T>(&self, _value: &T, _content_type: &str)
        -> Result<Bytes, ::Error>
    where
        T: Serialize,
    {
        unimplemented!();
    }

    /// Returns a `HeaderValue` representation of the default content type.
    pub fn content_type_header(&self) -> Option<&HeaderValue> {
        self.content_type
    }
}