Skip to main content

lamprey_common/v1/routes/
path_param.rs

1use crate::v1::types::{
2    Id, InviteCode, RoomFeature, document::DocumentRevisionRef, misc::InviteCodeReq,
3    reaction::ReactionKeyParam, room_template::RoomTemplateCode,
4};
5use std::str::FromStr;
6
7/// Trait for types that can be parsed from a path parameter string
8pub trait PathParam: Sized {
9    fn from_path_param(s: &str) -> Result<Self, PathParamError>;
10}
11
12// TODO: update type
13// pub trait PathParam: std::str::FromStr<Err = PathParamError>;
14
15/// Error type for path parameter extraction
16#[derive(Debug)]
17pub struct PathParamError(pub String);
18
19impl From<PathParamError> for http::Response<bytes::Bytes> {
20    fn from(err: PathParamError) -> Self {
21        http::Response::builder()
22            .status(http::StatusCode::BAD_REQUEST)
23            .body(bytes::Bytes::from(err.0))
24            .unwrap()
25    }
26}
27
28impl PathParam for String {
29    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
30        Ok(s.to_string())
31    }
32}
33
34impl PathParam for i64 {
35    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
36        s.parse()
37            .map_err(|_| PathParamError(format!("invalid i64: {}", s)))
38    }
39}
40
41impl PathParam for i32 {
42    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
43        s.parse()
44            .map_err(|_| PathParamError(format!("invalid i32: {}", s)))
45    }
46}
47
48impl PathParam for u64 {
49    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
50        s.parse()
51            .map_err(|_| PathParamError(format!("invalid u64: {}", s)))
52    }
53}
54
55impl PathParam for u32 {
56    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
57        s.parse()
58            .map_err(|_| PathParamError(format!("invalid u32: {}", s)))
59    }
60}
61
62impl<M: crate::v1::types::ids::Marker> PathParam for Id<M> {
63    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
64        s.parse()
65            .map_err(|_| PathParamError(format!("invalid id: {}", s)))
66    }
67}
68
69impl PathParam for RoomTemplateCode {
70    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
71        Ok(RoomTemplateCode(s.to_string()))
72    }
73}
74
75impl PathParam for InviteCode {
76    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
77        Ok(InviteCode(s.to_string()))
78    }
79}
80
81impl PathParam for InviteCodeReq {
82    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
83        InviteCodeReq::from_str(s).map_err(PathParamError)
84    }
85}
86
87impl PathParam for uuid::Uuid {
88    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
89        s.parse()
90            .map_err(|_| PathParamError(format!("invalid uuid: {}", s)))
91    }
92}
93
94impl PathParam for ReactionKeyParam {
95    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
96        s.parse::<ReactionKeyParam>()
97            .map_err(|_| PathParamError(format!("invalid reaction key: {}", s)))
98    }
99}
100
101impl PathParam for DocumentRevisionRef {
102    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
103        s.parse()
104            .map_err(|e: String| PathParamError(format!("invalid document revision id: {}", e)))
105    }
106}
107
108impl PathParam for RoomFeature {
109    fn from_path_param(s: &str) -> Result<Self, PathParamError> {
110        s.parse()
111            .map_err(|e| PathParamError(format!("invalid room feature: {e}")))
112    }
113}