resp_result/config/
status_signed.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::owner_leak::OwnerLeaker;
6
7#[derive(Debug, Clone)]
8/// the full info of status sign
9pub struct StatusSign {
10    /// the field name of the status sign
11    pub(super) field_name: Cow<'static, str>,
12    /// the type of the status sign
13    pub(super) ty: SignType,
14}
15
16impl StatusSign {
17    /// create a new [`StatusSign`] with provide `name` and `ty`
18    pub fn new(name: impl Into<Cow<'static, str>>, ty: SignType) -> Self {
19        Self {
20            field_name: name.into(),
21            ty,
22        }
23    }
24}
25#[derive(Debug, Clone)]
26/// the type of the sign
27pub enum SignType {
28    /// using bool sign the resp result
29    /// - `Success` => true
30    /// - `Err` => false
31    Bool,
32    /// using bool sign the resp result, but revert it
33    /// - `Success` => false
34    /// - `Err` => true
35    BoolRevert,
36    /// using the provide number as the sign
37    /// - `Success` => `on_ok`
38    /// - `Err` => `on_fail`
39    Number { on_ok: u8, on_fail: u8 },
40    /// using the provide string as the sign
41    /// - `Success` => `on_ok`
42    /// - `Err` => `on_fail`
43    Str {
44        on_ok: Cow<'static, str>,
45        on_fail: Cow<'static, str>,
46    },
47}
48
49impl SignType {
50    /// create a [`SignType`] using `Bool`
51    pub const fn new_bool() -> Self {
52        SignType::Bool
53    }
54
55    /// create a [`SignType`] using `BoolRevert`
56    pub const fn new_bool_rev() -> Self {
57        SignType::BoolRevert
58    }
59
60    /// create a [`SignType`] using `Number`
61    pub const fn new_number(ok: u8, err: u8) -> Self {
62        Self::Number {
63            on_ok: ok,
64            on_fail: err,
65        }
66    }
67    /// create a [`SignType`] using `Str`
68    pub fn new_str(ok: impl Into<Cow<'static, str>>, err: impl Into<Cow<'static, str>>) -> Self {
69        Self::Str {
70            on_ok: ok.into(),
71            on_fail: err.into(),
72        }
73    }
74}
75
76pub(crate) struct InnerStatusSign {
77    pub(crate) field: &'static str,
78    pub(crate) ok: StatusEnum,
79    pub(crate) err: StatusEnum,
80}
81
82impl From<StatusSign> for InnerStatusSign {
83    fn from(StatusSign { field_name, ty }: StatusSign) -> Self {
84        let (ok, err) = match ty {
85            SignType::Bool => (StatusEnum::Bool, StatusEnum::BoolRev),
86            SignType::BoolRevert => (StatusEnum::BoolRev, StatusEnum::Bool),
87            SignType::Number { on_ok, on_fail } => {
88                (StatusEnum::Number(on_ok), StatusEnum::Number(on_fail))
89            }
90            SignType::Str { on_ok, on_fail } => (StatusEnum::Str(on_ok), StatusEnum::Str(on_fail)),
91        };
92
93        Self {
94            field: field_name.leak(),
95            ok,
96            err,
97        }
98    }
99}
100
101pub(crate) enum StatusEnum {
102    Bool,
103    BoolRev,
104    Number(u8),
105    Str(Cow<'static, str>),
106}
107
108impl Serialize for StatusEnum {
109    #[inline]
110    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
111    where
112        S: serde::Serializer,
113    {
114        match self {
115            StatusEnum::Bool => serializer.serialize_bool(true),
116            StatusEnum::BoolRev => serializer.serialize_bool(false),
117            StatusEnum::Number(num) => serializer.serialize_u8(*num),
118            StatusEnum::Str(s) => serializer.serialize_str(s),
119        }
120    }
121}