toggl-portal/src/utils.rs

68 lines
1.7 KiB
Rust

use crate::entity::time_entry;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use chrono::{NaiveDate, NaiveTime};
use migration::{Condition, IntoCondition};
use sea_orm::ColumnTrait;
use tokio::signal;
#[derive(Debug)]
pub struct AppError(anyhow::Error);
// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
// `Result<_, AppError>`. That way you don't need to do that manually.
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
tracing::error!("{:?}", self);
(
StatusCode::INTERNAL_SERVER_ERROR,
"An internal error occurred",
)
.into_response()
}
}
pub type Result<T, E = AppError> = std::result::Result<T, E>;
pub async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}
pub fn day_exclusivity_condition(start: NaiveDate, end: NaiveDate) -> Condition {
time_entry::Column::Start
.between(
start.and_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()),
end.and_time(NaiveTime::from_hms_opt(23, 59, 59).unwrap()),
)
.into_condition()
}