refactor: Introduce json feature and make it optional
Gates support for `serde_json` behind a Cargo feature called `json` that is enabled by default.
This commit is contained in:
parent
479a6b3442
commit
c6c1746428
2 changed files with 18 additions and 7 deletions
|
|
@ -3,7 +3,11 @@ name = "crimp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Vincent Ambo <mail@tazj.in>"]
|
authors = ["Vincent Ambo <mail@tazj.in>"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = [ "json" ]
|
||||||
|
json = [ "serde", "serde_json"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
curl = "0.4"
|
curl = "0.4"
|
||||||
serde = "1.0"
|
serde = { version = "1.0", optional = true }
|
||||||
serde_json = "1.0"
|
serde_json = { version = "1.0", optional = true }
|
||||||
|
|
|
||||||
17
src/lib.rs
17
src/lib.rs
|
|
@ -12,12 +12,13 @@
|
||||||
//! [reqwest]: https://docs.rs/reqwest
|
//! [reqwest]: https://docs.rs/reqwest
|
||||||
|
|
||||||
extern crate curl;
|
extern crate curl;
|
||||||
extern crate serde;
|
|
||||||
extern crate serde_json;
|
#[cfg(feature = "json")] extern crate serde;
|
||||||
|
#[cfg(feature = "json")] extern crate serde_json;
|
||||||
|
|
||||||
use curl::easy::{Easy, List, ReadError};
|
use curl::easy::{Easy, List, ReadError};
|
||||||
use serde::Serialize;
|
#[cfg(feature = "json")] use serde::Serialize;
|
||||||
use serde::de::DeserializeOwned;
|
#[cfg(feature = "json")] use serde::de::DeserializeOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::string::{FromUtf8Error, ToString};
|
use std::string::{FromUtf8Error, ToString};
|
||||||
|
|
@ -40,6 +41,7 @@ pub struct Request<'a> {
|
||||||
|
|
||||||
enum Body<'a> {
|
enum Body<'a> {
|
||||||
NoBody,
|
NoBody,
|
||||||
|
#[cfg(feature = "json")]
|
||||||
Json(Vec<u8>),
|
Json(Vec<u8>),
|
||||||
Bytes {
|
Bytes {
|
||||||
content_type: &'a str,
|
content_type: &'a str,
|
||||||
|
|
@ -95,6 +97,7 @@ impl <'a> Request<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a JSON-encoded body from a serializable type.
|
/// Add a JSON-encoded body from a serializable type.
|
||||||
|
#[cfg(feature = "json")]
|
||||||
pub fn json<T: Serialize>(&'a mut self, body: &T)
|
pub fn json<T: Serialize>(&'a mut self, body: &T)
|
||||||
-> Result<&mut Self, serde_json::Error> {
|
-> Result<&mut Self, serde_json::Error> {
|
||||||
let json = serde_json::to_vec(body)?;
|
let json = serde_json::to_vec(body)?;
|
||||||
|
|
@ -112,9 +115,11 @@ impl <'a> Request<'a> {
|
||||||
// Optionally set content type if a body payload is
|
// Optionally set content type if a body payload is
|
||||||
// configured.
|
// configured.
|
||||||
match self.body {
|
match self.body {
|
||||||
Body::Json(..) => self.header("Content-Type", "application/json"),
|
|
||||||
Body::Bytes { content_type, .. } => self.header("Content-Type", content_type),
|
Body::Bytes { content_type, .. } => self.header("Content-Type", content_type),
|
||||||
Body::NoBody => Ok(&mut self),
|
Body::NoBody => Ok(&mut self),
|
||||||
|
|
||||||
|
#[cfg(feature = "json")]
|
||||||
|
Body::Json(..) => self.header("Content-Type", "application/json"),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
// Configure headers on the request:
|
// Configure headers on the request:
|
||||||
|
|
@ -134,6 +139,7 @@ impl <'a> Request<'a> {
|
||||||
.map_err(|_| ReadError::Abort)
|
.map_err(|_| ReadError::Abort)
|
||||||
})?,
|
})?,
|
||||||
|
|
||||||
|
#[cfg(feature = "json")]
|
||||||
Body::Json(json) => transfer.read_function(move |mut into| {
|
Body::Json(json) => transfer.read_function(move |mut into| {
|
||||||
into.write_all(&json)
|
into.write_all(&json)
|
||||||
.map(|_| json.len())
|
.map(|_| json.len())
|
||||||
|
|
@ -201,6 +207,7 @@ impl CurlResponse<Vec<u8>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to deserialize the HTTP response body from JSON.
|
/// Attempt to deserialize the HTTP response body from JSON.
|
||||||
|
#[cfg(feature = "json")]
|
||||||
pub fn as_json<T: DeserializeOwned>(self) -> Result<CurlResponse<T>, serde_json::Error> {
|
pub fn as_json<T: DeserializeOwned>(self) -> Result<CurlResponse<T>, serde_json::Error> {
|
||||||
let deserialized = serde_json::from_slice(&self.body)?;
|
let deserialized = serde_json::from_slice(&self.body)?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue