Skip to content

Commit

Permalink
Remove serde_derive
Browse files Browse the repository at this point in the history
Had to tweak the JSON examples a bit, but I think they're still
readable and clear. Hopefully this fixes the MSRV tests!
  • Loading branch information
neonmoe committed Aug 24, 2023
1 parent 7d72994 commit 750aced
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 37 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ native-tls = { version = "0.2", optional = true }

[dev-dependencies]
tiny_http = "0.8.2"
serde = { version = "=1.0.156", features = ["derive"] }

[package.metadata.docs.rs]
features = ["json-using-serde", "proxy", "https", "punycode"]

[features]
default = ["https"]
https = ["https-rustls"]
https-rustls = ["rustls", "once_cell", "webpki-roots", "rustls-webpki"]
https-rustls-probe = ["https-rustls", "rustls-native-certs"]
Expand Down
13 changes: 5 additions & 8 deletions examples/json.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/// This example demonstrates the `json-using-serde` feature.

#[derive(serde::Deserialize)]
struct Response {
/// The field in which `http://httpbin.org/anything` returns the body.
data: String,
}

fn main() -> Result<(), minreq::Error> {
let response = minreq::get("http://httpbin.org/anything")
.with_body("Hello, world!")
.send()?;
let json: Response = response.json()?;
println!("Hello, world! == {}", &json.data);

// httpbin.org/anything returns the body in the json field "data":
let json: serde_json::Value = response.json()?;
println!("\"Hello, world!\" == {}", json["data"]);

Ok(())
}
2 changes: 2 additions & 0 deletions src/native_tls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Derived from https://lib.rs/crates/native-tls.

#![allow(warnings)]

use std::any::Any;
use std::error;
use std::fmt;
Expand Down
15 changes: 4 additions & 11 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,13 @@ impl Response {
/// In case compiler cannot figure out return type you might need to declare it explicitly:
///
/// ```no_run
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct User {
/// name: String,
/// email: String,
/// }
/// use serde_json::Value;
///
/// # fn main() -> Result<(), minreq::Error> {
/// # let url_to_json_resource = "http://example.org/resource.json";
/// let user_name = minreq::get(url_to_json_resource).send()?
/// .json::<User>()? // explicitly declared type `User`
/// .name;
/// println!("User name is '{}'", &user_name);
/// // Value could be any type that implements Deserialize!
/// let user = minreq::get(url_to_json_resource).send()?.json::<Value>()?;
/// println!("User name is '{}'", user["name"]);
/// # Ok(())
/// # }
/// ```
Expand Down
22 changes: 6 additions & 16 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
extern crate minreq;
mod setup;

#[cfg(feature = "json-using-serde")]
use serde::{Deserialize, Serialize};

use self::setup::*;
use std::io;

Expand All @@ -17,28 +14,21 @@ fn test_https() {
);
}

#[cfg(feature = "json-using-serde")]
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
struct Json<'a> {
str: &'a str,
num: u32,
}

#[test]
#[cfg(feature = "json-using-serde")]
fn test_json_using_serde() {
let original_json = Json {
str: "Json test",
num: 42,
};
const JSON_SRC: &str = r#"{
"str": "Json test",
"num": 42
}"#;

let original_json: serde_json::Value = serde_json::from_str(JSON_SRC).unwrap();
let response = minreq::post(url("/echo"))
.with_json(&original_json)
.unwrap()
.send()
.unwrap();
let actual_json: Json = response.json().unwrap();

let actual_json: serde_json::Value = response.json().unwrap();
assert_eq!(&actual_json, &original_json);
}

Expand Down

0 comments on commit 750aced

Please sign in to comment.