typed_urls/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::collections::HashMap;
6use std::fmt::Display;
7
8use serde_json::Value;
9
10#[cfg(feature = "actix-routes")]
11pub mod actix;
12#[cfg(feature = "tera-templates")]
13pub mod tera;
14
15pub trait TypedUrl: Sized {
16    type Route: Display;
17
18    fn routes() -> &'static [Self::Route];
19
20    fn from_route(
21        route : &Self::Route,
22        args : &HashMap<String, Value>,
23    ) -> Option<Self>;
24}
25
26pub trait UrlPathParam {
27    fn from_value(value : &Value) -> Option<Self>
28    where
29        Self : std::marker::Sized;
30}
31
32impl UrlPathParam for String {
33    fn from_value(value : &Value) -> Option<Self> {
34        value.as_str().map(|x| x.to_owned())
35    }
36}
37
38impl UrlPathParam for i32 {
39    fn from_value(value : &Value) -> Option<Self> {
40        value.as_i64().map(|x| x as i32)
41    }
42}