Skip to main content

dhttp_access/
lib.rs

1pub mod action;
2pub mod error;
3pub mod expr;
4pub mod matcher;
5pub mod pattern;
6
7#[macro_export]
8#[cfg(feature = "orm")]
9macro_rules! orm_new_type {
10    (@json $ty:ty) => {
11        const _: () = {
12            use std::any::type_name;
13
14            use sea_orm::sea_query::{ArrayType, ValueType, ValueTypeErr};
15            use sea_orm::{ColIdx, ColumnType, DbErr, QueryResult, TryGetError, TryGetable, Value};
16
17            impl ValueType for $ty {
18                #[inline]
19                fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
20                    let value = <serde_json::Value as ValueType>::try_from(v)?;
21                    serde_json::from_value(value).map_err(|_| ValueTypeErr)
22                }
23
24                #[inline]
25                fn type_name() -> String {
26                    String::from(type_name::<Self>())
27                }
28
29                #[inline]
30                fn array_type() -> ArrayType {
31                    ArrayType::Json
32                }
33
34                #[inline]
35                fn column_type() -> ColumnType {
36                    ColumnType::Json
37                }
38            }
39
40            impl TryGetable for $ty {
41                #[inline]
42                fn try_get_by<I: ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
43                    res.try_get_by::<serde_json::Value, I>(index)
44                        .and_then(|value| {
45                            serde_json::from_value::<Self>(value)
46                                .map_err(|e| DbErr::Type(e.to_string()))
47                        })
48                        .map_err(TryGetError::DbErr)
49                }
50            }
51
52            impl From<$ty> for Value {
53                fn from(value: $ty) -> Self {
54                    Self::from(serde_json::to_value(value).unwrap())
55                }
56            }
57        };
58    };
59}
60
61#[macro_export]
62#[cfg(not(feature = "orm"))]
63macro_rules! orm_new_type {
64    (@json $ty:ty) => {};
65}
66
67#[cfg(feature = "orm")]
68pub mod db;
69#[cfg(feature = "migration")]
70pub mod migration;