tibba_util/
response.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 axum::Json;
16use axum::http::header;
17use axum::response::{IntoResponse, Response};
18use serde::Serialize;
19use std::fmt::Write;
20use std::time::Duration;
21use tibba_error::Error;
22
23type Result<T> = std::result::Result<T, Error>;
24
25pub type JsonResult<T> = Result<Json<T>>;
26
27const S_MAX_AGE_LIMIT_SECS: u64 = 3600;
28
29pub struct CacheJson<T> {
30    pub duration: Duration,
31    pub data: T,
32}
33
34pub type CacheJsonResult<T> = Result<CacheJson<T>>;
35
36impl<T> From<(Duration, T)> for CacheJson<T> {
37    fn from(value: (Duration, T)) -> Self {
38        Self {
39            duration: value.0,
40            data: value.1,
41        }
42    }
43}
44
45impl<T> IntoResponse for CacheJson<T>
46where
47    T: Serialize,
48{
49    fn into_response(self) -> Response {
50        let secs = self.duration.as_secs();
51
52        // use `write!` macro to build Header string efficiently, avoid multiple memory allocations.
53        // pre-allocate a reasonable capacity to further improve performance.
54        let mut cache_control_value = String::with_capacity(64);
55
56        // `write!` macro can write formatted content directly into String, more efficient than `format!` and `push_str`.
57        // because writing to String will not fail.
58        let _ = write!(&mut cache_control_value, "public, max-age={secs}");
59
60        if secs > S_MAX_AGE_LIMIT_SECS {
61            let _ = write!(
62                &mut cache_control_value,
63                ", s-maxage={S_MAX_AGE_LIMIT_SECS}"
64            );
65        }
66
67        // finally wrap the data into `Json`.
68        (
69            [(header::CACHE_CONTROL, cache_control_value)],
70            Json(self.data),
71        )
72            .into_response()
73    }
74}