1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use serde::{Deserialize, Serialize};
/// Canonical twelve-palace sequence used for cyclic palace arithmetic.
pub const PALACE_NAMES: [PalaceName; 12] = [
PalaceName::Life,
PalaceName::Siblings,
PalaceName::Spouse,
PalaceName::Children,
PalaceName::Wealth,
PalaceName::Health,
PalaceName::Migration,
PalaceName::Friends,
PalaceName::Career,
PalaceName::Property,
PalaceName::Spirit,
PalaceName::Parents,
];
/// A named Zi Wei Dou Shu palace.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PalaceName {
/// Life Palace (命宫).
Life,
/// Siblings Palace (兄弟宫).
Siblings,
/// Spouse Palace (夫妻宫).
Spouse,
/// Children Palace (子女宫).
Children,
/// Wealth Palace (财帛宫).
Wealth,
/// Health Palace (疾厄宫).
Health,
/// Migration Palace (迁移宫).
Migration,
/// Friends Palace (仆役宫).
Friends,
/// Career Palace (官禄宫).
Career,
/// Property Palace (田宅宫).
Property,
/// Spirit Palace (福德宫).
Spirit,
/// Parents Palace (父母宫).
Parents,
}
impl PalaceName {
/// Returns this palace's zero-based position in [`PALACE_NAMES`].
pub const fn index(self) -> usize {
match self {
Self::Life => 0,
Self::Siblings => 1,
Self::Spouse => 2,
Self::Children => 3,
Self::Wealth => 4,
Self::Health => 5,
Self::Migration => 6,
Self::Friends => 7,
Self::Career => 8,
Self::Property => 9,
Self::Spirit => 10,
Self::Parents => 11,
}
}
/// Returns the palace at `index`, wrapping with modulo arithmetic.
pub fn from_index(index: usize) -> Self {
PALACE_NAMES[index % PALACE_NAMES.len()]
}
/// Returns the palace offset by `delta`, wrapping in both directions.
pub fn offset(self, delta: isize) -> Self {
let len = PALACE_NAMES.len() as isize;
let index = (self.index() as isize + delta).rem_euclid(len) as usize;
Self::from_index(index)
}
}