Skip to main content

ShiftCycleConfig

Struct ShiftCycleConfig 

Source
pub struct ShiftCycleConfig {
    pub cycle: Vec<ShiftType>,
    pub cycle_length: u32,
    pub reference_date: NaiveDate,
    pub total_teams: u32,
}
Expand description

Runtime shift cycle configuration.

The default 42-day, 6-team configuration is available via default_config().

§Custom cycles

use shift_algorithm::{ShiftCycleConfig, ShiftType};
use chrono::NaiveDate;

let config = ShiftCycleConfig {
    cycle: vec![ShiftType::Morning, ShiftType::Afternoon, ShiftType::Rest],
    cycle_length: 3,
    reference_date: NaiveDate::from_ymd_opt(2025, 12, 15).unwrap(),
    total_teams: 1,
};

Fields§

§cycle: Vec<ShiftType>

The ordered list of shift types defining one full cycle. Must have length == cycle_length.

§cycle_length: u32

Number of days in one full cycle (= cycle.len()).

§reference_date: NaiveDate

The anchor reference date. Day 1 of the cycle falls on this date. Default: 2025-12-15.

§total_teams: u32

Total number of teams sharing this cycle. Each team is offset by cycle_length / total_teams days. Default: 6.

Implementations§

Source§

impl ShiftCycleConfig

Source

pub fn new( cycle: Vec<ShiftType>, reference_date: NaiveDate, total_teams: u32, ) -> Self

Create a new config, validating that cycle.len() == cycle_length.

§Panics

Panics if cycle.len() != cycle_length as usize.

Source

pub fn successor_of(&self, team_id: u32) -> u32

The team that follows yours in the circular rotation order.

Convenience wrapper around successor_team_id using self.total_teams.

use shift_algorithm::cycle::default_config;

let config = default_config();
assert_eq!(config.successor_of(1), 2);
assert_eq!(config.successor_of(6), 1);
Source

pub fn predecessor_of(&self, team_id: u32) -> u32

The team that yours follows in the circular rotation order.

The predecessor is the team whose shift your team takes over. Convenience wrapper around predecessor_team_id using self.total_teams.

use shift_algorithm::cycle::default_config;

let config = default_config();
assert_eq!(config.predecessor_of(1), 6); // Team 1 takes over from Team 6
assert_eq!(config.predecessor_of(2), 1); // Team 2 takes over from Team 1
assert_eq!(config.predecessor_of(6), 5);
Source

pub fn team_phase_offset(&self, team_id: u32) -> u32

Team phase offset in days.

Formula: (team_id - 1) * (cycle_length / total_teams).

For a 42-day, 6-team cycle:

  • Team 1 (一值): offset 0
  • Team 2 (二值): offset 7
  • Team 3 (三值): offset 14
  • Team 6 (六值): offset 35
use shift_algorithm::cycle::default_config;

let config = default_config();
assert_eq!(config.team_phase_offset(1), 0);
assert_eq!(config.team_phase_offset(2), 7);
assert_eq!(config.team_phase_offset(6), 35);
Source

pub fn shift_handover( &self, date: NaiveDate, team_id: u32, ) -> Option<(u32, u32)>

Find which team you take over from and which team takes over from you.

Shift handover happens within a single day between different shift types:

  • 夜 → 早 → 中 → 夜 (cyclical)
  • If you are on 休 or 学, there is no handover (you’re not working).

Returns (predecessor_team_id, successor_team_id) — the teams whose shifts you take over from and who takes over from you, respectively.

use shift_algorithm::cycle::default_config;
use chrono::NaiveDate;

let config = default_config();
let date = NaiveDate::from_ymd_opt(2026, 6, 26).unwrap();

// If team 1 is working 早班 today, predecessor should be the team on 夜班,
// successor should be the team on 中班.
if let Some((pred, succ)) = config.shift_handover(date, 1) {
    println!("Take over from team {}, hand over to team {}", pred, succ);
}

Trait Implementations§

Source§

impl Clone for ShiftCycleConfig

Source§

fn clone(&self) -> ShiftCycleConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ShiftCycleConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ShiftCycleConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for ShiftCycleConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.