tugraph/raw/
role_info.rs

1// Copyright 2023 antkiller
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 libc::c_char;
16use std::{collections::HashMap, ptr, slice};
17
18use crate::{ffi, types::AccessLevel};
19
20use super::ffi_util;
21
22raw_pod_rustlize!(
23    RawRoleInfo,
24    lgraph_api_role_info_t,
25    lgraph_api_role_info_destroy,
26);
27
28impl RawRoleInfo {
29    pub(crate) fn desc(&self) -> String {
30        unsafe {
31            let cstr = ffi::lgraph_api_role_info_get_desc(self.inner);
32            ffi_util::to_rust_string(cstr)
33        }
34    }
35
36    pub(crate) fn graph_access(&self) -> HashMap<String, AccessLevel> {
37        unsafe {
38            let mut cgraph_names: *mut *mut c_char = ptr::null_mut();
39            let mut caccess_levels: *mut i32 = ptr::null_mut();
40            let len = ffi::lgraph_api_role_info_get_graph_access(
41                self.inner,
42                &mut cgraph_names as *mut _,
43                &mut caccess_levels as *mut _,
44            );
45            let graph_names: Vec<_> = slice::from_raw_parts(cgraph_names, len)
46                .iter()
47                .map(|s| ffi_util::to_rust_string(*s))
48                .collect();
49            let access_levels: Vec<_> = slice::from_raw_parts(caccess_levels, len)
50                .iter()
51                .map(|l| AccessLevel::try_from(*l as u32).unwrap())
52                .collect();
53            graph_names
54                .into_iter()
55                .zip(access_levels.into_iter())
56                .collect()
57        }
58    }
59
60    pub(crate) fn disabled(&self) -> bool {
61        unsafe { ffi::lgraph_api_role_info_get_disabled(self.inner) }
62    }
63}