resp_result/config/
status_signed.rs1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::owner_leak::OwnerLeaker;
6
7#[derive(Debug, Clone)]
8pub struct StatusSign {
10 pub(super) field_name: Cow<'static, str>,
12 pub(super) ty: SignType,
14}
15
16impl StatusSign {
17 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)]
26pub enum SignType {
28 Bool,
32 BoolRevert,
36 Number { on_ok: u8, on_fail: u8 },
40 Str {
44 on_ok: Cow<'static, str>,
45 on_fail: Cow<'static, str>,
46 },
47}
48
49impl SignType {
50 pub const fn new_bool() -> Self {
52 SignType::Bool
53 }
54
55 pub const fn new_bool_rev() -> Self {
57 SignType::BoolRevert
58 }
59
60 pub const fn new_number(ok: u8, err: u8) -> Self {
62 Self::Number {
63 on_ok: ok,
64 on_fail: err,
65 }
66 }
67 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}