actix_router/
resource_path.rs1use crate::Path;
2
3pub trait Resource {
10 type Path: ResourcePath;
12
13 fn resource_path(&mut self) -> &mut Path<Self::Path>;
15}
16
17pub trait ResourcePath {
18 fn path(&self) -> &str;
19}
20
21impl ResourcePath for String {
22 fn path(&self) -> &str {
23 self.as_str()
24 }
25}
26
27impl ResourcePath for &str {
28 fn path(&self) -> &str {
29 self
30 }
31}
32
33impl ResourcePath for bytestring::ByteString {
34 fn path(&self) -> &str {
35 self
36 }
37}
38
39#[cfg(feature = "http")]
40impl ResourcePath for http::Uri {
41 fn path(&self) -> &str {
42 self.path()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use bytestring::ByteString;
49
50 use super::*;
51
52 #[test]
53 fn resource_path_implementations_expose_their_path() {
54 let owned = "/owned".to_owned();
55 assert_eq!(<String as ResourcePath>::path(&owned), "/owned");
56
57 let borrowed = "/borrowed";
58 assert_eq!(<&str as ResourcePath>::path(&borrowed), "/borrowed");
59
60 let bytes = ByteString::from("/bytes");
61 assert_eq!(<ByteString as ResourcePath>::path(&bytes), "/bytes");
62 }
63
64 #[cfg(feature = "http")]
65 #[test]
66 fn uri_resource_path_ignores_queries() {
67 let uri = http::Uri::from_static("/resource?query=value");
68
69 assert_eq!(<http::Uri as ResourcePath>::path(&uri), "/resource");
70 }
71}