resp_result/config/
serde.rs1use 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
19pub trait SerdeConfig {
21 fn body_name(&self) -> Cow<'static, str> {
27 BODY.into()
28 }
29 fn err_msg_name(&self) -> Cow<'static, str> {
35 ERROR_MESSAGE.into()
36 }
37
38 fn fixed_field(&self) -> bool {
44 true
45 }
46
47 fn signed_status(&self) -> Option<StatusSign> {
56 Some(SIGNED_STATUS.clone())
57 }
58
59 #[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 if cfg.signed_status.is_some() {
117 ok_size += 1;
118 err_size += 1;
119 }
120 #[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}