tibba_util/
lib.rs

1// Copyright 2025 Tree xie.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use lz4_flex::block::DecompressError;
16use once_cell::sync::Lazy;
17use snafu::Snafu;
18use std::env;
19use tibba_error::Error as BaseError;
20
21#[derive(Snafu, Debug)]
22pub enum Error {
23    #[snafu(display("{source}"))]
24    Zstd { source: std::io::Error },
25    #[snafu(display("{source}"))]
26    Lz4Decompress { source: DecompressError },
27    #[snafu(display("{source}"))]
28    InvalidHeaderName {
29        source: axum::http::header::InvalidHeaderName,
30    },
31    #[snafu(display("{source}"))]
32    InvalidHeaderValue {
33        source: axum::http::header::InvalidHeaderValue,
34    },
35    #[snafu(display("{source}"))]
36    Axum { source: axum::Error },
37    #[snafu(display("{message}"))]
38    Invalid { message: String },
39    #[snafu(display("{source}"))]
40    Deserialize { source: serde_urlencoded::de::Error },
41}
42
43impl From<Error> for BaseError {
44    fn from(val: Error) -> Self {
45        let sub_category = match val {
46            Error::Zstd { .. } => "zstd",
47            Error::Lz4Decompress { .. } => "lz4_decompress",
48            Error::InvalidHeaderName { .. } => "invalid_header_name",
49            Error::InvalidHeaderValue { .. } => "invalid_header_value",
50            Error::Axum { .. } => "axum",
51            Error::Invalid { .. } => "invalid",
52            Error::Deserialize { .. } => "deserialize",
53        };
54        // pass `val` to `new`, not the internal `source`.
55        BaseError::new(val)
56            .with_category("util")
57            .with_sub_category(sub_category)
58    }
59}
60
61static RUST_ENV: Lazy<String> =
62    Lazy::new(|| env::var("RUST_ENV").unwrap_or_else(|_| "dev".to_string()));
63
64pub fn get_env() -> &'static str {
65    &RUST_ENV
66}
67
68/// Whether it is a development environment
69/// Used to determine whether it is a local development environment
70pub fn is_development() -> bool {
71    get_env() == "dev"
72}
73
74/// Whether it is a test environment
75pub fn is_test() -> bool {
76    get_env() == "test"
77}
78
79/// Whether it is a production environment
80pub fn is_production() -> bool {
81    get_env() == "production"
82}
83
84mod compression;
85mod datetime;
86mod http;
87mod request;
88mod response;
89mod string;
90mod uri;
91mod value;
92
93pub use compression::*;
94pub use datetime::*;
95pub use http::*;
96pub use request::*;
97pub use response::*;
98pub use string::*;
99pub use uri::*;
100pub use value::*;