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
183
184
185
186
187
188
189
//! # Malaga HTTP Utils
//! 
//! `malaga_http_utils` is a small library to handler request and response objects
//! 
//! # Example:
//! ```
//! #[macro_use]
//! extern crate serde_derive;
//! 
//! extern crate serde_json;
//! extern crate serde;
//! extern crate bincode;
//! extern crate malaga_http_utils;
//! 
//! use malaga_http_utils::{HttpObj, utils::*};
//! use std::str;
//! 
//! impl HttpObj for TestReq {
//!    fn serialize_to_struct(req: &[u8]) -> Self {
//!        let req_string = str::from_utf8(req).unwrap();
//!        let ser_req: TestReq = serde_json::from_str(req_string).unwrap();
//!        
//!        ser_req
//!    }
//! 
//!     fn serialize_to_binary(req: TestReq) -> Vec<u8> {
//!         let ser_req: Vec<u8> = bincode::serialize(&req).unwrap();
//! 
//!         ser_req
//!     } 
//! }
//! 
//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
//!     struct TestHeader {
//!         #[serde(rename = "Content-Type")]
//!         content_type: String,
//!         #[serde(rename = "Authorization")]
//!         pub authorization: String,
//!     }
//! 
//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
//! struct Body {
//!     user: String,
//! }
//! 
//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
//! struct TestReq {
//!     headers: TestHeader,
//!     method: Methods,
//!     body: Body,
//! }
//! 
//! fn main() {
//!     let resq = b"{
//!        \"headers\": {
//!            \"Content-Type\": \"application/json\",
//!            \"Authorization\": \"Basis 1ddmcdd\"
//!        },
//!        \"method\": \"POST\",
//!        \"body\": {
//!            \"user\":\"test\"
//!        }
//!    }";
//!
//!    let resq_struct_ser: TestReq = TestReq::serialize_to_struct(resq);
//!    let resq_binary_ser: Vec<u8> = TestReq::serialize_to_binary(resq_struct_ser);
//!    let req_string: TestReq = bincode::deserialize(&resq_binary_ser[..]).unwrap();
//! 
//!    assert_eq!(req_string, TestReq{
//!        headers: TestHeader {
//!            content_type: String::from("application/json"),
//!            authorization: String::from("Basis 1ddmcdd"),
//!        },
//!        method: Methods::POST,
//!        body: Body {
//!            user: String::from("test")
//!        }
//!    });
//! }
//! ```

#[macro_use]
extern crate serde_derive;

extern crate serde_json;
extern crate serde;
extern crate bincode;

pub mod utils;

/// trait to serialize http objects to binary or struct
pub trait HttpObj {
    fn serialize_to_struct(http_bin: &[u8]) -> Self;
    fn serialize_to_binary(http_obj: Self) -> Vec<u8>;
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::str;

    impl HttpObj for TestReq {
        fn serialize_to_struct(req: &[u8]) -> Self {
            let req_string = str::from_utf8(req).unwrap();
            let ser_req: TestReq = serde_json::from_str(req_string).unwrap();
            
            ser_req
        }

        fn serialize_to_binary(req: TestReq) -> Vec<u8> {
            let ser_req: Vec<u8> = bincode::serialize(&req).unwrap();

            ser_req
        } 
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
        struct TestHeader {
            #[serde(rename = "Content-Type")]
            content_type: String,
            #[serde(rename = "Authorization")]
            pub authorization: String,
        }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    struct Body {
        user: String,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    struct TestReq {
        headers: TestHeader,
        method: utils::Methods,
        body: Body,
    }

    #[test]
    fn it_should_serialize_to_strcut() {

        let resq = b"{
            \"headers\": {
                \"Content-Type\": \"application/json\",
                \"Authorization\": \"Basis 1ddmcdd\"
            },
            \"method\": \"POST\",
            \"body\": {
                \"user\":\"test\"
            }
        }";

        let resq_serialized: TestReq = TestReq::serialize_to_struct(resq);
        let method = utils::get_method(resq_serialized.method);

        assert_eq!(resq_serialized.body.user, "test");
        assert_eq!(method, "POST");
    }

    #[test]
    fn it_should_serializa_to_binary() {
        let resq = b"{
            \"headers\": {
                \"Content-Type\": \"application/json\",
                \"Authorization\": \"Basis 1ddmcdd\"
            },
            \"method\": \"POST\",
            \"body\": {
                \"user\":\"test\"
            }
        }";

        let resq_struct_ser: TestReq = TestReq::serialize_to_struct(resq);

        let resq_binary_ser: Vec<u8> = TestReq::serialize_to_binary(resq_struct_ser);

        let req_string: TestReq = bincode::deserialize(&resq_binary_ser[..]).unwrap();
        
        
        assert_eq!(req_string, TestReq{
            headers: TestHeader {
                content_type: String::from("application/json"),
                authorization: String::from("Basis 1ddmcdd"),
            },
            method: utils::Methods::POST,
            body: Body {
                user: String::from("test")
            }
        });
    }
}