1use chrono::{DateTime, offset};
16use snafu::Snafu;
17use tibba_error::Error as BaseError;
18use time::macros::format_description;
19use time::{OffsetDateTime, PrimitiveDateTime};
20
21pub(crate) 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(crate) 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)]
51pub enum Error {
52 #[snafu(display("{source}"))]
53 Sqlx { source: sqlx::Error },
54 #[snafu(display("{source}"))]
55 Json { source: serde_json::Error },
56 #[snafu(display("Not supported function: {}", name))]
57 NotSupported { name: String },
58 #[snafu(display("Not found"))]
59 NotFound,
60 #[snafu(display("Invalid datetime: {value}"))]
61 InvalidDatetime { value: String },
62}
63
64impl From<Error> for BaseError {
65 fn from(val: Error) -> Self {
66 let err = match val {
67 Error::Sqlx { source } => BaseError::new(source)
68 .with_sub_category("sqlx")
69 .with_exception(true),
70 Error::Json { source } => BaseError::new(source)
71 .with_sub_category("json")
72 .with_exception(true),
73 Error::NotSupported { name } => {
74 BaseError::new(format!("Not supported function: {name}"))
75 .with_sub_category("not_supported")
76 .with_exception(true)
77 }
78 Error::NotFound => BaseError::new("Not found")
79 .with_sub_category("not_found")
80 .with_exception(true),
81 Error::InvalidDatetime { value } => {
82 BaseError::new(format!("Invalid datetime: {value}"))
83 .with_sub_category("invalid_datetime")
84 }
85 };
86 err.with_category("model")
87 }
88}
89
90mod configuration;
91mod detector_group;
92mod detector_group_user;
93mod file;
94mod http_detector;
95mod http_stat;
96mod model;
97mod schema;
98mod user;
99mod web_page_detector;
100
101pub use configuration::*;
102pub use detector_group::*;
103pub use detector_group_user::*;
104pub use file::*;
105pub use http_detector::*;
106pub use http_stat::*;
107pub use model::*;
108pub use schema::*;
109pub use user::*;
110pub use web_page_detector::*;