~lthms/typed-urls

ref: bb0b25803aea783924d68628c7b24130dcab34da typed-urls/src/lib.rs -rw-r--r-- 815 bytes
bb0b2580Thomas Letan Turn typed-urls-tera into a feature and introduce the actix feature 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::collections::HashMap;
use std::fmt::Display;

use serde_json::Value;
pub use typed_urls_derive::TypedUrl;

#[cfg(feature = "tera-templates")]
pub mod tera;
#[cfg(feature = "actix-routes")]
pub mod actix;

pub trait TypedUrl: Sized {
    type Route: Display;

    fn routes() -> &'static [Self::Route];
    fn from_route(
        route : &Self::Route,
        args : &HashMap<String, Value>,
    ) -> Option<Self>;
}

pub trait UrlPathParam {
    fn from_value(value : &Value) -> Option<Self>
    where
        Self : std::marker::Sized;
}

impl UrlPathParam for String {
    fn from_value(value : &Value) -> Option<Self> {
        value.as_str().map(|x| x.to_owned())
    }
}

impl UrlPathParam for i32 {
    fn from_value(value : &Value) -> Option<Self> {
        value.as_i64().map(|x| x as i32)
    }
}