Skip to main content

tibba_util/
lib.rs

1// Copyright 2026 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 snafu::Snafu;
17use std::env;
18use std::sync::LazyLock;
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        // 单次 match:从 source 构造 BaseError 并打 sub_category,
46        // 与项目其他 snafu 模块(tibba-config / tibba-sql 等)保持一致
47        let err = match val {
48            Error::Zstd { source } => BaseError::new(source).with_sub_category("zstd"),
49            Error::Lz4Decompress { source } => {
50                BaseError::new(source).with_sub_category("lz4_decompress")
51            }
52            Error::InvalidHeaderName { source } => {
53                BaseError::new(source).with_sub_category("invalid_header_name")
54            }
55            Error::InvalidHeaderValue { source } => {
56                BaseError::new(source).with_sub_category("invalid_header_value")
57            }
58            Error::Axum { source } => BaseError::new(source).with_sub_category("axum"),
59            Error::Invalid { message } => BaseError::new(message).with_sub_category("invalid"),
60            Error::Deserialize { source } => {
61                BaseError::new(source).with_sub_category("deserialize")
62            }
63        };
64        err.with_category("util")
65    }
66}
67
68static RUST_ENV: LazyLock<String> =
69    LazyLock::new(|| env::var("RUST_ENV").unwrap_or_else(|_| "dev".to_string()));
70
71pub fn get_env() -> &'static str {
72    &RUST_ENV
73}
74
75/// Whether it is a development environment
76/// Used to determine whether it is a local development environment
77pub fn is_development() -> bool {
78    get_env() == "dev"
79}
80
81/// Whether it is a test environment
82pub fn is_test() -> bool {
83    get_env() == "test"
84}
85
86/// Whether it is a production environment
87pub fn is_production() -> bool {
88    get_env() == "production"
89}
90
91mod compression;
92mod datetime;
93mod http;
94mod request;
95mod response;
96mod string;
97mod uri;
98mod value;
99
100pub use compression::*;
101pub use datetime::*;
102pub use http::*;
103pub use request::*;
104pub use response::*;
105pub use string::*;
106pub use uri::*;
107pub use value::*;