Skip to main content

qimen_core/
lib.rs

1//! Accurate, typed time-based Chai Bu rotating Qimen Dunjia charts.
2//!
3//! Calendar calculation and plate construction are separated into independent
4//! crates. This crate adds the three wonders/six instruments, nine stars, eight
5//! doors, eight deities, duty star/door, void branches and travelling horse.
6//! Its output records conventions explicitly so another program can reproduce it.
7//!
8//! ```
9//! let request = qimen_core::ChartRequest::new(2026, 9, 18, 14);
10//! let chart = qimen_core::calculate(&request)?;
11//! assert_eq!(chart.palaces.len(), 9);
12//! assert_eq!(chart.schema_version, "1.1");
13//! # Ok::<(), qimen_core::Error>(())
14//! ```
15
16mod engine;
17mod extensions;
18mod model;
19mod request;
20mod symbols;
21
22pub use extensions::*;
23pub use model::{
24    AnnotationBasis, CenterPalaceRule, Chart, Conventions, HeavenStem, Horse, Leaders, Palace,
25    PillarVoids, TianQinRule, Xun,
26};
27pub use qimen_calendar::{
28    Branch, CalendarError, CalendarRequest, CalendarResult, CivilDateTime, Cycle, DayBoundary,
29    FourPillars, LunarDate, SolarTerm, Stem,
30};
31pub use request::{CalculationRequest, Calculator, calculate_with_options};
32pub use symbols::{Deity, Direction, Door, Dun, Element, Method, Star, Trigram, Yuan};
33
34/// Calendar input for the default time-based Chai Bu rotating chart.
35pub type ChartRequest = CalendarRequest;
36
37/// Version of the serialized chart structure.
38pub const SCHEMA_VERSION: &str = "1.1";
39
40/// An invalid calendar request, malformed JSON, or serialization failure.
41#[derive(Debug, thiserror::Error)]
42pub enum Error {
43    /// Invalid Gregorian date, time, year range or UTC offset.
44    #[error(transparent)]
45    Calendar(#[from] CalendarError),
46    /// Invalid request JSON or a failed JSON serialization.
47    #[error(transparent)]
48    Json(#[from] serde_json::Error),
49}
50
51/// Calculates the four pillars and complete nine-palace chart.
52///
53/// The default method is 时家拆补转盘, always hosting the center at 坤二,
54/// with 天禽 following 天芮. Input uses the supplied civil clock and fixed UTC
55/// offset; no implicit longitude correction or daylight-saving lookup occurs.
56/// Invalid input is returned as an error before plate construction.
57pub fn calculate(request: &ChartRequest) -> Result<Chart, Error> {
58    let calendar = qimen_calendar::calculate(request)?;
59    Ok(engine::build(request, calendar))
60}
61
62/// Parses a strict JSON request and returns a compact serialized chart.
63///
64/// Missing `minute` and `second` default to zero, `utc_offset_minutes` to
65/// 480 (UTC+08:00), and `day_boundary` to `"zi_start"`. Unknown request fields
66/// are errors. Optional `extensions` select explicitly named annotation rules;
67/// omitting them leaves every extension disabled. Language bindings use this
68/// function to share the exact schema.
69pub fn calculate_json(request_json: &str) -> Result<String, Error> {
70    let request = serde_json::from_str::<CalculationRequest>(request_json)?;
71    Ok(serde_json::to_string(&request.calculate()?)?)
72}
73
74/// Produces an indented JSON chart for human-readable command-line output.
75pub fn calculate_json_pretty(request_json: &str) -> Result<String, Error> {
76    let request = serde_json::from_str::<CalculationRequest>(request_json)?;
77    Ok(serde_json::to_string_pretty(&request.calculate()?)?)
78}
79
80/// Returns the generated JSON Schema for input, including serde defaults.
81#[cfg(feature = "schema")]
82#[must_use]
83pub fn request_schema() -> schemars::Schema {
84    schemars::schema_for!(CalculationRequest)
85}
86
87/// Returns the generated JSON Schema for the complete versioned chart output.
88#[cfg(feature = "schema")]
89#[must_use]
90pub fn chart_schema() -> schemars::Schema {
91    schemars::schema_for!(Chart)
92}