Skip to main content

x_iztro/
lib.rs

1//! 紫微斗数排盘核心库,移植自 JS iztro v2.5.8。
2//!
3//! 入口:[`by_solar`] / [`by_lunar`] 排盘,[`get_horoscope`] 计算运限,
4//! [`astrolabe_to_prompt`] / [`horoscope_to_prompt`] 生成 AI 分析文本。
5//! 排盘行为由 [`Config`] 的六个开关控制,默认与 JS iztro 一致。
6
7#![warn(missing_docs)]
8
9/// 绑定层共用的编组与分派。
10///
11/// 排盘入口只被 `python` feature 与 wasm32 目标的绑定文件消费——两者都不在
12/// 默认的本机构建里,故豁免 dead_code;C FFI 只经它走统一查询入口。
13#[allow(dead_code)]
14pub(crate) mod bridge;
15
16/// 排盘与运限主流程
17pub mod astro;
18/// 枚举、常量与星耀数据表
19pub mod data;
20/// 跨语言绑定的序列化 DTO(camelCase + 翻译值 + 语言无关标识)
21pub mod dto;
22/// 排盘入口的错误类型
23pub mod error;
24/// 六语言词表与翻译入口
25pub mod i18n;
26/// 星盘、宫位、星耀与运限的数据结构
27pub mod models;
28/// AI 分析 Prompt 生成
29pub mod prompt;
30/// 安星算法
31pub mod star;
32/// 通用索引工具
33pub mod utils;
34
35#[cfg(feature = "python")]
36mod python;
37
38/// C ABI 导出(供 Go/C 等语言调用)
39#[doc(hidden)]
40pub mod ffi;
41
42#[cfg(target_arch = "wasm32")]
43#[doc(hidden)]
44pub mod wasm;
45
46// Re-export main public API
47pub use astro::builder::{by_lunar, by_solar};
48pub use astro::horoscope::get_horoscope;
49pub use astro::palace::{get_five_elements_class, get_palace_names, get_soul_and_body};
50pub use astro::query::{
51    get_major_star_by_lunar_date, get_major_star_by_solar_date, get_sign_by_lunar_date,
52    get_sign_by_solar_date, get_zodiac_by_solar_date,
53};
54pub use data::stars::StarKey;
55pub use data::types::*;
56pub use error::{BridgeError, IztroError};
57pub use i18n::lookup::{key_of, key_of_in, translate_key};
58pub use i18n::{
59    translate_brightness, translate_earthly_branch, translate_five_elements_class,
60    translate_gender, translate_heavenly_stem, translate_horoscope_name, translate_mutagen,
61    translate_palace, translate_sign, translate_star, translate_time, translate_zodiac,
62};
63pub use models::astrolabe::{
64    Astrolabe, PalaceRef, PalaceTarget, RawChineseDate, RawDates, RawLunarDate, StarRef,
65};
66pub use models::horoscope::{
67    AgeItem, HoroscopeData, HoroscopeItem, HoroscopeRef, YearlyDecStar, YearlyItem,
68};
69pub use models::palace::{Decadal, PalaceData};
70pub use models::star::Star;
71pub use models::surpalaces::SurroundedPalaces;
72pub use prompt::{astrolabe_to_prompt, horoscope_to_prompt};
73
74/// 便捷函数:排盘并返回 JSON
75///
76/// # Errors
77/// 入参非法时返回 [`IztroError`](同 [`by_solar`])。
78pub fn by_solar_json(
79    solar_date: &str,
80    time_index: u8,
81    gender: Gender,
82    fix_leap: bool,
83    language: Language,
84    config: Config,
85) -> Result<String, IztroError> {
86    let astrolabe = by_solar(solar_date, time_index, gender, fix_leap, language, config)?;
87    Ok(serde_json::to_string(&astrolabe.to_dto())
88        .expect("DTO 只含字符串、数值与容器,序列化不会失败"))
89}
90
91/// 便捷函数:农历排盘并返回 JSON
92///
93/// # Errors
94/// 入参非法时返回 [`IztroError`](同 [`by_lunar`])。
95pub fn by_lunar_json(
96    lunar_date: &str,
97    time_index: u8,
98    gender: Gender,
99    leap: LeapMonth,
100    language: Language,
101    config: Config,
102) -> Result<String, IztroError> {
103    let astrolabe = by_lunar(lunar_date, time_index, gender, leap, language, config)?;
104    Ok(serde_json::to_string(&astrolabe.to_dto())
105        .expect("DTO 只含字符串、数值与容器,序列化不会失败"))
106}