resp_result/config/
serde.rs

1use std::borrow::Cow;
2
3use crate::owner_leak::OwnerLeaker;
4
5use super::{
6    status_signed::{InnerStatusSign, SignType},
7    StatusSign,
8};
9
10static SIGNED_STATUS: StatusSign = StatusSign {
11    field_name: Cow::Borrowed("is-ok"),
12    ty: SignType::Bool,
13};
14#[cfg(feature = "extra-error")]
15static EXTRA_ERR_MESSAGE: &str = "extra-error-message";
16static ERROR_MESSAGE: &str = "error-message";
17static BODY: &str = "body";
18
19/// the config information of serialize
20pub trait SerdeConfig {
21    /// the name of body field, the field will be available when the [`RespResult`](crate::RespResult)
22    /// is `Success(_)`
23    ///
24    /// ## Default
25    /// the default field name is `body`
26    fn body_name(&self) -> Cow<'static, str> {
27        BODY.into()
28    }
29    /// the name of error_message field, this field will be available when [`RespResult`](crate::RespResult)
30    /// is `Err(_)`
31    ///
32    /// ## Default
33    /// the default field name is `error-message`
34    fn err_msg_name(&self) -> Cow<'static, str> {
35        ERROR_MESSAGE.into()
36    }
37
38    /// fixed field size,
39    /// - if return `true`, will also serialize not available field with `null`
40    ///
41    /// ## Default
42    /// the default value is true
43    fn fixed_field(&self) -> bool {
44        true
45    }
46
47    /// sign the status of response
48    /// - Some(_) **enable** this sign
49    /// - None **disable** this sign
50    ///
51    /// ## Default
52    /// default enable this sign using `bool`
53    /// - true -> `Success`
54    /// - false -> `Err`
55    fn signed_status(&self) -> Option<StatusSign> {
56        Some(SIGNED_STATUS.clone())
57    }
58
59    /// extra error message
60    /// - Some(_) **enable** extra error message
61    ///- None **disable** extra error message
62    /// extra-error
63    ///
64    /// ## Default
65    /// default enable with field name `extra-msg`
66    #[cfg(feature = "extra-error")]
67    fn extra_message(&self) -> Option<Cow<'static, str>> {
68        Some(EXTRA_ERR_MESSAGE.into())
69    }
70}
71
72pub(crate) struct InnerSerdeConfig {
73    pub(crate) body_name: &'static str,
74    pub(crate) err_msg_name: &'static str,
75    pub(crate) full_field: bool,
76    pub(crate) signed_status: Option<InnerStatusSign>,
77    #[cfg(feature = "extra-error")]
78    pub(crate) extra_code: Option<&'static str>,
79    pub(crate) field_size: FieldSize,
80}
81
82impl InnerSerdeConfig {
83    pub(crate) fn into_inner<C: SerdeConfig>(cfg: &C) -> Self {
84        let mut s = Self {
85            body_name: cfg.body_name().leak(),
86            err_msg_name: cfg.err_msg_name().leak(),
87            full_field: cfg.fixed_field(),
88            signed_status: cfg.signed_status().map(Into::into),
89            #[cfg(feature = "extra-error")]
90            extra_code: cfg.extra_message().leak(),
91            field_size: Default::default(),
92        };
93
94        let f = FieldSize::new(&s);
95        s.field_size = f;
96
97        s
98    }
99
100    pub(crate) fn get_field_size(&self) -> (usize, usize) {
101        let FieldSize { ok_size, err_size } = self.field_size;
102        (ok_size, err_size)
103    }
104}
105
106#[derive(Debug, Default)]
107pub(crate) struct FieldSize {
108    ok_size: usize,
109    err_size: usize,
110}
111
112impl FieldSize {
113    pub(crate) fn new(cfg: &InnerSerdeConfig) -> Self {
114        let (mut ok_size, mut err_size) = (1, 1);
115        // 简易状态标记
116        if cfg.signed_status.is_some() {
117            ok_size += 1;
118            err_size += 1;
119        }
120        //额外的异常码
121        #[cfg(feature = "extra-error")]
122        if cfg.extra_code.is_some() {
123            if cfg.full_field {
124                ok_size += 1;
125            }
126            err_size += 1;
127        }
128
129        if cfg.full_field {
130            ok_size += 1;
131            err_size += 1;
132        }
133        Self { ok_size, err_size }
134    }
135}