| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use crate::id_types::{ContextId, EventId, UserId}; |
| 21 |
use chrono::{DateTime, NaiveDate, Utc}; |
| 22 |
use serde::{Deserialize, Serialize}; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 31 |
#[serde(rename_all = "camelCase")] |
| 32 |
pub enum ContextKind { |
| 33 |
|
| 34 |
Vacation, |
| 35 |
|
| 36 |
Trip, |
| 37 |
|
| 38 |
Illness, |
| 39 |
|
| 40 |
Sprint, |
| 41 |
|
| 42 |
Other, |
| 43 |
} |
| 44 |
|
| 45 |
impl ContextKind { |
| 46 |
|
| 47 |
#[must_use] |
| 48 |
pub const fn as_str(self) -> &'static str { |
| 49 |
match self { |
| 50 |
Self::Vacation => "Vacation", |
| 51 |
Self::Trip => "Trip", |
| 52 |
Self::Illness => "Illness", |
| 53 |
Self::Sprint => "Sprint", |
| 54 |
Self::Other => "Other", |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[must_use] |
| 67 |
pub fn parse(stored: &str) -> Self { |
| 68 |
match stored { |
| 69 |
"Vacation" => Self::Vacation, |
| 70 |
"Trip" => Self::Trip, |
| 71 |
"Illness" => Self::Illness, |
| 72 |
"Sprint" => Self::Sprint, |
| 73 |
_ => Self::Other, |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 80 |
#[serde(rename_all = "camelCase")] |
| 81 |
pub struct Context { |
| 82 |
|
| 83 |
pub id: ContextId, |
| 84 |
|
| 85 |
pub user_id: UserId, |
| 86 |
|
| 87 |
pub label: String, |
| 88 |
|
| 89 |
pub kind: ContextKind, |
| 90 |
|
| 91 |
pub starts_on: NaiveDate, |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub ends_on: NaiveDate, |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub migrated_from_event_id: Option<EventId>, |
| 106 |
|
| 107 |
pub created_at: DateTime<Utc>, |
| 108 |
|
| 109 |
pub updated_at: DateTime<Utc>, |
| 110 |
} |
| 111 |
|
| 112 |
impl Context { |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#[must_use] |
| 119 |
pub fn covers(&self, day: NaiveDate) -> bool { |
| 120 |
self.starts_on <= day && day <= self.ends_on |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
#[must_use] |
| 125 |
pub fn days(&self) -> i64 { |
| 126 |
(self.ends_on - self.starts_on).num_days() + 1 |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#[must_use] |
| 135 |
pub fn touches(&self, other: &Self) -> bool { |
| 136 |
self.starts_on <= other.ends_on.succ_opt().unwrap_or(other.ends_on) |
| 137 |
&& other.starts_on <= self.ends_on.succ_opt().unwrap_or(self.ends_on) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
#[cfg(test)] |
| 142 |
mod tests { |
| 143 |
use super::*; |
| 144 |
|
| 145 |
fn ctx(starts: &str, ends: &str) -> Context { |
| 146 |
Context { |
| 147 |
id: ContextId::new(), |
| 148 |
user_id: UserId::new(), |
| 149 |
label: "Leave".to_owned(), |
| 150 |
kind: ContextKind::Vacation, |
| 151 |
starts_on: starts.parse().expect("a date"), |
| 152 |
ends_on: ends.parse().expect("a date"), |
| 153 |
migrated_from_event_id: None, |
| 154 |
created_at: Utc::now(), |
| 155 |
updated_at: Utc::now(), |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
fn day(date: &str) -> NaiveDate { |
| 160 |
date.parse().expect("a date") |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn both_ends_are_inside() { |
| 165 |
let leave = ctx("2026-08-03", "2026-08-17"); |
| 166 |
assert!(leave.covers(day("2026-08-03")), "the first day is off"); |
| 167 |
assert!(leave.covers(day("2026-08-17")), "and so is the last"); |
| 168 |
assert!(leave.covers(day("2026-08-10"))); |
| 169 |
assert!(!leave.covers(day("2026-08-02"))); |
| 170 |
assert!(!leave.covers(day("2026-08-18"))); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn a_single_day_context_is_one_day_long() { |
| 175 |
assert_eq!(ctx("2026-08-03", "2026-08-03").days(), 1); |
| 176 |
assert_eq!(ctx("2026-08-03", "2026-08-17").days(), 15); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn touching_runs_are_joinable_and_a_gap_is_not() { |
| 181 |
|
| 182 |
let first = ctx("2026-08-06", "2026-08-09"); |
| 183 |
let second = ctx("2026-08-10", "2026-08-11"); |
| 184 |
assert!(first.touches(&second), "adjacent days are one run"); |
| 185 |
assert!( |
| 186 |
second.touches(&first), |
| 187 |
"and it does not depend on the order" |
| 188 |
); |
| 189 |
|
| 190 |
let apart = ctx("2026-08-12", "2026-08-13"); |
| 191 |
assert!(!first.touches(&apart), "a clear day between is two runs"); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn an_unknown_kind_reads_as_other_rather_than_failing() { |
| 196 |
assert_eq!(ContextKind::parse("Sabbatical"), ContextKind::Other); |
| 197 |
assert_eq!(ContextKind::parse("Vacation"), ContextKind::Vacation); |
| 198 |
|
| 199 |
for kind in [ |
| 200 |
ContextKind::Vacation, |
| 201 |
ContextKind::Trip, |
| 202 |
ContextKind::Illness, |
| 203 |
ContextKind::Sprint, |
| 204 |
ContextKind::Other, |
| 205 |
] { |
| 206 |
assert_eq!(ContextKind::parse(kind.as_str()), kind); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|