zino_model/user/
visibility.rs

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
use serde::{Deserialize, Serialize};
use sqlx::{Database, Decode, Type};
use strum::{AsRefStr, Display, EnumString, IntoStaticStr};
use zino_core::{BoxError, JsonValue};

/// User visibility.
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    Default,
    Serialize,
    Deserialize,
    AsRefStr,
    Display,
    EnumString,
    IntoStaticStr,
)]
#[non_exhaustive]
pub enum UserVisibility {
    /// It indicates that the user can only be visible in a group.
    /// This is the default value.
    #[default]
    Internal,
    /// It indicates that the user is visible to everyone.
    Public,
    /// It indicates that the user can be visible in a group and its subgroups.
    Protected,
    /// It indicates that the user can only be visible by itself.
    Private,
}

impl From<UserVisibility> for JsonValue {
    #[inline]
    fn from(value: UserVisibility) -> Self {
        value.as_ref().into()
    }
}

impl<DB> Type<DB> for UserVisibility
where
    DB: Database,
    String: Type<DB>,
{
    #[inline]
    fn type_info() -> <DB as Database>::TypeInfo {
        <String as Type<DB>>::type_info()
    }
}

impl<'r, DB> Decode<'r, DB> for UserVisibility
where
    DB: Database,
    &'r str: Decode<'r, DB>,
{
    #[inline]
    fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxError> {
        let value = <&'r str as Decode<'r, DB>>::decode(value)?;
        Ok(value.parse()?)
    }
}