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
use crate::{impl_deserialize, impl_serialize};

use crate::de::Error;

/// Describe the visibility of the table
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StAccess {
    /// Visible to all
    Public,
    /// Visible only to the owner
    Private,
}

impl StAccess {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Public => "public",
            Self::Private => "private",
        }
    }

    /// Select the appropriated [Self] for the name.
    ///
    /// A name that start with '_' like '_sample' is [Self::Private]
    pub fn for_name(of: &str) -> Self {
        if of.starts_with('_') {
            Self::Private
        } else {
            Self::Public
        }
    }
}

impl<'a> TryFrom<&'a str> for StAccess {
    type Error = &'a str;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Ok(match value {
            "public" => Self::Public,
            "private" => Self::Private,
            x => return Err(x),
        })
    }
}

impl_serialize!([] StAccess, (self, ser) => ser.serialize_str(self.as_str()));
impl_deserialize!([] StAccess, de => {
    let value = de.deserialize_str_slice()?;
    StAccess::try_from(value).map_err(|x| {
        Error::custom(format!(
            "DecodeError for StAccess: `{x}`. Expected `public` | 'private'"
        ))
    })
});

/// Describe is the table is a `system table` or not.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StTableType {
    /// Created by the system
    ///
    /// System tables are `StAccess::Public` by default
    System,
    /// Created by the User
    User,
}

impl StTableType {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::System => "system",
            Self::User => "user",
        }
    }
}

impl<'a> TryFrom<&'a str> for StTableType {
    type Error = &'a str;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Ok(match value {
            "system" => Self::System,
            "user" => Self::User,
            x => return Err(x),
        })
    }
}

impl_serialize!([] StTableType, (self, ser) => ser.serialize_str(self.as_str()));
impl_deserialize!([] StTableType, de => {
    let value = de.deserialize_str_slice()?;
    StTableType::try_from(value).map_err(|x| {
        Error::custom(format!(
            "DecodeError for StTableType: `{x}`. Expected 'system' | 'user'"
        ))
    })
});