#![no_std]
extern crate alloc;
use alloc::string::String;
use swiss_eph;
use wasm_bindgen::prelude::*;
use serde::{Deserialize, Serialize};
pub(crate) use swiss_eph as swe_bindings;
pub mod astronomy;
pub mod vedic;
pub mod geo;
pub mod muhurat;
pub mod constants;
#[wasm_bindgen]
pub fn get_version() -> String {
String::from(env!("CARGO_PKG_VERSION"))
}
#[wasm_bindgen]
pub fn get_swisseph_version() -> String {
let mut buf = [0i8; 256];
unsafe {
swe_bindings::swe_version(buf.as_mut_ptr());
}
let c_str = unsafe { core::ffi::CStr::from_ptr(buf.as_ptr()) };
String::from(c_str.to_str().unwrap_or("Unknown"))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[wasm_bindgen]
pub struct Location {
pub latitude: f64,
pub longitude: f64,
pub altitude: f64,
}
#[wasm_bindgen]
impl Location {
#[wasm_bindgen(constructor)]
pub fn new(latitude: f64, longitude: f64, altitude: f64) -> Self {
Self { latitude, longitude, altitude }
}
}
#[wasm_bindgen]
pub fn calculate_sunrise(year: i32, month: u32, day: u32, location: &Location) -> f64 {
geo::sunrise_sunset::calculate_sunrise(year, month, day, location.latitude, location.longitude, location.altitude)
}
#[wasm_bindgen]
pub fn calculate_sunset(year: i32, month: u32, day: u32, location: &Location) -> f64 {
geo::sunrise_sunset::calculate_sunset(year, month, day, location.latitude, location.longitude, location.altitude)
}
pub use astronomy::planets::PlanetData;
#[wasm_bindgen]
pub fn calculate_houses(
jd: f64,
lat: f64,
lon: f64,
hsys: char,
ayan_mode: i32
) -> Result<astronomy::houses::HouseInfo, JsValue> {
let mode = if ayan_mode < 0 {
None
} else {
Some(astronomy::ayanamsha::AyanamshaMode::from_i32(ayan_mode))
};
astronomy::houses::calculate_houses(jd, lat, lon, hsys, mode)
}
#[wasm_bindgen]
pub fn calculate_planets(jd: f64, ayan_mode: i32) -> Result<JsValue, JsValue> {
let mode = astronomy::ayanamsha::AyanamshaMode::from_i32(ayan_mode);
let ayan_val = astronomy::ayanamsha::get_ayanamsha(mode, jd);
let planets = astronomy::planets::get_planet_positions_bulk(jd, ayan_val);
Ok(serde_wasm_bindgen::to_value(&planets)?)
}
#[wasm_bindgen]
pub fn calculate_vimshottari(moon_long: f64, birth_time_ms: f64, current_time_ms: f64) -> vedic::dasha::DashaInfo {
vedic::dasha::calculate_vimshottari(moon_long, birth_time_ms, current_time_ms)
}
#[wasm_bindgen]
pub fn p_julday(year: i32, month: i32, day: i32, hour: f64, gregflag: i32) -> f64 {
unsafe {
swe_bindings::swe_julday(year, month, day, hour, gregflag)
}
}
#[derive(Serialize)]
pub struct PlanetaryPosition {
pub longitude: f64,
pub latitude: f64,
pub distance: f64,
pub speed_long: f64,
pub speed_lat: f64,
pub speed_dist: f64,
}
#[wasm_bindgen]
pub fn p_calc_ut(tjd_ut: f64, ipl: i32, iflag: i32) -> Result<JsValue, JsValue> {
let mut xx = [0.0; 6];
let mut serr = [0i8; 256];
unsafe {
let ret_flag = swe_bindings::swe_calc_ut(tjd_ut, ipl, iflag, xx.as_mut_ptr(), serr.as_mut_ptr());
if ret_flag < 0 {
let c_str = core::ffi::CStr::from_ptr(serr.as_ptr());
return Err(JsValue::from_str(c_str.to_str().unwrap_or("Unknown error")));
}
}
let result = PlanetaryPosition {
longitude: xx[0],
latitude: xx[1],
distance: xx[2],
speed_long: xx[3],
speed_lat: xx[4],
speed_dist: xx[5],
};
Ok(serde_wasm_bindgen::to_value(&result)?)
}