Skip to main content

qimen_core/extensions/
options.rs

1use serde::{Deserialize, Serialize};
2
3/// The optional annotations to calculate after constructing a base chart.
4///
5/// Every field defaults to disabled. Each selected rule is copied into the
6/// output, so a caller can distinguish school conventions from chart facts.
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(default, deny_unknown_fields)]
10pub struct ExtensionOptions {
11    /// 暗干: fly the hour stem from the duty-door palace.
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub hidden_stems: Option<HiddenStemRule>,
14    /// 星、门、干旺衰, evaluated against both the palace and solar month.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub strength: Option<StrengthRule>,
17    /// 十二长生 for every stem and every branch in its palace.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub growth_stages: Option<GrowthRule>,
20    /// 六仪击刑, preserving heavenly/earthly and hosted-stem identities.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub punishments: Option<PunishmentRule>,
23    /// 天干入墓 under the explicitly selected convention.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub tombs: Option<TombRule>,
26    /// Day-pillar travelling horse, independent of the base chart's hour horse.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub day_horse: Option<DayHorseRule>,
29    /// 门迫: a door's element controls its destination palace's element.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub door_pressure: Option<DoorPressureRule>,
32}
33
34impl ExtensionOptions {
35    /// Whether all optional annotations are disabled.
36    #[must_use]
37    pub const fn is_empty(&self) -> bool {
38        self.hidden_stems.is_none()
39            && self.strength.is_none()
40            && self.growth_stages.is_none()
41            && self.punishments.is_none()
42            && self.tombs.is_none()
43            && self.day_horse.is_none()
44            && self.door_pressure.is_none()
45    }
46
47    /// Enables every implemented annotation with the documented conventions.
48    ///
49    /// This is an explicit convenience preset, not a claim that these are the
50    /// only traditional rules. In particular, tombs follow the twelve stages;
51    /// classical 三奇入墓 may use a different location for 乙.
52    #[must_use]
53    pub const fn all() -> Self {
54        Self {
55            hidden_stems: Some(HiddenStemRule::DutyDoorHourStemWithCenterFallback),
56            strength: Some(StrengthRule::ClassicalStarsAndFiveElements),
57            growth_stages: Some(GrowthRule::YangForwardYinReverseFireEarth),
58            punishments: Some(PunishmentRule::SixInstrumentBranches),
59            tombs: Some(TombRule::GrowthStageFireEarth),
60            day_horse: Some(DayHorseRule::DayBranchThreeHarmony),
61            door_pressure: Some(DoorPressureRule::DoorControlsPalace),
62        }
63    }
64}
65
66/// Supported 暗干 construction conventions.
67#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69#[serde(rename_all = "snake_case")]
70pub enum HiddenStemRule {
71    /// Put the hour stem at the duty door, then fly the nine instruments in
72    /// numeric palace order (yang forward, yin backward). If it repeats that
73    /// palace's earth stem, start at palace five. 甲 uses its hidden instrument.
74    DutyDoorHourStemWithCenterFallback,
75}
76
77/// Supported element-strength conventions.
78#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
80#[serde(rename_all = "snake_case")]
81pub enum StrengthRule {
82    /// Stars use 生我为废、我生为旺、同我为相、我克为休、克我为囚.
83    /// Doors/stems use ordinary 旺相休囚死. Both evaluate the palace element
84    /// and the element of the solar-month pillar's branch (not lunar month).
85    ClassicalStarsAndFiveElements,
86}
87
88/// Supported twelve-stage growth conventions.
89#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
91#[serde(rename_all = "snake_case")]
92pub enum GrowthRule {
93    /// Yang stems progress forward, yin stems backward; 戊 follows 丙 and
94    /// 己 follows 丁. Every branch in a double-branch palace remains separate.
95    YangForwardYinReverseFireEarth,
96}
97
98/// Supported 六仪击刑 conventions.
99#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
101#[serde(rename_all = "snake_case")]
102pub enum PunishmentRule {
103    /// 戊/己/庚/辛/壬/癸 at 卯/未/寅/午/辰/巳 respectively.
104    /// Heavenly-plate instruments are the traditional primary application;
105    /// earthly and hidden plates are separately identified geometric annotations.
106    SixInstrumentBranches,
107}
108
109/// Supported stem-tomb conventions; these must not be conflated.
110#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
112#[serde(rename_all = "snake_case")]
113pub enum TombRule {
114    /// Use 墓 in the yang-forward/yin-reverse twelve stages, with earth
115    /// following fire. In this convention 乙's tomb is 戌, not 未.
116    GrowthStageFireEarth,
117    /// Classical 三奇入墓 only: 乙→未、丙→戌、丁→丑. Other stems
118    /// are explicitly not applicable; this does not supply a six-instrument rule.
119    TraditionalThreeWonders,
120}
121
122/// Supported day-horse conventions.
123#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(rename_all = "snake_case")]
126pub enum DayHorseRule {
127    /// 申子辰→寅、寅午戌→申、亥卯未→巳、巳酉丑→亥, using the
128    /// calculated day pillar and therefore the request's day-boundary rule.
129    DayBranchThreeHarmony,
130}
131
132/// Supported door-pressure conventions.
133#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(rename_all = "snake_case")]
136pub enum DoorPressureRule {
137    /// Door element controls palace element; the reverse is not 门迫.
138    DoorControlsPalace,
139}