1use snafu::Snafu;
16use std::env;
17use std::sync::LazyLock;
18use tibba_error::Error as BaseError;
19
20#[derive(Snafu, Debug)]
24pub enum Error {
25 #[cfg(feature = "compression")]
26 #[snafu(display("{source}"))]
27 Zstd { source: std::io::Error },
28 #[cfg(feature = "compression")]
29 #[snafu(display("{source}"))]
30 Lz4Decompress {
31 source: lz4_flex::block::DecompressError,
32 },
33 #[cfg(feature = "compression")]
35 #[snafu(display("decompressed size {size} exceeds limit {limit}"))]
36 DecompressTooLarge { size: usize, limit: usize },
37 #[cfg(feature = "http")]
38 #[snafu(display("{source}"))]
39 InvalidHeaderName {
40 source: axum::http::header::InvalidHeaderName,
41 },
42 #[cfg(feature = "http")]
43 #[snafu(display("{source}"))]
44 InvalidHeaderValue {
45 source: axum::http::header::InvalidHeaderValue,
46 },
47 #[snafu(display("{message}"))]
48 Invalid { message: String },
49 #[snafu(display("{source}"))]
50 Deserialize { source: serde_urlencoded::de::Error },
51}
52
53impl From<Error> for BaseError {
54 fn from(val: Error) -> Self {
55 let err = match val {
58 #[cfg(feature = "compression")]
59 Error::Zstd { source } => BaseError::new(source).with_sub_category("zstd"),
60 #[cfg(feature = "compression")]
61 Error::Lz4Decompress { source } => {
62 BaseError::new(source).with_sub_category("lz4_decompress")
63 }
64 #[cfg(feature = "compression")]
66 Error::DecompressTooLarge { size, limit } => {
67 BaseError::new(format!("decompressed size {size} exceeds limit {limit}"))
68 .with_sub_category("decompress_too_large")
69 .with_exception(true)
70 }
71 #[cfg(feature = "http")]
72 Error::InvalidHeaderName { source } => {
73 BaseError::new(source).with_sub_category("invalid_header_name")
74 }
75 #[cfg(feature = "http")]
76 Error::InvalidHeaderValue { source } => {
77 BaseError::new(source).with_sub_category("invalid_header_value")
78 }
79 Error::Invalid { message } => BaseError::new(message).with_sub_category("invalid"),
80 Error::Deserialize { source } => {
81 BaseError::new(source).with_sub_category("deserialize")
82 }
83 };
84 err.with_category("util")
85 }
86}
87
88pub const DEFAULT_RUST_ENV: &str = "production";
103
104static RUST_ENV: LazyLock<String> =
105 LazyLock::new(|| env::var("RUST_ENV").unwrap_or_else(|_| DEFAULT_RUST_ENV.to_string()));
106
107pub fn get_env() -> &'static str {
112 &RUST_ENV
113}
114
115pub fn is_development() -> bool {
117 get_env() == "dev"
118}
119
120pub fn is_test() -> bool {
122 get_env() == "test"
123}
124
125pub fn is_production() -> bool {
132 !is_development() && !is_test()
133}
134
135#[cfg(feature = "compression")]
136mod compression;
137mod datetime;
138#[cfg(feature = "http")]
139mod http;
140mod request;
141mod response;
142mod string;
143mod uri;
144mod validate;
145mod value;
146
147#[cfg(feature = "compression")]
148pub use compression::*;
149pub use datetime::*;
150#[cfg(feature = "http")]
151pub use http::*;
152pub use request::*;
153pub use response::*;
154pub use string::*;
155pub use uri::*;
156pub use validate::*;
157pub use value::*;