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
use serde::de::DeserializeOwned;
use std::ops::{Deref, DerefMut};

use json_api::{self, Error};
use json_api::doc::{NewObject, Object};
use json_api::query::{self, Page, Query as JsonApiQuery, Sort};
use json_api::value::{Key, Path, Value};
use json_api::value::collections::{map, set, Set};
use rocket::data::{self, Data, FromData};
use rocket::http::Status;
use rocket::outcome::IntoOutcome;
use rocket::request::{self, FromRequest, Request};

#[derive(Debug)]
pub struct Create<T: DeserializeOwned>(pub T);

impl<T: DeserializeOwned> Create<T> {
    /// Consumes the `Create` wrapper and returns the wrapped value.
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T: DeserializeOwned> Deref for Create<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: DeserializeOwned> DerefMut for Create<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: DeserializeOwned> FromData for Create<T> {
    type Error = Error;

    fn from_data(_: &Request, data: Data) -> data::Outcome<Self, Self::Error> {
        json_api::from_reader::<_, NewObject, _>(data.open())
            .map(Create)
            .into_outcome(Status::BadRequest)
    }
}

#[derive(Debug)]
pub struct Update<T: DeserializeOwned>(pub T);

impl<T: DeserializeOwned> Update<T> {
    /// Consumes the `Update` wrapper and returns the wrapped value.
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T: DeserializeOwned> Deref for Update<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: DeserializeOwned> DerefMut for Update<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: DeserializeOwned> FromData for Update<T> {
    type Error = Error;

    fn from_data(_: &Request, data: Data) -> data::Outcome<Self, Self::Error> {
        json_api::from_reader::<_, Object, _>(data.open())
            .map(Update)
            .into_outcome(Status::BadRequest)
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Query {
    inner: JsonApiQuery,
}

impl Query {
    /// Consumes the [`Query`] wrapper and returns the wrapped value.
    ///
    /// [`Query`]: ./struct.Query.html
    pub fn into_inner(self) -> JsonApiQuery {
        self.inner
    }

    pub fn fields(&self) -> map::Iter<Key, Set> {
        self.inner.fields.iter()
    }

    pub fn filter(&self) -> map::Iter<Path, Value> {
        self.inner.filter.iter()
    }

    pub fn include(&self) -> set::Iter<Path> {
        self.inner.include.iter()
    }

    pub fn page(&self) -> Option<Page> {
        self.inner.page
    }

    pub fn sort(&self) -> set::Iter<Sort> {
        self.inner.sort.iter()
    }
}

impl<'a, 'r> FromRequest<'a, 'r> for Query {
    type Error = Error;

    fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
        let data = req.uri().query();

        data.map(query::from_str)
            .unwrap_or_else(|| Ok(Default::default()))
            .map(|inner| Query { inner })
            .into_outcome(Status::BadRequest)
    }
}