resp_result/
lib.rs

1#![cfg_attr(feature = "nightly_try_v2", feature(try_trait_v2))]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![doc = include_str!("../Readme.md")]
4
5mod config;
6mod convert;
7mod expect_ext;
8mod extra_flag;
9mod owner_leak;
10mod resp_body;
11mod resp_error;
12mod resp_result;
13
14#[cfg(feature = "for-axum")]
15pub use self::resp_result::to_response::axum::axum_respond_part;
16use once_cell::sync::OnceCell;
17
18use config::InnerConfig;
19pub use config::{ConfigTrait, DefaultConfig, RespConfig, SerdeConfig, SignType, StatusSign};
20pub use convert::{
21    from_request::{FromRequestFamily, MapReject, ToInner},
22    resp_try, IntoRespResult, IntoRespResultWithErr,
23};
24pub use extra_flag::{
25    flag_wrap::FlagWrap,
26    flags::{ExtraFlag, ExtraFlags, HeaderType},
27};
28pub use resp_error::RespError;
29pub use resp_result::{Nil, RespResult};
30
31pub type FlagRespResult<T, E> = RespResult<FlagWrap<T>, E>;
32
33static RESP_RESULT_CONFIG: OnceCell<InnerConfig> = OnceCell::new();
34
35pub fn try_set_config<C: ConfigTrait>(cfg: &C) -> Result<(), SetRespResultConfigureError> {
36    let inner = InnerConfig::from_cfg(cfg);
37
38    #[cfg(feature = "trace")]
39    trace::event!(trace::Level::DEBUG, set_config = "On Going");
40    RESP_RESULT_CONFIG
41        .set(inner)
42        .map_err(|_| SetRespResultConfigureError)
43}
44
45/// set the [`RespResult`] config, will change the action on generate response body
46///
47/// ## Panic
48///
49/// the config can only been set once, multiple times set will cause panic
50pub fn set_config<C: ConfigTrait>(cfg: &C) {
51    match try_set_config(cfg) {
52        Ok(_) => {
53            #[cfg(feature = "trace")]
54            trace::event!(trace::Level::INFO, set_config = "Ready");
55        }
56        Err(err) => {
57            #[cfg(feature = "trace")]
58            trace::event!(trace::Level::ERROR, set_config = "Error", error = %err);
59            panic!("{}", err);
60        }
61    }
62}
63
64pub(crate) fn get_config() -> &'static InnerConfig {
65    RESP_RESULT_CONFIG.get_or_init(|| {
66        #[cfg(feature = "trace")]
67        trace::event!(
68            trace::Level::WARN,
69            set_config = "None",
70            action = "Using Default"
71        );
72        Default::default()
73    })
74}
75#[derive(Debug, thiserror::Error)]
76#[error("RespResult Configure has set")]
77pub struct SetRespResultConfigureError;