~lthms/typed-urls

ref: 1f203c30307c611503efa32cc92f1bf5ebf8a0eb typed-urls/src/lib.rs -rw-r--r-- 717 bytes
1f203c30Thomas Letan Derive Serialize implementation 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
use std::collections::HashMap;
use std::fmt::Display;

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

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)
    }
}