rocket_community/serde/uuid.rs
1//! UUID path/query parameter and form value parsing support.
2//!
3//! # Enabling
4//!
5//! This module is only available when the `uuid` feature is enabled. Enable it
6//! in `Cargo.toml` as follows:
7//!
8//! ```toml
9//! [dependencies.rocket]
10//! package = "rocket_community"
11//! version = "0.6.0"
12//! features = ["uuid"]
13//! ```
14//!
15//! # Usage
16//!
17//! `Uuid` implements [`FromParam`] and [`FromFormField`] (i.e,
18//! [`FromForm`](crate::form::FromForm)), allowing UUID values to be accepted
19//! directly in paths, queries, and forms. You can use the `Uuid` type directly
20//! as a target of a dynamic parameter:
21//!
22//! ```rust
23//! # #[macro_use] extern crate rocket_community as rocket;
24//! use rocket::serde::uuid::Uuid;
25//!
26//! #[get("/users/<id>")]
27//! fn user(id: Uuid) -> String {
28//! format!("We found: {}", id)
29//! }
30//! ```
31//!
32//! You can also use the `Uuid` as a form value, including in query strings:
33//!
34//! ```rust
35//! # #[macro_use] extern crate rocket_community as rocket;
36//! use rocket::serde::uuid::Uuid;
37//!
38//! #[get("/user?<id>")]
39//! fn user(id: Uuid) -> String {
40//! format!("User ID: {}", id)
41//! }
42//! ```
43//!
44//! Additionally, `Uuid` implements `UriDisplay<P>` for all `P`. As such, route
45//! URIs including `Uuid`s can be generated in a type-safe manner:
46//!
47//! ```rust
48//! # #[macro_use] extern crate rocket_community as rocket;
49//! use rocket::serde::uuid::Uuid;
50//! use rocket::response::Redirect;
51//!
52//! #[get("/user/<id>")]
53//! fn user(id: Uuid) -> String {
54//! format!("User ID: {}", id)
55//! }
56//!
57//! #[get("/user?<id>")]
58//! fn old_user_path(id: Uuid) -> Redirect {
59//! # let _ = Redirect::to(uri!(user(&id)));
60//! # let _ = Redirect::to(uri!(old_user_path(id)));
61//! # let _ = Redirect::to(uri!(old_user_path(&id)));
62//! Redirect::to(uri!(user(id)))
63//! }
64//! ```
65//!
66//! # Extra Features
67//!
68//! The [`uuid`](https://docs.rs/uuid/1) crate exposes extra `v{n}` features
69//! for generating UUIDs which are not enabled by Rocket. To enable these
70//! features, depend on `uuid` directly. The extra functionality can be accessed
71//! via both `rocket::serde::uuid::Uuid` or the direct `uuid::Uuid`; the types
72//! are one and the same.
73//!
74//! ```toml
75//! [dependencies.uuid]
76//! version = "1"
77//! features = ["v1", "v4"]
78//! ```
79
80use crate::form::{self, FromFormField, ValueField};
81use crate::request::FromParam;
82
83/// Error returned on [`FromParam`] or [`FromFormField`] failures.
84///
85pub use uuid_::Error;
86
87pub use uuid_::{fmt, uuid, Builder, Bytes, Uuid, Variant, Version};
88
89impl<'a> FromParam<'a> for Uuid {
90 type Error = Error;
91
92 /// A value is successfully parsed if `param` is a properly formatted Uuid.
93 /// Otherwise, an error is returned.
94 #[inline(always)]
95 fn from_param(param: &'a str) -> Result<Uuid, Self::Error> {
96 param.parse()
97 }
98}
99
100impl<'v> FromFormField<'v> for Uuid {
101 #[inline]
102 fn from_value(field: ValueField<'v>) -> form::Result<'v, Self> {
103 Ok(field.value.parse().map_err(form::error::Error::custom)?)
104 }
105}
106
107#[cfg(test)]
108mod test {
109 use super::{FromParam, Uuid};
110
111 #[test]
112 fn test_from_param() {
113 let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2";
114 let uuid = Uuid::from_param(uuid_str).unwrap();
115 assert_eq!(uuid_str, uuid.to_string());
116 }
117
118 #[test]
119 fn test_from_param_invalid() {
120 let uuid_str = "c1aa1e3b-9614-4895-9ebd-705255fa5bc2p";
121 assert!(Uuid::from_param(uuid_str).is_err());
122 }
123}