aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 06bf18069218728d18c93ee9d2b4e5bc2c915309 (plain)
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
39
40
41
42
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

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