casdoor_sdk_rust/sdk/
models.rs

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::fmt::{Debug, Display};

use serde::{de::DeserializeOwned, Deserialize, Serialize};

pub trait Model: Debug + Clone + DeserializeOwned + Serialize {
    /// Model identifier, used for splicing URLs.
    fn ident() -> &'static str;
    /// Models identifier, used for splicing URLs.
    fn plural_ident() -> &'static str;
    /// Indicate whether this model currently supports updating individual columns one by one.
    fn support_update_columns() -> bool;
    fn owner(&self) -> &str;
    fn name(&self) -> &str;
    fn id(&self) -> String {
        format!("{}/{}", self.owner(), self.name())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ModelModifyArgs<M> {
    pub action: ModelAction,
    pub model: M,
    pub columns: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ModelAddArgs<M> {
    pub model: M,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ModelUpdateArgs<M> {
    pub model: M,
    pub columns: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ModelDeleteArgs<M> {
    pub model: M,
}


#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum ModelAction {
    Add,
    Delete,
    Update,
}

impl Display for ModelAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ModelAction::Add => write!(f, "add"),
            ModelAction::Delete => write!(f, "delete"),
            ModelAction::Update => write!(f, "update"),
        }
    }
}


#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum ModelActionAffect {
    #[default]
    Affected,
    Unaffected,
}

impl Display for ModelActionAffect {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Affected => write!(f, "Affected"),
            Self::Unaffected => write!(f, "Unaffected"),
        }
    }
}

impl ModelActionAffect {
    pub fn is_affected(&self) -> bool {
        matches!(self, ModelActionAffect::Affected)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct QueryArgs {
    #[serde(rename = "pageSize", skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i32>,
    #[serde(rename = "p", skip_serializing_if = "Option::is_none")]
    pub page: Option<i32>,
    #[serde(rename = "field", skip_serializing_if = "Option::is_none")]
    pub field: Option<String>,
    #[serde(rename = "value", skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(rename = "sortField", skip_serializing_if = "Option::is_none")]
    pub sort_field: Option<String>,
    #[serde(rename = "sortOrder", skip_serializing_if = "Option::is_none")]
    pub sort_order: Option<String>,
}

pub(crate) trait IsQueryArgs: Serialize {}

impl IsQueryArgs for QueryArgs {}


#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryResult<M> {
    items: Vec<M>,
    total: i64,
}

impl<M> From<(Vec<M>, i64)> for QueryResult<M> {
    #[inline(always)]
    fn from(value: (Vec<M>, i64)) -> Self {
        Self {
            items: value.0,
            total: value.1,
        }
    }
}