diff --git a/ohkami/Cargo.toml b/ohkami/Cargo.toml index 5b03ca5a8..42f760ba5 100644 --- a/ohkami/Cargo.toml +++ b/ohkami/Cargo.toml @@ -39,6 +39,7 @@ lambda_runtime = { version = "0.14", optional = true } base64 = { version = "0.22" } hmac = { version = "0.12", default-features = false } sha2 = { version = "0.10", default-features = false } +uuid = { version = "1.17" } # optional mime_guess = { version = "2.0", optional = true } diff --git a/ohkami/src/handle/param.rs b/ohkami/src/handle/param.rs index b246d7a23..c48408d82 100644 --- a/ohkami/src/handle/param.rs +++ b/ohkami/src/handle/param.rs @@ -134,8 +134,8 @@ pub struct Path(pub T); /// is required to be impl. /// /// ### required -/// - `type Errpr` -/// - `fn from_param` +/// - `type Error: IntoResponse` +/// - `fn from_param(param: Cow<'p, str>) -> Result` pub trait FromParam<'p>: bound::Schema + Sized { /// If this extraction never fails, `std::convert::Infallible` is recomended. type Error: IntoResponse; @@ -245,6 +245,21 @@ const _: () = { }; } signed_integers! { i8, i16, i32, i64, isize } + + impl<'p> FromParam<'p> for uuid::Uuid { + type Error = Response; + + fn from_param(param: Cow<'p, str>) -> Result { + uuid::Uuid::try_parse(¶m).map_err(|_| { + #[cfg(debug_assertions)] { + crate::WARNING!( + "Failed to parse UUID from path param `{param}`", + ); + } + Response::BadRequest().with_text("unexpected path") + }) + } + } }; impl<'req, P: FromParam<'req>> FromRequest<'req> for Path

{ diff --git a/ohkami_openapi/Cargo.toml b/ohkami_openapi/Cargo.toml index 21a37603f..faefd1347 100644 --- a/ohkami_openapi/Cargo.toml +++ b/ohkami_openapi/Cargo.toml @@ -16,5 +16,8 @@ license = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } +# for built-in utility impl of `Schema` trait +uuid = { version = "1.17" } + [features] no-release-serialize = [] \ No newline at end of file diff --git a/ohkami_openapi/src/lib.rs b/ohkami_openapi/src/lib.rs index 86ca59218..0bcbe6eb2 100644 --- a/ohkami_openapi/src/lib.rs +++ b/ohkami_openapi/src/lib.rs @@ -137,6 +137,12 @@ const _: () = { number().format("double") } } + + impl Schema for uuid::Uuid { + fn schema() -> impl Into { + string().format("uuid") + } + } impl Schema for Vec { fn schema() -> impl Into {