1use chrono::{DateTime, offset};
16use snafu::Snafu;
17use tibba_error::Error as BaseError;
18use time::macros::format_description;
19use time::{OffsetDateTime, PrimitiveDateTime};
20
21pub fn format_datetime(datetime: PrimitiveDateTime) -> String {
22 let ts = datetime.assume_utc().unix_timestamp();
23 if let Some(value) = DateTime::from_timestamp(ts, 0) {
24 value.with_timezone(&offset::Local).to_string()
25 } else {
26 String::new()
27 }
28}
29
30pub fn parse_primitive_datetime(s: &str) -> Result<PrimitiveDateTime> {
31 let fmt_t = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
32 let fmt_space = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
33 if let Ok(dt) = PrimitiveDateTime::parse(s, fmt_t) {
34 return Ok(dt);
35 }
36 if let Ok(dt) = PrimitiveDateTime::parse(s, fmt_space) {
37 return Ok(dt);
38 }
39 if let Ok(odt) = OffsetDateTime::parse(s, &time::format_description::well_known::Rfc3339) {
40 let utc = odt.to_offset(time::UtcOffset::UTC);
41 return Ok(PrimitiveDateTime::new(utc.date(), utc.time()));
42 }
43 Err(Error::InvalidDatetime {
44 value: s.to_string(),
45 })
46}
47
48type Result<T> = std::result::Result<T, Error>;
49
50#[derive(Debug, Snafu)]
51#[snafu(visibility(pub))]
52pub enum Error {
53 #[snafu(display("{source}"))]
54 Sqlx { source: sqlx::Error },
55 #[snafu(display("{source}"))]
56 Json { source: serde_json::Error },
57 #[snafu(display("Not supported function: {}", name))]
58 NotSupported { name: String },
59 #[snafu(display("Not found"))]
60 NotFound,
61 #[snafu(display("Invalid datetime: {value}"))]
62 InvalidDatetime { value: String },
63 #[snafu(display("Insufficient balance"))]
64 InsufficientBalance,
65}
66
67impl From<Error> for BaseError {
68 fn from(val: Error) -> Self {
69 let err = match val {
70 Error::Sqlx { source } => BaseError::new(source)
71 .with_sub_category("sqlx")
72 .with_exception(true),
73 Error::Json { source } => BaseError::new(source)
74 .with_sub_category("json")
75 .with_exception(true),
76 Error::NotSupported { name } => {
77 BaseError::new(format!("Not supported function: {name}"))
78 .with_sub_category("not_supported")
79 .with_exception(true)
80 }
81 Error::NotFound => BaseError::new("Not found")
82 .with_sub_category("not_found")
83 .with_exception(true),
84 Error::InvalidDatetime { value } => {
85 BaseError::new(format!("Invalid datetime: {value}"))
86 .with_sub_category("invalid_datetime")
87 }
88 Error::InsufficientBalance => BaseError::new("Insufficient balance")
89 .with_sub_category("insufficient_balance")
90 .with_status(402),
91 };
92 err.with_category("model")
93 }
94}
95
96mod configuration;
97mod model;
98mod schema;
99mod user;
100
101pub use configuration::*;
102pub use model::*;
103pub use schema::*;
104pub use user::*;