cyfs_core/app/
system.rs

1use super::dec_app::*;
2use cyfs_base::ObjectId;
3
4
5pub struct SystemDecApp;
6
7impl SystemDecApp {
8    pub fn gen_system_dec_id() -> DecAppId {
9        let owner = cyfs_base::ObjectId::default();
10        DecApp::generate_id(owner, "cyfs-system-service")
11            .try_into()
12            .unwrap()
13    }
14
15    pub fn gen_anonymous_dec_id() -> DecAppId {
16        let owner = cyfs_base::ObjectId::default();
17        DecApp::generate_id(owner, "cyfs-anonymous-service")
18            .try_into()
19            .unwrap()
20    }
21}
22
23
24use once_cell::sync::OnceCell;
25use std::borrow::Cow;
26
27pub(crate) static SYSTEM_DEC_APP: OnceCell<ObjectId> = OnceCell::new();
28pub(crate) static ANONYMOUS_DEC_APP: OnceCell<ObjectId> = OnceCell::new();
29
30// get the default dec_app id for all the system service and system core
31pub fn get_system_dec_app() -> &'static ObjectId {
32    SYSTEM_DEC_APP.get_or_init(|| {
33        let dec_id = SystemDecApp::gen_system_dec_id().into();
34        info!("init system dec app id: {}", dec_id);
35        dec_id
36    })
37}
38
39pub fn is_system_dec_app(dec_id: &Option<ObjectId>) -> bool {
40    match dec_id {
41        Some(id) => {
42            id == get_system_dec_app()
43        }
44        None => {
45            true
46        }
47    }
48}
49
50
51// get the default dec_app id for all the unknown service and incoming reqeust
52pub fn get_anonymous_dec_app() -> &'static ObjectId {
53    ANONYMOUS_DEC_APP.get_or_init(|| {
54        let dec_id = SystemDecApp::gen_anonymous_dec_id().into();
55        info!("init anonymous dec app id: {}", dec_id);
56        dec_id
57    })
58}
59
60pub fn is_anonymous_dec_app(dec_id: &Option<ObjectId>) -> bool {
61    match dec_id {
62        Some(id) => {
63            id == get_anonymous_dec_app()
64        }
65        None => {
66            true
67        }
68    }
69}
70
71pub fn dec_id_to_string(dec_id: &ObjectId) -> Cow<str> {
72    if dec_id == get_system_dec_app() {
73        Cow::Borrowed("system")
74    } else if dec_id == get_anonymous_dec_app() {
75        Cow::Borrowed("anonymous")
76    } else {
77        Cow::Owned(dec_id.to_string())
78    }
79}
80
81#[test]
82fn test() {
83    let id = get_system_dec_app().to_owned();
84    assert_eq!(
85        id.to_string(),
86        "9tGpLNncauC9kGhZ7GsztFvVegaKwBXoSDjkxGDHqrn6"
87    );
88    println!("{}", id);
89}