shift_algorithm/types.rs
1//! Core data types for the shift scheduling system.
2
3use serde::{Deserialize, Serialize};
4
5/// The five shift types in a standard Chinese rotating shift system.
6///
7/// # Variants
8///
9/// | Variant | Label | Full Label | Category |
10/// |---------|-------|-----------|----------|
11/// | [`Morning`](ShiftType::Morning) | 早 | 早班 | Work |
12/// | [`Afternoon`](ShiftType::Afternoon) | 中 | 中班 | Work |
13/// | [`Rest`](ShiftType::Rest) | 休 | 休班 | Rest |
14/// | [`Night`](ShiftType::Night) | 夜 | 夜班 | Work |
15/// | [`Study`](ShiftType::Study) | 学 | 学习班 | Rest |
16///
17/// # Example
18///
19/// ```rust
20/// use shift_algorithm::ShiftType;
21///
22/// assert!(ShiftType::Morning.is_work());
23/// assert!(!ShiftType::Rest.is_work());
24/// assert!(ShiftType::Rest.is_rest());
25/// assert!(ShiftType::Study.is_rest());
26/// ```
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
28#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
29pub enum ShiftType {
30 /// 早班 — morning shift
31 Morning,
32 /// 中班 — afternoon shift
33 Afternoon,
34 /// 休班 — rest day
35 Rest,
36 /// 夜班 — night shift
37 Night,
38 /// 学习班 — study/training day (counts as rest for scheduling purposes)
39 Study,
40}
41
42impl ShiftType {
43 /// Short Chinese label (single character).
44 pub fn label(&self) -> &'static str {
45 match self {
46 ShiftType::Morning => "早",
47 ShiftType::Afternoon => "中",
48 ShiftType::Rest => "休",
49 ShiftType::Night => "夜",
50 ShiftType::Study => "学",
51 }
52 }
53
54 /// Full Chinese label.
55 pub fn full_label(&self) -> &'static str {
56 match self {
57 ShiftType::Morning => "早班",
58 ShiftType::Afternoon => "中班",
59 ShiftType::Rest => "休班",
60 ShiftType::Night => "夜班",
61 ShiftType::Study => "学习班",
62 }
63 }
64
65 /// Short English label.
66 pub fn label_en(&self) -> &'static str {
67 match self {
68 ShiftType::Morning => "AM",
69 ShiftType::Afternoon => "PM",
70 ShiftType::Rest => "R ",
71 ShiftType::Night => "NT",
72 ShiftType::Study => "TR",
73 }
74 }
75
76 /// Padded English label (3 chars for alignment).
77 pub fn label_en_padded(&self) -> &'static str {
78 match self {
79 ShiftType::Morning => "AM ",
80 ShiftType::Afternoon => "PM ",
81 ShiftType::Rest => "R ",
82 ShiftType::Night => "NT ",
83 ShiftType::Study => "TR ",
84 }
85 }
86
87 /// Full English label.
88 pub fn full_label_en(&self) -> &'static str {
89 match self {
90 ShiftType::Morning => "Morning",
91 ShiftType::Afternoon => "Afternoon",
92 ShiftType::Rest => "Rest",
93 ShiftType::Night => "Night",
94 ShiftType::Study => "Study",
95 }
96 }
97
98 /// Returns `true` if this is a working shift (Morning, Afternoon, or Night).
99 ///
100 /// Used for counting work days, consecutive work stats, etc.
101 pub fn is_work(&self) -> bool {
102 matches!(self, ShiftType::Morning | ShiftType::Afternoon | ShiftType::Night)
103 }
104
105 /// Returns `true` if this counts as rest (Rest or Study).
106 ///
107 /// Study days are treated as rest because the worker is not on duty.
108 pub fn is_rest(&self) -> bool {
109 matches!(self, ShiftType::Rest | ShiftType::Study)
110 }
111}
112
113/// Result of querying what shift falls on a given date.
114///
115/// Returned by [`get_shift_info`](crate::get_shift_info).
116///
117/// # Fields
118///
119/// | Field | Type | Range | Description |
120/// |-------|------|-------|-------------|
121/// | `date` | `NaiveDate` | — | The queried date |
122/// | `day_of_cycle` | `u32` | `1..=cycle_length` | Which day in the cycle (1-based) |
123/// | `cycle_index` | `u32` | `0..=cycle_length-1` | Zero-based index into the cycle array |
124/// | `shift_type` | [`ShiftType`] | — | The shift type for this date |
125///
126/// ```rust
127/// use shift_algorithm::cycle::default_config;
128/// use shift_algorithm::get_shift_info;
129///
130/// let config = default_config();
131/// let info = get_shift_info(config.reference_date, &config, 0);
132///
133/// assert_eq!(info.day_of_cycle, 1);
134/// assert_eq!(info.cycle_index, 0);
135/// ```
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct ShiftInfo {
138 /// The queried date.
139 pub date: chrono::NaiveDate,
140 /// Day index within the cycle (1-based, 1..=cycle_length).
141 pub day_of_cycle: u32,
142 /// Zero-based index into the cycle array (0..=cycle_length-1).
143 pub cycle_index: u32,
144 /// The shift type for this date.
145 pub shift_type: ShiftType,
146}
147
148/// Runtime shift cycle configuration.
149///
150/// The default 42-day, 6-team configuration is available via
151/// [`default_config()`](crate::cycle::default_config).
152///
153/// # Custom cycles
154///
155/// ```rust
156/// use shift_algorithm::{ShiftCycleConfig, ShiftType};
157/// use chrono::NaiveDate;
158///
159/// let config = ShiftCycleConfig {
160/// cycle: vec![ShiftType::Morning, ShiftType::Afternoon, ShiftType::Rest],
161/// cycle_length: 3,
162/// reference_date: NaiveDate::from_ymd_opt(2025, 12, 15).unwrap(),
163/// total_teams: 1,
164/// };
165/// ```
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct ShiftCycleConfig {
168 /// The ordered list of shift types defining one full cycle.
169 /// Must have length == `cycle_length`.
170 pub cycle: Vec<ShiftType>,
171 /// Number of days in one full cycle (= `cycle.len()`).
172 pub cycle_length: u32,
173 /// The anchor reference date. Day 1 of the cycle falls on this date.
174 /// Default: 2025-12-15.
175 pub reference_date: chrono::NaiveDate,
176 /// Total number of teams sharing this cycle.
177 /// Each team is offset by `cycle_length / total_teams` days.
178 /// Default: 6.
179 pub total_teams: u32,
180}
181
182/// Chinese team name for a team ID.
183///
184/// ```rust
185/// use shift_algorithm::team_name;
186/// assert_eq!(team_name(1), "一值");
187/// assert_eq!(team_name(3), "三值");
188/// assert_eq!(team_name(6), "六值");
189/// ```
190pub fn team_name(id: u32) -> String {
191 let prefix = match id {
192 1 => "一", 2 => "二", 3 => "三",
193 4 => "四", 5 => "五", 6 => "六",
194 _ => return format!("{}值", id),
195 };
196 format!("{}值", prefix)
197}
198
199impl ShiftCycleConfig {
200 /// Create a new config, validating that `cycle.len() == cycle_length`.
201 ///
202 /// # Panics
203 /// Panics if `cycle.len() != cycle_length as usize`.
204 pub fn new(cycle: Vec<ShiftType>, reference_date: chrono::NaiveDate, total_teams: u32) -> Self {
205 let cycle_length = cycle.len() as u32;
206 assert!(cycle_length >= 1, "cycle must be non-empty");
207 assert!(total_teams >= 1, "total_teams must be >= 1");
208 Self { cycle, cycle_length, reference_date, total_teams }
209 }
210
211 /// Team phase offset in days.
212 ///
213 /// Formula: `(team_id - 1) * (cycle_length / total_teams)`.
214 ///
215 /// For a 42-day, 6-team cycle:
216 /// - Team 1 (一值): offset 0
217 /// - Team 2 (二值): offset 7
218 /// - Team 3 (三值): offset 14
219 /// - ...
220 /// - Team 6 (六值): offset 35
221 ///
222 /// ```rust
223 /// use shift_algorithm::cycle::default_config;
224 ///
225 /// let config = default_config();
226 /// assert_eq!(config.team_phase_offset(1), 0);
227 /// assert_eq!(config.team_phase_offset(2), 7);
228 /// assert_eq!(config.team_phase_offset(6), 35);
229 /// ```
230 pub fn team_phase_offset(&self, team_id: u32) -> u32 {
231 (team_id - 1) * (self.cycle_length / self.total_teams)
232 }
233}