Les entrées exposent repo_url ; sur /entries et l'accueil, le nom du projet ouvre le repo dans un nouvel onglet (soulignement pointillé). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
236 lines
6.1 KiB
Rust
236 lines
6.1 KiB
Rust
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<Utc>,
|
||
}
|
||
|
||
#[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<String>,
|
||
/// `color`, or the client's color.
|
||
pub effective_color: String,
|
||
/// Link to the source repository.
|
||
pub repo_url: Option<String>,
|
||
pub archived: bool,
|
||
pub created_at: DateTime<Utc>,
|
||
}
|
||
|
||
#[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<String>,
|
||
pub started_at: DateTime<Utc>,
|
||
/// `None` while the timer is running.
|
||
pub ended_at: Option<DateTime<Utc>>,
|
||
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<String>,
|
||
/// Projects only: `http(s)://` link to the source repository.
|
||
#[serde(default)]
|
||
pub repo_url: Option<String>,
|
||
}
|
||
|
||
#[derive(Debug, Default, Deserialize)]
|
||
pub struct UpdateInput {
|
||
pub name: Option<String>,
|
||
pub archived: Option<bool>,
|
||
/// Absent: unchanged. `null`: reset to the client's color (projects only).
|
||
#[serde(default, deserialize_with = "present")]
|
||
pub color: Option<Option<String>>,
|
||
/// Projects only. Absent: unchanged. `null` or `""`: removed.
|
||
#[serde(default, deserialize_with = "present")]
|
||
pub repo_url: Option<Option<String>>,
|
||
}
|
||
|
||
#[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<FixedOffset>,
|
||
pub ended_at: DateTime<FixedOffset>,
|
||
#[serde(default)]
|
||
pub note: String,
|
||
}
|
||
|
||
#[derive(Debug, Default, Deserialize)]
|
||
pub struct UpdateEntry {
|
||
pub project_id: Option<i64>,
|
||
pub started_at: Option<DateTime<FixedOffset>>,
|
||
pub ended_at: Option<DateTime<FixedOffset>>,
|
||
pub note: Option<String>,
|
||
}
|
||
|
||
#[derive(Debug, Default, Clone, Deserialize)]
|
||
pub struct EntryFilter {
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub from: Option<NaiveDate>,
|
||
/// Inclusive.
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub to: Option<NaiveDate>,
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub client_id: Option<i64>,
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub project_id: Option<i64>,
|
||
}
|
||
|
||
#[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<NaiveDate>,
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub to: Option<NaiveDate>,
|
||
#[serde(default)]
|
||
pub group_by: GroupBy,
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub client_id: Option<i64>,
|
||
#[serde(default, deserialize_with = "empty_as_none")]
|
||
pub project_id: Option<i64>,
|
||
}
|
||
|
||
#[derive(Debug, Serialize)]
|
||
pub struct Report {
|
||
pub from: NaiveDate,
|
||
pub to: NaiveDate,
|
||
pub group_by: GroupBy,
|
||
pub rows: Vec<ReportRow>,
|
||
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<String>,
|
||
/// 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<Option<Option<T>>, D::Error>
|
||
where
|
||
D: Deserializer<'de>,
|
||
T: Deserialize<'de>,
|
||
{
|
||
Option::<T>::deserialize(de).map(Some)
|
||
}
|
||
|
||
/// HTML forms send empty strings for unset fields.
|
||
pub fn empty_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
|
||
where
|
||
D: Deserializer<'de>,
|
||
T: FromStr,
|
||
T::Err: Display,
|
||
{
|
||
match Option::<String>::deserialize(de)? {
|
||
Some(s) if !s.trim().is_empty() => {
|
||
s.trim().parse().map(Some).map_err(serde::de::Error::custom)
|
||
}
|
||
_ => Ok(None),
|
||
}
|
||
}
|