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