Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Add hyper::Uri support #21

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ doctest = false

[dependencies]
cookie = {version = "0.6", default-features = false}
hyper = "0.10"
mime = "0.2"
hyper = { version = "0.11", features = ["raw_status"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is raw_status needed?

mime = "0.3"
serde = "1.0"
serde_bytes = "0.10"
time = "0.1"

[dev-dependencies]
serde_test = "1.0"
time = "0.1"

[replace]
"hyper:0.11.2" = { git = "https://github.com/hyperium/hyper.git" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here?

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The supported types are:
* `hyper::header::Headers`
* `hyper::http::RawStatus`
* `hyper::method::Method`
* `hyper::Uri`
* `mime::Mime`
* `time::Tm`

Expand Down
49 changes: 43 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
//! * `cookie::Cookie`
//! * `hyper::header::ContentType`
//! * `hyper::header::Headers`
//! * `hyper::http::RawStatus`
//! * `hyper::method::Method`
//! * `hyper::RawStatus`
//! * `hyper::Method`
//! * `hyper::Uri`
//! * `mime::Mime`
//! * `time::Tm`
//!
Expand Down Expand Up @@ -62,15 +63,17 @@ extern crate time;

use cookie::Cookie;
use hyper::header::{ContentType, Headers};
use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::RawStatus;
use hyper::Method;
use hyper::Uri;
use mime::Mime;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_bytes::{ByteBuf, Bytes};
use serde::de::{self, MapAccess, SeqAccess, Visitor};
use serde::ser::{SerializeMap, SerializeSeq};
use std::cmp;
use std::fmt;
use std::str::FromStr;
use std::ops::{Deref, DerefMut};
use std::str;
use time::{Tm, strptime};
Expand Down Expand Up @@ -412,7 +415,7 @@ impl<'a> Serialize for Ser<'a, Headers> {
for header in self.v.iter() {
let name = header.name();
let value = self.v.get_raw(name).unwrap();
serializer.serialize_entry(name, &Value(value, self.pretty))?;
serializer.serialize_entry(name, &Value(&value.iter().map(|v| v.to_vec()).collect::<Vec<Vec<u8>>>(), self.pretty))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to allocate a new Vec<Vec<u8>>?

}
serializer.end()
}
Expand Down Expand Up @@ -466,7 +469,7 @@ impl<'de> Deserialize<'de> for De<Mime> {
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: de::Error,
{
v.parse::<Mime>().map(De::new).map_err(|()| {
v.parse::<Mime>().map(De::new).map_err(|_| {
E::custom("could not parse mime type")
})
}
Expand Down Expand Up @@ -534,3 +537,37 @@ impl<'a> Serialize for Ser<'a, Tm> {
serializer.serialize_str(&self.v.rfc3339().to_string())
}
}

impl<'de> Deserialize<'de> for De<Uri> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,
{
struct UriVisitor;

impl<'de> Visitor<'de> for UriVisitor {
type Value = De<Uri>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "an HTTP Uri value")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: de::Error,
{
Uri::from_str(v)
.map(De::new)
.map_err(|e| E::custom(format!("{:?}", e)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't use Debug.

}
}

deserializer.deserialize_string(UriVisitor)
}
}

impl<'a> Serialize for Ser<'a, Uri> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
{
serializer.serialize_str(&self.v.to_string())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allocates for nothing.

}
}
6 changes: 4 additions & 2 deletions tests/supported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ extern crate time;

use cookie::Cookie;
use hyper::header::{ContentType, Headers};
use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::RawStatus;
use hyper::Method;
use hyper::Uri;
use hyper_serde::{De, Ser, Serde};
use mime::Mime;
use serde::{Deserialize, Serialize};
Expand All @@ -30,4 +31,5 @@ fn supported() {
is_supported::<Mime>();
is_supported::<RawStatus>();
is_supported::<Tm>();
is_supported::<Uri>();
}
29 changes: 20 additions & 9 deletions tests/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
extern crate cookie;
extern crate hyper;
extern crate hyper_serde;
#[macro_use]
extern crate mime;
extern crate serde;
extern crate serde_test;
extern crate time;

use cookie::Cookie;
use hyper::header::{ContentType, Headers};
use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::RawStatus;
use hyper::Method;
use hyper::Uri;
use hyper_serde::{De, Ser, deserialize};
use serde::Deserialize;
use serde_test::{Deserializer, Token, assert_ser_tokens};
Expand All @@ -19,7 +18,7 @@ use time::Duration;

#[test]
fn test_content_type() {
let content_type = ContentType(mime!(Application / Json));
let content_type = ContentType("Application/Json".parse().unwrap());
let tokens = &[Token::Str("application/json")];

assert_ser_tokens(&Ser::new(&content_type), tokens);
Expand Down Expand Up @@ -58,10 +57,10 @@ fn test_headers_not_empty() {
use hyper::header::Host;

let mut headers = Headers::new();
headers.set(Host {
hostname: "baguette".to_owned(),
port: None,
});
headers.set(Host::new(
"baguette",
None
));

// In Hyper 0.9, Headers is internally a HashMap and thus testing this
// with multiple headers is non-deterministic.
Expand Down Expand Up @@ -121,6 +120,18 @@ fn test_tm() {
assert_de_tokens(&time, tokens);
}

#[test]
fn test_uri() {
use std::str::FromStr;

let uri_string = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
let uri = Uri::from_str(uri_string).unwrap();
let tokens = &[Token::Str(uri_string)];

assert_ser_tokens(&Ser::new(&uri), tokens);
assert_de_tokens(&uri, tokens);
}

pub fn assert_de_tokens<T>(value: &T, tokens: &[Token])
where T: Debug + PartialEq,
for<'de> De<T>: Deserialize<'de>,
Expand Down