use std::fmt::Display; use std::str::FromStr; use chrono::{DateTime, FixedOffset, NaiveDate, Utc}; use serde::{Deserialize, Deserializer, Serialize}; use crate::clock; #[derive(Debug, Clone, Serialize, sqlx::FromRow)] pub struct Client { pub id: i64, pub name: String, pub color: String, pub archived: bool, pub created_at: DateTime, } #[derive(Debug, Clone, Serialize, sqlx::FromRow)] pub struct Project { pub id: i64, pub client_id: i64, pub client_name: String, pub name: String, /// Own color; `None` means the client's color is used. pub color: Option, /// `color`, or the client's color. pub effective_color: String, /// Link to the source repository. pub repo_url: Option, pub archived: bool, pub created_at: DateTime, } #[derive(Debug, Clone, Serialize, sqlx::FromRow)] pub struct Entry { pub id: i64, pub project_id: i64, pub project_name: String, pub client_id: i64, pub client_name: String, /// The project's effective color. pub color: String, pub client_color: String, /// The project's repository link. pub repo_url: Option, pub started_at: DateTime, /// `None` while the timer is running. pub ended_at: Option>, pub note: String, /// Elapsed seconds (up to now for a running entry). pub seconds: i64, /// True when another entry covers part of the same time span. pub overlaps: bool, } impl Entry { pub fn running(&self) -> bool { self.ended_at.is_none() } pub fn duration(&self) -> String { clock::hhmm(self.seconds) } pub fn day(&self) -> String { clock::fmt_day(clock::local_date(&self.started_at)) } pub fn start_time(&self) -> String { clock::fmt_time(&self.started_at) } pub fn end_time(&self) -> String { self.ended_at .as_ref() .map(clock::fmt_time) .unwrap_or_else(|| "…".into()) } pub fn start_input(&self) -> String { clock::input_value(&self.started_at) } pub fn end_input(&self) -> String { self.ended_at .as_ref() .map(clock::input_value) .unwrap_or_default() } pub fn started_rfc3339(&self) -> String { self.started_at.to_rfc3339() } } // --- API inputs ------------------------------------------------------------- #[derive(Debug, Deserialize)] pub struct NameInput { pub name: String, /// `#rrggbb`. Clients get one from the palette, projects inherit their client's. #[serde(default)] pub color: Option, /// Projects only: `http(s)://` link to the source repository. #[serde(default)] pub repo_url: Option, } #[derive(Debug, Default, Deserialize)] pub struct UpdateInput { pub name: Option, pub archived: Option, /// Absent: unchanged. `null`: reset to the client's color (projects only). #[serde(default, deserialize_with = "present")] pub color: Option>, /// Projects only. Absent: unchanged. `null` or `""`: removed. #[serde(default, deserialize_with = "present")] pub repo_url: Option>, } #[derive(Debug, Deserialize)] pub struct StartTimer { pub project_id: i64, #[serde(default)] pub note: String, } #[derive(Debug, Deserialize)] pub struct NewEntry { pub project_id: i64, pub started_at: DateTime, pub ended_at: DateTime, #[serde(default)] pub note: String, } #[derive(Debug, Default, Deserialize)] pub struct UpdateEntry { pub project_id: Option, pub started_at: Option>, pub ended_at: Option>, pub note: Option, } #[derive(Debug, Default, Clone, Deserialize)] pub struct EntryFilter { #[serde(default, deserialize_with = "empty_as_none")] pub from: Option, /// Inclusive. #[serde(default, deserialize_with = "empty_as_none")] pub to: Option, #[serde(default, deserialize_with = "empty_as_none")] pub client_id: Option, #[serde(default, deserialize_with = "empty_as_none")] pub project_id: Option, } #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum GroupBy { Client, #[default] Project, Day, } #[derive(Debug, Default, Clone, Deserialize)] pub struct ReportQuery { #[serde(default, deserialize_with = "empty_as_none")] pub from: Option, #[serde(default, deserialize_with = "empty_as_none")] pub to: Option, #[serde(default)] pub group_by: GroupBy, #[serde(default, deserialize_with = "empty_as_none")] pub client_id: Option, #[serde(default, deserialize_with = "empty_as_none")] pub project_id: Option, } #[derive(Debug, Serialize)] pub struct Report { pub from: NaiveDate, pub to: NaiveDate, pub group_by: GroupBy, pub rows: Vec, pub total_seconds: i64, } impl Report { pub fn total(&self) -> String { clock::hhmm(self.total_seconds) } } #[derive(Debug, Serialize)] pub struct ReportRow { pub key: String, pub label: String, pub seconds: i64, /// Client or project color; `None` for days. pub color: Option, /// Share of the largest row, 0–100, for bar rendering. pub percent: u32, } impl ReportRow { pub fn duration(&self) -> String { clock::hhmm(self.seconds) } } /// Distinguishes an explicit `null` (`Some(None)`) from an absent field (`None`). fn present<'de, D, T>(de: D) -> Result>, D::Error> where D: Deserializer<'de>, T: Deserialize<'de>, { Option::::deserialize(de).map(Some) } /// HTML forms send empty strings for unset fields. pub fn empty_as_none<'de, D, T>(de: D) -> Result, D::Error> where D: Deserializer<'de>, T: FromStr, T::Err: Display, { match Option::::deserialize(de)? { Some(s) if !s.trim().is_empty() => { s.trim().parse().map(Some).map_err(serde::de::Error::custom) } _ => Ok(None), } }