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
use cyfs_base::*;

use std::str::FromStr;


#[derive(Debug, Eq, PartialEq)]
pub enum MetaAction {
    GlobalStateAddAccess,
    GlobalStateRemoveAccess,
    GlobalStateClearAccess,

    GlobalStateAddLink,
    GlobalStateRemoveLink,
    GlobalStateClearLink,

    GlobalStateAddObjectMeta,
    GlobalStateRemoveObjectMeta,
    GlobalStateClearObjectMeta,

    GlobalStateAddPathConfig,
    GlobalStateRemovePathConfig,
    GlobalStateClearPathConfig,
}

impl ToString for MetaAction {
    fn to_string(&self) -> String {
        (match *self {
            Self::GlobalStateAddAccess => "global-state-add-access",
            Self::GlobalStateRemoveAccess => "global-state-remove-access",
            Self::GlobalStateClearAccess => "global-state-clear-access",

            Self::GlobalStateAddLink => "global-state-add-link",
            Self::GlobalStateRemoveLink => "global-state-remove-link",
            Self::GlobalStateClearLink => "global-state-clear-link",

            Self::GlobalStateAddObjectMeta => "global-state-add-object-meta",
            Self::GlobalStateRemoveObjectMeta => "global-state-remove-object-meta",
            Self::GlobalStateClearObjectMeta => "global-state-clear-object-meta",

            Self::GlobalStateAddPathConfig => "global-state-add-path-config",
            Self::GlobalStateRemovePathConfig => "global-state-remove-path-config",
            Self::GlobalStateClearPathConfig => "global-state-clear-path-config",
        })
        .to_owned()
    }
}

impl FromStr for MetaAction {
    type Err = BuckyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let ret = match value {
            "global-state-add-access" => Self::GlobalStateAddAccess,
            "global-state-remove-access" => Self::GlobalStateRemoveAccess,
            "global-state-clear-access" => Self::GlobalStateClearAccess,

            "global-state-add-link" => Self::GlobalStateAddLink,
            "global-state-remove-link" => Self::GlobalStateRemoveLink,
            "global-state-clear-link" => Self::GlobalStateClearLink,

            "global-state-add-object-meta" => Self::GlobalStateAddObjectMeta,
            "global-state-remove-object-meta" => Self::GlobalStateRemoveObjectMeta,
            "global-state-clear-object-meta" => Self::GlobalStateClearObjectMeta,

            "global-state-add-path-config" => Self::GlobalStateAddPathConfig,
            "global-state-remove-path-config" => Self::GlobalStateRemovePathConfig,
            "global-state-clear-path-config" => Self::GlobalStateClearPathConfig,
            
            v @ _ => {
                let msg = format!("unknown meta action: {}", v);
                error!("{}", msg);

                return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
            }
        };

        Ok(ret)
    }
}