qimen_core/model.rs
1use qimen_calendar::{Branch, CalendarResult, Cycle, Stem};
2use serde::{Deserialize, Serialize};
3
4use crate::{ChartRequest, Deity, Direction, Door, Dun, Element, Method, Star, Trigram, Yuan};
5
6/// The supported center-palace convention.
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum CenterPalaceRule {
11 /// Always host palace five in palace two, for both yang and yin dun.
12 AlwaysKun,
13}
14
15/// The supported placement of the central star.
16#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
18#[serde(rename_all = "snake_case")]
19pub enum TianQinRule {
20 /// 天禽 and its center earth stem travel with 天芮.
21 FollowsTianRui,
22}
23
24/// The pillar used for a chart-level branch annotation.
25#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(rename_all = "snake_case")]
28pub enum AnnotationBasis {
29 /// Use the hour pillar.
30 Hour,
31}
32
33/// Explicit conventions needed when comparing results between schools/software.
34#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
35#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
36pub struct Conventions {
37 /// Center-palace hosting rule.
38 pub center_palace: CenterPalaceRule,
39 /// Central-star movement rule.
40 pub tian_qin: TianQinRule,
41 /// Basis for the void markers on individual palaces.
42 pub void_basis: AnnotationBasis,
43 /// Basis for the horse marker.
44 pub horse_basis: AnnotationBasis,
45}
46
47/// The hour's ten-position period and its hidden 甲 stem.
48#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub struct Xun {
51 /// 甲子, 甲戌, 甲申, 甲午, 甲辰 or 甲寅.
52 pub head: Cycle,
53 /// 戊, 己, 庚, 辛, 壬 or 癸, respectively.
54 pub hidden_stem: Stem,
55 /// The two unpaired branches for this period.
56 pub void_branches: [Branch; 2],
57}
58
59/// The two void branches of each pillar; palace markers use the hour pair.
60#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
61#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
62pub struct PillarVoids {
63 /// Year-pillar void branches.
64 pub year: [Branch; 2],
65 /// Month-pillar void branches.
66 pub month: [Branch; 2],
67 /// Day-pillar void branches.
68 pub day: [Branch; 2],
69 /// Hour-pillar void branches.
70 pub hour: [Branch; 2],
71}
72
73/// Duty star and duty door, preserving the original center palace when relevant.
74#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
75#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
76pub struct Leaders {
77 /// 值符星; this is 天禽 when the period's hidden stem originated in palace five.
78 pub star: Star,
79 /// 值使门; palace five uses 死门 under the selected hosting convention.
80 pub door: Door,
81 /// Earth palace containing the period's hidden stem, before hosting.
82 pub original_palace: u8,
83 /// Current palace of the duty star, after center hosting.
84 pub star_palace: u8,
85 /// Current palace of the duty door, after center hosting.
86 pub door_palace: u8,
87 /// Duty-door numeric flight result before center hosting; may be five.
88 pub door_raw_palace: u8,
89}
90
91/// A travelling horse; its containing field identifies the source pillar.
92#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
93#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
94pub struct Horse {
95 /// One of 寅, 申, 巳 or 亥.
96 pub branch: Branch,
97 /// Luo Shu palace number containing the horse branch.
98 pub palace: u8,
99}
100
101/// A heavenly-plate stem, including its provenance and center-hosting identity.
102#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
103#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
104pub struct HeavenStem {
105 /// One of the three wonders or six instruments (never visible 甲).
106 pub stem: Stem,
107 /// Earth-plate palace from which the stem was rotated.
108 pub source_palace: u8,
109 /// True only for the palace-five stem travelling with 天禽.
110 pub is_center_hosted: bool,
111}
112
113/// All fixed and moving components of one Luo Shu palace.
114#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
116pub struct Palace {
117 /// Luo Shu number, one through nine; the output array is in this order.
118 pub number: u8,
119 /// Geographic direction (the center has its own value).
120 pub direction: Direction,
121 /// Later Heaven trigram; absent for the center.
122 pub trigram: Option<Trigram>,
123 /// Fixed palace element.
124 pub element: Element,
125 /// Native earth-plate stem, including the actual center stem at palace five.
126 pub earth_stem: Stem,
127 /// Additional center earth stem hosted at 坤二; absent elsewhere.
128 pub hosted_earth_stem: Option<Stem>,
129 /// Rotated heavenly-plate stems; two with 天芮/天禽, none at the center.
130 pub heaven_stems: Vec<HeavenStem>,
131 /// Moving stars; two with 天芮/天禽, none at the center.
132 pub stars: Vec<Star>,
133 /// Moving door; absent at the center.
134 pub door: Option<Door>,
135 /// Moving deity; absent at the center.
136 pub deity: Option<Deity>,
137 /// Hour-pillar void branches contained in this palace; may hold both.
138 pub void_branches: Vec<Branch>,
139 /// Whether the hour-branch travelling horse occupies this palace.
140 pub is_horse: bool,
141}
142
143/// A complete, reproducible, language-independent Qimen chart.
144#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
145#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
146pub struct Chart {
147 /// Structural output-schema version, independent of the crate release.
148 pub schema_version: String,
149 /// The exact validated civil input and its selected calendar conventions.
150 pub input: ChartRequest,
151 /// Four pillars, surrounding solar terms and lunar date.
152 pub calendar: CalendarResult,
153 /// Calculation school and plate method.
154 pub method: Method,
155 /// Explicit center-hosting, star and annotation conventions.
156 pub conventions: Conventions,
157 /// Yang or yin dun.
158 pub dun: Dun,
159 /// Five-day upper, middle or lower yuan.
160 pub yuan: Yuan,
161 /// Formation number, one through nine.
162 pub ju: u8,
163 /// Five-day 甲/己 head (符头) used to select the yuan.
164 pub yuan_head: Cycle,
165 /// Hour-period head, hidden stem and void branches.
166 pub xun: Xun,
167 /// Duty star and door with source and destination palaces.
168 pub leaders: Leaders,
169 /// Hour-branch travelling horse.
170 pub horse: Horse,
171 /// Void branches for each of the four pillars.
172 pub pillar_voids: PillarVoids,
173 /// The nine palaces, always in numeric Luo Shu order, one through nine.
174 pub palaces: [Palace; 9],
175 /// Opt-in annotations and their named calculation rules; absent by default.
176 /// These never alter the base calendar or rotating plates.
177 #[serde(default, skip_serializing_if = "Option::is_none")]
178 pub extensions: Option<crate::ChartExtensions>,
179}
180
181impl Chart {
182 /// Returns the palace with the supplied Luo Shu number, or `None` outside 1–9.
183 #[must_use]
184 pub fn palace(&self, number: u8) -> Option<&Palace> {
185 number
186 .checked_sub(1)
187 .and_then(|index| self.palaces.get(usize::from(index)))
188 }
189}