egui_kit/extract/path.rs
1//! [`Path`]: typed path parameters.
2
3mod de;
4
5use serde::de::DeserializeOwned;
6
7use super::FromRequest;
8use crate::error::{Rejection, RouteError};
9use crate::request::Request;
10use de::PathDeserializer;
11
12/// Typed path parameters, deserialized with serde.
13///
14/// `T` can be a single value (`Path<u32>`), a tuple (`Path<(u32, String)>`) or a struct
15/// whose field names match the parameter names. Optional parameters (`{?lang}`) need an
16/// `Option<_>`. A parameter that fails to parse results in a 404; use a
17/// [matcher](crate::Router::matcher) to try other routes instead.
18///
19/// In a layout's `load`, only the parameters of its router's prefix are available.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct Path<T>(pub T);
22
23impl<S, T: DeserializeOwned> FromRequest<S> for Path<T> {
24 fn from_request(req: &Request, _state: &S) -> Result<Self, Rejection> {
25 T::deserialize(PathDeserializer::new(req.params()))
26 .map(Path)
27 .map_err(|err| RouteError::not_found(format!("invalid path parameters: {err}")).into())
28 }
29}