Skip to main content

qimen_core/extensions/
model.rs

1use serde::{Deserialize, Serialize};
2
3use super::{
4    DayHorseRule, DoorPressureRule, GrowthRule, HiddenStemRule, PunishmentRule, StrengthRule,
5    TombRule,
6};
7use crate::{Branch, Cycle, Door, Element, Horse, Star, Stem};
8
9/// Optional annotations. Absent fields were not requested, rather than negative.
10#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
11#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
12pub struct ChartExtensions {
13    /// Nine-palace 暗干 with its construction convention.
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub hidden_stems: Option<HiddenStems>,
16    /// Star, door and stem strengths against palace and solar month.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub strength: Option<Strengths>,
19    /// Stem growth stages at each constituent branch of a palace.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub growth_stages: Option<GrowthStages>,
22    /// Six-instrument punishment annotations.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub punishments: Option<Punishments>,
25    /// Stem tomb annotations with an explicit convention and applicability.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub tombs: Option<Tombs>,
28    /// Day-pillar horse, preserving the base chart's separate hour horse.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub day_horse: Option<DayHorse>,
31    /// Door-over-palace element control; excludes the reverse relationship.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub door_pressure: Option<DoorPressures>,
34}
35
36/// A plate carrying a stem annotation.
37#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39#[serde(rename_all = "snake_case")]
40pub enum StemPlate {
41    /// Rotating heavenly plate.
42    Heaven,
43    /// Fixed earthly plate, including the explicitly identified hosted center.
44    Earth,
45    /// Optional nine-palace 暗干 plate; included only when requested.
46    Hidden,
47}
48
49/// Identity of a stem occurrence; hosted stems must not be silently collapsed.
50#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
51#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
52pub struct StemPlacement {
53    /// Destination Luo Shu palace, one through nine.
54    pub palace: u8,
55    /// Heavenly, earthly or optional hidden plate.
56    pub plate: StemPlate,
57    /// Heavenly stem.
58    pub stem: Stem,
59    /// Source earth palace; absent for constructed hidden-plate stems.
60    pub source_palace: Option<u8>,
61    /// Whether this is an additional hosted occurrence of the center stem.
62    pub is_center_hosted: bool,
63}
64
65/// A constructed hidden stem in one numeric palace.
66#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct HiddenStem {
69    /// Luo Shu palace number.
70    pub palace: u8,
71    /// Hidden-plate stem.
72    pub stem: Stem,
73}
74
75/// Nine-palace hidden-stem calculation and its actual starting point.
76#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
77#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78pub struct HiddenStems {
79    /// Selected construction convention.
80    pub rule: HiddenStemRule,
81    /// Hour stem, with 甲 replaced by its period's hidden instrument.
82    pub effective_hour_stem: Stem,
83    /// Actual initial numeric palace, after the repetition fallback.
84    pub start_palace: u8,
85    /// Whether repetition with the duty door's earth stem moved the start to five.
86    pub used_center_fallback: bool,
87    /// Exactly nine records, in numeric palace order.
88    pub palaces: [HiddenStem; 9],
89}
90
91macro_rules! states {
92    ($(#[$meta:meta])* $name:ident { $($(#[$variant_meta:meta])* $variant:ident => $label:literal),+ $(,)? }) => {
93        $(#[$meta])*
94        #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
95        #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
96        #[serde(rename_all = "snake_case")]
97        pub enum $name { $($(#[$variant_meta])* $variant),+ }
98
99        impl $name {
100            /// Traditional Chinese label.
101            #[must_use]
102            pub const fn name(self) -> &'static str {
103                match self { $(Self::$variant => $label),+ }
104            }
105        }
106    };
107}
108
109states! {
110    /// Nine-star strength; 废 is distinct from the ordinary element's 死.
111    StarStrengthState {
112        /// The star generates the environment: 旺.
113        Wang => "旺",
114        /// The star and environment have the same element: 相.
115        Xiang => "相",
116        /// The star controls the environment: 休.
117        Xiu => "休",
118        /// The environment controls the star: 囚.
119        Qiu => "囚",
120        /// The environment generates the star: 废.
121        Fei => "废",
122    }
123}
124
125states! {
126    /// Ordinary five-element strength, used for doors and stems.
127    ElementStrengthState {
128        /// Same element as the environment: 旺.
129        Wang => "旺",
130        /// Generated by the environment: 相.
131        Xiang => "相",
132        /// Generates the environment: 休.
133        Xiu => "休",
134        /// Controls the environment: 囚.
135        Qiu => "囚",
136        /// Controlled by the environment: 死.
137        Si => "死",
138    }
139}
140
141/// Strength of a moving star under both explicitly recorded environments.
142#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
143#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
144pub struct StarStrength {
145    /// Star identity, including a separate 天禽 entry.
146    pub star: Star,
147    /// Star's intrinsic element.
148    pub element: Element,
149    /// Relationship to the destination palace's element.
150    pub at_palace: StarStrengthState,
151    /// Relationship to the solar-month branch's element.
152    pub at_month: StarStrengthState,
153}
154
155/// Strength of a moving door.
156#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
157#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
158pub struct DoorStrength {
159    /// Door identity.
160    pub door: Door,
161    /// Door's intrinsic element.
162    pub element: Element,
163    /// Relationship to the destination palace's element.
164    pub at_palace: ElementStrengthState,
165    /// Relationship to the solar-month branch's element.
166    pub at_month: ElementStrengthState,
167}
168
169/// Strength of one stem occurrence.
170#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
171#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
172pub struct StemStrength {
173    /// Stem and plate provenance.
174    pub placement: StemPlacement,
175    /// Stem's intrinsic element.
176    pub element: Element,
177    /// Relationship to the destination palace's element.
178    pub at_palace: ElementStrengthState,
179    /// Relationship to the solar-month branch's element.
180    pub at_month: ElementStrengthState,
181}
182
183/// All strength annotations for a single palace.
184#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
185#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
186pub struct PalaceStrength {
187    /// Luo Shu palace number.
188    pub palace: u8,
189    /// Fixed destination-palace element.
190    pub element: Element,
191    /// Separate annotations for every star, including 天禽.
192    pub stars: Vec<StarStrength>,
193    /// Door annotation, absent at the center.
194    pub door: Option<DoorStrength>,
195    /// Every earthly/heavenly stem; hidden stems are included when enabled.
196    pub stems: Vec<StemStrength>,
197}
198
199/// Strength annotations with the exact seasonal environment used.
200#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
201#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
202pub struct Strengths {
203    /// Selected rule, distinguishing star strength from ordinary element strength.
204    pub rule: StrengthRule,
205    /// Branch of the solar-month pillar; never inferred from lunar month number.
206    pub month_branch: Branch,
207    /// Element of the solar-month branch. 辰戌丑未 are earth; no implicit
208    /// eighteen-day seasonal-transition adjustment is applied.
209    pub month_element: Element,
210    /// All nine palaces in numeric order.
211    pub palaces: Vec<PalaceStrength>,
212}
213
214states! {
215    /// One of the twelve stages of stem growth.
216    GrowthStage {
217        /// 长生.
218        ChangSheng => "长生",
219        /// 沐浴.
220        MuYu => "沐浴",
221        /// 冠带.
222        GuanDai => "冠带",
223        /// 临官.
224        LinGuan => "临官",
225        /// 帝旺.
226        DiWang => "帝旺",
227        /// 衰.
228        Shuai => "衰",
229        /// 病.
230        Bing => "病",
231        /// 死.
232        Si => "死",
233        /// 墓.
234        Mu => "墓",
235        /// 绝.
236        Jue => "绝",
237        /// 胎.
238        Tai => "胎",
239        /// 养.
240        Yang => "养",
241    }
242}
243
244/// One growth stage at one concrete palace branch.
245#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
246#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
247pub struct BranchGrowth {
248    /// The actual palace branch; double-branch palaces retain both entries.
249    pub branch: Branch,
250    /// Stage at that branch under the selected growth convention.
251    pub stage: GrowthStage,
252}
253
254/// Growth stages for one stem occurrence.
255#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
256#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
257pub struct StemGrowth {
258    /// Stem and plate provenance.
259    pub placement: StemPlacement,
260    /// Zero branches at center five; one or two elsewhere.
261    pub branches: Vec<BranchGrowth>,
262}
263
264/// Twelve-stage results and the polarity/earth convention used.
265#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
266#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
267pub struct GrowthStages {
268    /// Explicit growth convention.
269    pub rule: GrowthRule,
270    /// Every earthly/heavenly stem; hidden stems are included when enabled.
271    pub stems: Vec<StemGrowth>,
272}
273
274/// Punishment evaluation for one stem occurrence.
275#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
276#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
277pub struct StemPunishment {
278    /// Stem and plate provenance.
279    pub placement: StemPlacement,
280    /// The six instrument's hidden 甲 pillar; absent for the three wonders.
281    pub hidden_jia: Option<Cycle>,
282    /// Palace branch struck by this instrument; absent when not applicable.
283    pub punished_branch: Option<Branch>,
284    /// Whether the actual destination palace contains that branch.
285    pub is_punished: bool,
286}
287
288/// 六仪击刑 evaluations, with plate identities retained.
289#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
290#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
291pub struct Punishments {
292    /// Selected punishment convention.
293    pub rule: PunishmentRule,
294    /// Heavenly instruments are primary; other plate results are explicitly typed.
295    pub stems: Vec<StemPunishment>,
296}
297
298/// Tomb evaluation for one stem occurrence.
299#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
300#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
301pub struct StemTomb {
302    /// Stem and plate provenance.
303    pub placement: StemPlacement,
304    /// Branch containing the tomb; `None` when the chosen rule does not cover
305    /// this stem (for example a six instrument under the three-wonders rule).
306    pub tomb_branch: Option<Branch>,
307    /// Whether the palace contains the tomb branch; `None` means not applicable.
308    pub is_in_tomb: Option<bool>,
309}
310
311/// Tomb evaluations; applicability remains distinct from a negative result.
312#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
313#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
314pub struct Tombs {
315    /// Selected tomb convention.
316    pub rule: TombRule,
317    /// Every stem with explicit rule applicability.
318    pub stems: Vec<StemTomb>,
319}
320
321/// Optional day horse, independent of the existing hour-based palace marker.
322#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
323#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
324pub struct DayHorse {
325    /// Selected derivation convention.
326    pub rule: DayHorseRule,
327    /// Exact day pillar after applying the request's day-boundary convention.
328    pub pillar: Cycle,
329    /// Day horse branch and its palace.
330    pub horse: Horse,
331}
332
333/// Door pressure for a single occupied door palace.
334#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
335#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
336pub struct DoorPressure {
337    /// Destination Luo Shu palace number.
338    pub palace: u8,
339    /// Moving door identity.
340    pub door: Door,
341    /// Intrinsic door element.
342    pub door_element: Element,
343    /// Destination palace element.
344    pub palace_element: Element,
345    /// True only when the door's element controls the palace's element.
346    pub is_pressed: bool,
347}
348
349/// Door-pressure results for all eight doors.
350#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
351#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
352pub struct DoorPressures {
353    /// Selected control-direction convention.
354    pub rule: DoorPressureRule,
355    /// Eight door entries in numeric destination-palace order.
356    pub doors: Vec<DoorPressure>,
357}