Rename external 'uuid' to 'extern_uuid'.

Use '_uuid' to refer to the crate internally.
This commit is contained in:
Sergio Benitez 2021-03-05 14:42:23 -08:00
parent 165f4f59b3
commit f0a6b9a25a
2 changed files with 24 additions and 22 deletions

View File

@ -14,7 +14,7 @@
//! features = ["uuid"] //! features = ["uuid"]
//! ``` //! ```
pub extern crate _uuid as uuid_crate; pub extern crate _uuid as extern_uuid;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
@ -25,9 +25,10 @@ use serde::{Deserialize, Serialize};
use rocket::request::FromParam; use rocket::request::FromParam;
use rocket::form::{self, FromFormField, ValueField}; use rocket::form::{self, FromFormField, ValueField};
type ParseError = <self::uuid_crate::Uuid as FromStr>::Err; /// UUID data and form guard: consume UUID values.
///
/// Implements [`FromParam`] and [`FromFormValue`] for accepting UUID values. /// `Uuuid` implements [`FromParam`] and [`FromForm`] for accepting UUID values
/// in paths, queries, and forms.
/// ///
/// # Usage /// # Usage
/// ///
@ -67,7 +68,7 @@ type ParseError = <self::uuid_crate::Uuid as FromStr>::Err;
/// } /// }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)]
#[serde(transparent)] #[serde(transparent)]
pub struct Uuid(uuid_crate::Uuid); pub struct Uuid(_uuid::Uuid);
impl Uuid { impl Uuid {
/// Consumes the Uuid wrapper, returning the underlying `Uuid` type. /// Consumes the Uuid wrapper, returning the underlying `Uuid` type.
@ -77,10 +78,10 @@ impl Uuid {
/// # extern crate rocket_contrib; /// # extern crate rocket_contrib;
/// # use std::str::FromStr; /// # use std::str::FromStr;
/// # fn main() { /// # fn main() {
/// use rocket_contrib::uuid::{uuid_crate, Uuid}; /// use rocket_contrib::uuid::{extern_uuid, Uuid};
/// ///
/// let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2"; /// let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
/// let real_uuid = uuid_crate::Uuid::from_str(uuid_str).unwrap(); /// let real_uuid = extern_uuid::Uuid::from_str(uuid_str).unwrap();
/// let my_inner_uuid = Uuid::from_str(uuid_str) /// let my_inner_uuid = Uuid::from_str(uuid_str)
/// .expect("valid UUID string") /// .expect("valid UUID string")
/// .into_inner(); /// .into_inner();
@ -89,7 +90,7 @@ impl Uuid {
/// # } /// # }
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn into_inner(self) -> uuid_crate::Uuid { pub fn into_inner(self) -> _uuid::Uuid {
self.0 self.0
} }
} }
@ -101,11 +102,12 @@ impl fmt::Display for Uuid {
} }
} }
impl<'a> FromParam<'a> for Uuid { impl<'a> FromParam<'a> for Uuid {
type Error = ParseError; type Error = _uuid::Error;
/// 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 `ParseError` is returned. /// Otherwise, an error is returned.
#[inline(always)] #[inline(always)]
fn from_param(param: &'a str) -> Result<Uuid, Self::Error> { fn from_param(param: &'a str) -> Result<Uuid, Self::Error> {
param.parse() param.parse()
@ -119,7 +121,7 @@ impl<'v> FromFormField<'v> for Uuid {
} }
impl FromStr for Uuid { impl FromStr for Uuid {
type Err = ParseError; type Err = _uuid::Error;
#[inline] #[inline]
fn from_str(s: &str) -> Result<Uuid, Self::Err> { fn from_str(s: &str) -> Result<Uuid, Self::Err> {
@ -128,23 +130,22 @@ impl FromStr for Uuid {
} }
impl Deref for Uuid { impl Deref for Uuid {
type Target = uuid_crate::Uuid; type Target = _uuid::Uuid;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl PartialEq<uuid_crate::Uuid> for Uuid { impl PartialEq<_uuid::Uuid> for Uuid {
#[inline(always)] #[inline(always)]
fn eq(&self, other: &uuid_crate::Uuid) -> bool { fn eq(&self, other: &_uuid::Uuid) -> bool {
self.0.eq(other) self.0.eq(other)
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::uuid_crate;
use super::Uuid; use super::Uuid;
use super::FromParam; use super::FromParam;
use super::FromStr; use super::FromStr;
@ -167,8 +168,8 @@ mod 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_crate::Uuid = uuid_str.parse().unwrap(); let real_uuid: _uuid::Uuid = uuid_str.parse().unwrap();
let inner_uuid: uuid_crate::Uuid = uuid_wrapper.into_inner(); let inner_uuid: _uuid::Uuid = uuid_wrapper.into_inner();
assert_eq!(real_uuid, inner_uuid) assert_eq!(real_uuid, inner_uuid)
} }
@ -176,7 +177,7 @@ mod 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_crate::Uuid = uuid_str.parse().unwrap(); let real_uuid: _uuid::Uuid = uuid_str.parse().unwrap();
assert_eq!(uuid_wrapper, real_uuid) assert_eq!(uuid_wrapper, real_uuid)
} }

View File

@ -4,14 +4,15 @@ use std::collections::HashMap;
use rocket::State; use rocket::State;
use rocket_contrib::uuid::Uuid; use rocket_contrib::uuid::Uuid;
use rocket_contrib::uuid::uuid_crate as uuid; use rocket_contrib::uuid::extern_uuid;
#[cfg(test)] mod tests; #[cfg(test)] mod tests;
// A small people mapping in managed state for the sake of this example. In a // A small people mapping in managed state for the sake of this example. In a
// real application this would be a database. Notice that we use the uuid::Uuid // real application this would be a database. Notice that we use the external
// type here and not the rocket_contrib::uuid::Uuid type. // Uuid type here and not the rocket_contrib::uuid::Uuid type. We do this purely
struct People(HashMap<uuid::Uuid, &'static str>); // for demonstrative purposes; in practice, we could use the contrib `Uuid`.
struct People(HashMap<extern_uuid::Uuid, &'static str>);
#[get("/people/<id>")] #[get("/people/<id>")]
fn people(id: Uuid, people: State<People>) -> Result<String, String> { fn people(id: Uuid, people: State<People>) -> Result<String, String> {