59 lines
2.0 KiB
Rust
59 lines
2.0 KiB
Rust
//! `POST /v1/synastry`: two sets of birth data -> two natal charts ->
|
|||
|
|
//! inter-chart aspects, house overlays and the composite chart.
|
||
|
|
|
||
|
|
use astro_engine::{ChartOptions, Synastry, calculate_synastry};
|
||
|
|
use serde::{Deserialize, Serialize};
|
||
|
|
|
||
|
|
use crate::AppState;
|
||
|
|
use crate::error::ApiError;
|
||
|
|
use crate::natal::{self, Meta, NatalRequest, NatalResult};
|
||
|
|
|
||
|
|
#[derive(Debug, Deserialize)]
|
||
|
|
#[serde(deny_unknown_fields)]
|
||
|
|
pub struct SynastryRequest {
|
||
|
|
pub person1: NatalRequest,
|
||
|
|
pub person2: NatalRequest,
|
||
|
|
/// Calculation options for both charts (per-person `options` are not allowed).
|
||
|
|
#[serde(default)]
|
||
|
|
pub options: ChartOptions,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Serialize)]
|
||
|
|
pub struct SynastryResponse {
|
||
|
|
pub person1: NatalResult,
|
||
|
|
pub person2: NatalResult,
|
||
|
|
#[serde(flatten)]
|
||
|
|
pub synastry: Synastry,
|
||
|
|
pub meta: Meta,
|
||
|
|
}
|
||
|
|
|
||
|
|
fn person_chart(
|
||
|
|
state: &AppState,
|
||
|
|
label: &'static str,
|
||
|
|
mut req: NatalRequest,
|
||
|
|
options: ChartOptions,
|
||
|
|
) -> Result<NatalResult, ApiError> {
|
||
|
|
if req.options != ChartOptions::default() {
|
||
|
|
return Err(
|
||
|
|
ApiError::BadRequest("options must be given at the top level, not per person".into()).for_person(label)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
req.options = options;
|
||
|
|
natal::calculate(state, req).map_err(|e| e.for_person(label))
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn calculate(state: &AppState, req: SynastryRequest) -> Result<SynastryResponse, ApiError> {
|
||
|
|
let person1 = person_chart(state, "person1", req.person1, req.options)?;
|
||
|
|
let person2 = person_chart(state, "person2", req.person2, req.options)?;
|
||
|
|
let mut synastry = calculate_synastry(&person1.chart, &person2.chart, &req.options);
|
||
|
|
for (label, p) in [("person1", &person1), ("person2", &person2)] {
|
||
|
|
if !p.time.time_known {
|
||
|
|
synastry.warnings.push(format!(
|
||
|
|
"Birth time of {label} is unknown: its angles are left out of the inter-chart aspects, there are no \
|
||
|
|
house overlays into its chart, and the composite has no angles or houses."
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Ok(SynastryResponse { person1, person2, synastry, meta: natal::meta(state) })
|
||
|
|
}
|