mirror of https://github.com/rwf2/Rocket.git
Rename 'UUID' to 'Uuid' in 'rocket_contrib'.
This commit is contained in:
parent
4157c573e8
commit
105137a46d
|
@ -20,7 +20,7 @@
|
||||||
//! * [msgpack](struct.MsgPack.html)
|
//! * [msgpack](struct.MsgPack.html)
|
||||||
//! * [handlebars_templates](struct.Template.html)
|
//! * [handlebars_templates](struct.Template.html)
|
||||||
//! * [tera_templates](struct.Template.html)
|
//! * [tera_templates](struct.Template.html)
|
||||||
//! * [uuid](struct.UUID.html)
|
//! * [uuid](struct.Uuid.html)
|
||||||
//!
|
//!
|
||||||
//! The recommend way to include features from this crate via Cargo in your
|
//! The recommend way to include features from this crate via Cargo in your
|
||||||
//! project is by adding a `[dependencies.rocket_contrib]` section to your
|
//! project is by adding a `[dependencies.rocket_contrib]` section to your
|
||||||
|
@ -78,4 +78,4 @@ pub use templates::{Template, Engines};
|
||||||
mod uuid;
|
mod uuid;
|
||||||
|
|
||||||
#[cfg(feature = "uuid")]
|
#[cfg(feature = "uuid")]
|
||||||
pub use uuid::{UUID, UuidParseError};
|
pub use uuid::{Uuid, UuidParseError};
|
||||||
|
|
|
@ -9,7 +9,7 @@ use rocket::http::RawStr;
|
||||||
|
|
||||||
pub use self::uuid_ext::ParseError as UuidParseError;
|
pub use self::uuid_ext::ParseError as UuidParseError;
|
||||||
|
|
||||||
/// Implements `FromParam` and `FormFormValue` for accepting UUID values from
|
/// Implements `FromParam` and `FormFormValue` for accepting Uuid values from
|
||||||
/// the [uuid](https://github.com/rust-lang-nursery/uuid) crate.
|
/// the [uuid](https://github.com/rust-lang-nursery/uuid) crate.
|
||||||
///
|
///
|
||||||
/// # Usage
|
/// # Usage
|
||||||
|
@ -24,21 +24,21 @@ pub use self::uuid_ext::ParseError as UuidParseError;
|
||||||
/// features = ["uuid"]
|
/// features = ["uuid"]
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// You can use the `UUID` type directly as a target of a dynamic parameter:
|
/// You can use the `Uuid` type directly as a target of a dynamic parameter:
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// #[get("/users/<id>")]
|
/// #[get("/users/<id>")]
|
||||||
/// fn user(id: UUID) -> String {
|
/// fn user(id: Uuid) -> String {
|
||||||
/// format!("We found: {}", id)
|
/// format!("We found: {}", id)
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// You can also use the `UUID` as a form value, including in query strings:
|
/// You can also use the `Uuid` as a form value, including in query strings:
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// #[derive(FromForm)]
|
/// #[derive(FromForm)]
|
||||||
/// struct UserQuery {
|
/// struct UserQuery {
|
||||||
/// id: UUID
|
/// id: Uuid
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// #[post("/user?<user_query>")]
|
/// #[post("/user?<user_query>")]
|
||||||
|
@ -46,22 +46,20 @@ pub use self::uuid_ext::ParseError as UuidParseError;
|
||||||
/// format!("User ID: {}", user_query.id)
|
/// format!("User ID: {}", user_query.id)
|
||||||
/// }
|
/// }
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct UUID(uuid_ext::Uuid);
|
pub struct Uuid(uuid_ext::Uuid);
|
||||||
|
|
||||||
impl UUID {
|
impl Uuid {
|
||||||
/// Consumes the UUID wrapper returning the underlying `Uuid` type.
|
/// Consumes the Uuid wrapper returning the underlying `Uuid` type.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # extern crate rocket_contrib;
|
/// # extern crate rocket_contrib;
|
||||||
/// # extern crate uuid;
|
/// # extern crate uuid;
|
||||||
/// # use rocket_contrib::UUID;
|
|
||||||
/// # use std::str::FromStr;
|
/// # use std::str::FromStr;
|
||||||
/// # use uuid::Uuid;
|
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
/// let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
||||||
/// let real_uuid = Uuid::from_str(uuid_str).unwrap();
|
/// let real_uuid = uuid::Uuid::from_str(uuid_str).unwrap();
|
||||||
/// let my_inner_uuid = UUID::from_str(uuid_str).unwrap().into_inner();
|
/// let my_inner_uuid = rocket_contrib::Uuid::from_str(uuid_str).unwrap().into_inner();
|
||||||
/// assert_eq!(real_uuid, my_inner_uuid);
|
/// assert_eq!(real_uuid, my_inner_uuid);
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -71,45 +69,45 @@ impl UUID {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for UUID {
|
impl fmt::Display for Uuid {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FromParam<'a> for UUID {
|
impl<'a> FromParam<'a> for Uuid {
|
||||||
type Error = UuidParseError;
|
type Error = UuidParseError;
|
||||||
|
|
||||||
/// A value is successfully parsed if `param` is a properly formatted UUID.
|
/// A value is successfully parsed if `param` is a properly formatted Uuid.
|
||||||
/// Otherwise, a `UuidParseError` is returned.
|
/// Otherwise, a `UuidParseError` is returned.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_param(param: &'a RawStr) -> Result<UUID, Self::Error> {
|
fn from_param(param: &'a RawStr) -> Result<Uuid, Self::Error> {
|
||||||
param.parse()
|
param.parse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'v> FromFormValue<'v> for UUID {
|
impl<'v> FromFormValue<'v> for Uuid {
|
||||||
type Error = &'v RawStr;
|
type Error = &'v RawStr;
|
||||||
|
|
||||||
/// A value is successfully parsed if `form_value` is a properly formatted
|
/// A value is successfully parsed if `form_value` is a properly formatted
|
||||||
/// UUID. Otherwise, the raw form value is returned.
|
/// Uuid. Otherwise, the raw form value is returned.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_form_value(form_value: &'v RawStr) -> Result<UUID, &'v RawStr> {
|
fn from_form_value(form_value: &'v RawStr) -> Result<Uuid, &'v RawStr> {
|
||||||
form_value.parse().map_err(|_| form_value)
|
form_value.parse().map_err(|_| form_value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for UUID {
|
impl FromStr for Uuid {
|
||||||
type Err = UuidParseError;
|
type Err = UuidParseError;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_str(s: &str) -> Result<UUID, Self::Err> {
|
fn from_str(s: &str) -> Result<Uuid, Self::Err> {
|
||||||
Ok(UUID(try!(s.parse())))
|
Ok(Uuid(try!(s.parse())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for UUID {
|
impl Deref for Uuid {
|
||||||
type Target = uuid_ext::Uuid;
|
type Target = uuid_ext::Uuid;
|
||||||
|
|
||||||
fn deref<'a>(&'a self) -> &'a Self::Target {
|
fn deref<'a>(&'a self) -> &'a Self::Target {
|
||||||
|
@ -117,7 +115,7 @@ impl Deref for UUID {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<uuid_ext::Uuid> for UUID {
|
impl PartialEq<uuid_ext::Uuid> for Uuid {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn eq(&self, other: &uuid_ext::Uuid) -> bool {
|
fn eq(&self, other: &uuid_ext::Uuid) -> bool {
|
||||||
self.0.eq(other)
|
self.0.eq(other)
|
||||||
|
@ -127,28 +125,28 @@ impl PartialEq<uuid_ext::Uuid> for UUID {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::uuid_ext;
|
use super::uuid_ext;
|
||||||
use super::{UUID, UuidParseError};
|
use super::{Uuid, UuidParseError};
|
||||||
use super::FromParam;
|
use super::FromParam;
|
||||||
use super::FromStr;
|
use super::FromStr;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_str() {
|
fn test_from_str() {
|
||||||
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
||||||
let uuid_wrapper = UUID::from_str(uuid_str).unwrap();
|
let uuid_wrapper = Uuid::from_str(uuid_str).unwrap();
|
||||||
assert_eq!(uuid_str, uuid_wrapper.to_string())
|
assert_eq!(uuid_str, uuid_wrapper.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_param() {
|
fn test_from_param() {
|
||||||
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
||||||
let uuid_wrapper = UUID::from_param(uuid_str.into()).unwrap();
|
let uuid_wrapper = Uuid::from_param(uuid_str.into()).unwrap();
|
||||||
assert_eq!(uuid_str, uuid_wrapper.to_string())
|
assert_eq!(uuid_str, uuid_wrapper.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_inner() {
|
fn test_into_inner() {
|
||||||
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
||||||
let uuid_wrapper = UUID::from_param(uuid_str.into()).unwrap();
|
let uuid_wrapper = Uuid::from_param(uuid_str.into()).unwrap();
|
||||||
let real_uuid: uuid_ext::Uuid = uuid_str.parse().unwrap();
|
let real_uuid: uuid_ext::Uuid = uuid_str.parse().unwrap();
|
||||||
let inner_uuid: uuid_ext::Uuid = uuid_wrapper.into_inner();
|
let inner_uuid: uuid_ext::Uuid = uuid_wrapper.into_inner();
|
||||||
assert_eq!(real_uuid, inner_uuid)
|
assert_eq!(real_uuid, inner_uuid)
|
||||||
|
@ -157,7 +155,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_partial_eq() {
|
fn test_partial_eq() {
|
||||||
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
|
||||||
let uuid_wrapper = UUID::from_param(uuid_str.into()).unwrap();
|
let uuid_wrapper = Uuid::from_param(uuid_str.into()).unwrap();
|
||||||
let real_uuid: uuid_ext::Uuid = uuid_str.parse().unwrap();
|
let real_uuid: uuid_ext::Uuid = uuid_str.parse().unwrap();
|
||||||
assert_eq!(uuid_wrapper, real_uuid)
|
assert_eq!(uuid_wrapper, real_uuid)
|
||||||
}
|
}
|
||||||
|
@ -165,7 +163,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_param_invalid() {
|
fn test_from_param_invalid() {
|
||||||
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2p";
|
let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2p";
|
||||||
let uuid_result = UUID::from_param(uuid_str.into());
|
let uuid_result = Uuid::from_param(uuid_str.into());
|
||||||
assert_eq!(uuid_result, Err(UuidParseError::InvalidLength(37)));
|
assert_eq!(uuid_result, Err(UuidParseError::InvalidLength(37)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,7 @@ extern crate rocket;
|
||||||
extern crate rocket_contrib;
|
extern crate rocket_contrib;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use uuid::Uuid;
|
use rocket_contrib::Uuid;
|
||||||
use rocket_contrib::UUID;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -17,12 +16,12 @@ mod tests;
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
// A small people lookup table for the sake of this example. In a real
|
// A small people lookup table for the sake of this example. In a real
|
||||||
// application this could be a database lookup. Notice that we use the
|
// application this could be a database lookup. Notice that we use the
|
||||||
// uuid::Uuid type here and not the rocket_contrib::UUID type.
|
// uuid::Uuid type here and not the rocket_contrib::Uuid type.
|
||||||
static ref PEOPLE: HashMap<Uuid, &'static str> = {
|
static ref PEOPLE: HashMap<uuid::Uuid, &'static str> = {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
let lacy_id = Uuid::parse_str("7f205202-7ba1-4c39-b2fc-3e630722bf9f").unwrap();
|
let lacy_id = uuid::Uuid::parse_str("7f205202-7ba1-4c39-b2fc-3e630722bf9f").unwrap();
|
||||||
let bob_id = Uuid::parse_str("4da34121-bc7d-4fc1-aee6-bf8de0795333").unwrap();
|
let bob_id = uuid::Uuid::parse_str("4da34121-bc7d-4fc1-aee6-bf8de0795333").unwrap();
|
||||||
let george_id = Uuid::parse_str("ad962969-4e3d-4de7-ac4a-2d86d6d10839").unwrap();
|
let george_id = uuid::Uuid::parse_str("ad962969-4e3d-4de7-ac4a-2d86d6d10839").unwrap();
|
||||||
m.insert(lacy_id, "Lacy");
|
m.insert(lacy_id, "Lacy");
|
||||||
m.insert(bob_id, "Bob");
|
m.insert(bob_id, "Bob");
|
||||||
m.insert(george_id, "George");
|
m.insert(george_id, "George");
|
||||||
|
@ -31,9 +30,9 @@ lazy_static! {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/people/<id>")]
|
#[get("/people/<id>")]
|
||||||
fn people(id: UUID) -> Result<String, String> {
|
fn people(id: Uuid) -> Result<String, String> {
|
||||||
// Because UUID implements the Deref trait, we use Deref coercion to convert
|
// Because Uuid implements the Deref trait, we use Deref coercion to convert
|
||||||
// rocket_contrib::UUID to uuid::Uuid.
|
// rocket_contrib::Uuid to uuid::Uuid.
|
||||||
Ok(PEOPLE.get(&id)
|
Ok(PEOPLE.get(&id)
|
||||||
.map(|person| format!("We found: {}", person))
|
.map(|person| format!("We found: {}", person))
|
||||||
.ok_or(format!("Person not found for UUID: {}", id))?)
|
.ok_or(format!("Person not found for UUID: {}", id))?)
|
||||||
|
|
Loading…
Reference in New Issue