Skip to main content

casdoor_rust_sdk/
model.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19
20/// Model has the same definition as
21/// <https://github.com/casdoor/casdoor/blob/master/object/model.go>.
22#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase", default)]
24pub struct Model {
25    pub owner: String,
26    pub name: String,
27    pub created_time: String,
28    pub display_name: String,
29    pub description: String,
30
31    pub model_text: String,
32}
33
34impl Client {
35    /// Get the models of the client's organization.
36    pub async fn get_models(&self) -> Result<Vec<Model>> {
37        self.get_list("get-models", &[("owner", &self.config.organization_name)])
38            .await
39    }
40
41    /// Get one page of the models of the client's organization, together with the total
42    /// number of the models.
43    pub async fn get_pagination_models(
44        &self,
45        p: i32,
46        page_size: i32,
47        query: &[(&str, &str)],
48    ) -> Result<(Vec<Model>, i32)> {
49        self.get_pagination(
50            "get-models",
51            &self.config.organization_name,
52            p,
53            page_size,
54            query,
55        )
56        .await
57    }
58
59    /// Get one model by name.
60    pub async fn get_model(&self, name: &str) -> Result<Option<Model>> {
61        self.get_object("get-model", &[("id", &self.get_id(name))])
62            .await
63    }
64
65    /// Add a model.
66    pub async fn add_model(&self, model: &Model) -> Result<bool> {
67        Ok(self.modify_model("add-model", model, &[]).await?.1)
68    }
69
70    /// Update a model.
71    pub async fn update_model(&self, model: &Model) -> Result<bool> {
72        Ok(self.modify_model("update-model", model, &[]).await?.1)
73    }
74
75    /// Delete a model.
76    pub async fn delete_model(&self, model: &Model) -> Result<bool> {
77        Ok(self.modify_model("delete-model", model, &[]).await?.1)
78    }
79
80    async fn modify_model(
81        &self,
82        action: &str,
83        model: &Model,
84        columns: &[&str],
85    ) -> Result<(Response, bool)> {
86        let mut model = model.clone();
87        model.owner = get_owner(&model.owner, &self.config.organization_name);
88
89        let id = format!("{}/{}", model.owner, model.name);
90        self.modify_object(action, &id, &model, columns).await
91    }
92}