1use serde::Serialize;
4
5use crate::aspects::Aspect;
6use crate::chart::Placement;
7use crate::dignities::{element as element_of, traditional_ruler};
8use crate::pyfloat;
9
10type Q = [f64; 4];
12
13fn element_qualities(element: &str) -> Q {
14 match element {
15 "Fire" => [1.0, 0.0, 0.0, 1.0],
16 "Air" => [1.0, 0.0, 1.0, 0.0],
17 "Water" => [0.0, 1.0, 1.0, 0.0],
18 "Earth" => [0.0, 1.0, 0.0, 1.0],
19 _ => [0.0; 4],
20 }
21}
22
23fn planet_qualities(planet: &str) -> Option<Q> {
24 Some(match planet {
25 "Sun" | "Mars" | "Uranus" | "Pluto" => [1.0, 0.0, 0.0, 1.0],
26 "Moon" | "Venus" | "Neptune" => [0.0, 1.0, 1.0, 0.0],
27 "Mercury" | "Saturn" => [0.0, 1.0, 0.0, 1.0],
28 "Jupiter" => [1.0, 0.0, 1.0, 0.0],
29 _ => return None,
30 })
31}
32
33#[derive(Debug, Clone, PartialEq, Serialize)]
35pub struct Qualities {
36 pub hot: f64,
38 pub cold: f64,
40 pub wet: f64,
42 pub dry: f64,
44}
45
46#[derive(Debug, Clone, PartialEq, Serialize)]
48pub struct Scores {
49 pub choleric: f64,
51 pub sanguine: f64,
53 pub phlegmatic: f64,
55 pub melancholic: f64,
57}
58
59#[derive(Debug, Clone, PartialEq, Serialize)]
61pub struct Factor {
62 pub factor: &'static str,
64 pub details: String,
66 pub hot: f64,
68 pub cold: f64,
70 pub wet: f64,
72 pub dry: f64,
74}
75
76#[derive(Debug, Clone, PartialEq, Serialize)]
78pub struct Temperament {
79 pub primary_temperament: &'static str,
81 pub secondary_temperament: &'static str,
83 pub scores: Scores,
85 pub percentages: Scores,
87 pub qualities: Qualities,
89 pub quality_percentages: Qualities,
91 pub breakdown: Vec<Factor>,
93}
94
95pub fn temperament(planets: &[Placement], aspects: &[Aspect]) -> Temperament {
96 let mut totals: Q = [0.0; 4];
97 let mut breakdown = Vec::new();
98 let mut add = |factor: &'static str, details: String, q: Q, weight: f64| {
99 let w = q.map(|v| v * weight);
100 for i in 0..4 {
101 totals[i] += w[i];
102 }
103 breakdown.push(Factor {
104 factor,
105 details,
106 hot: pyfloat::round(w[0], 1),
107 cold: pyfloat::round(w[1], 1),
108 wet: pyfloat::round(w[2], 1),
109 dry: pyfloat::round(w[3], 1),
110 });
111 };
112 let by_name = |name: &str| planets.iter().rev().find(|p| p.name == name);
114 let sun = by_name("Sun");
115 let moon = by_name("Moon");
116
117 if let Some(asc) = by_name("Ascendant") {
118 let element = element_of(asc.sign).unwrap_or("Fire");
119 add(
120 "Ascendant Sign",
121 format!("{} ({element})", asc.sign),
122 element_qualities(element),
123 4.0,
124 );
125 let lord = traditional_ruler(asc.sign).unwrap_or("Mars");
126 if let Some(lord_planet) = by_name(lord) {
127 add(
128 "Lord of Ascendant Nature",
129 lord.to_string(),
130 planet_qualities(lord).unwrap_or_default(),
131 3.0,
132 );
133 let lord_element = element_of(lord_planet.sign).unwrap_or("Fire");
134 add(
135 "Lord of Ascendant Sign",
136 format!("{lord} in {} ({lord_element})", lord_planet.sign),
137 element_qualities(lord_element),
138 3.0,
139 );
140 }
141 }
142
143 if let Some(sun) = sun {
144 let element = element_of(sun.sign).unwrap_or("Fire");
145 add(
146 "Sun Sign",
147 format!("{} ({element})", sun.sign),
148 element_qualities(element),
149 4.0,
150 );
151 let (season, q) = match sun.sign {
152 "Aries" | "Taurus" | "Gemini" => ("Spring (Hot & Wet)", [1.0, 0.0, 1.0, 0.0]),
153 "Cancer" | "Leo" | "Virgo" => ("Summer (Hot & Dry)", [1.0, 0.0, 0.0, 1.0]),
154 "Libra" | "Scorpio" | "Sagittarius" => ("Autumn (Cold & Dry)", [0.0, 1.0, 0.0, 1.0]),
155 _ => ("Winter (Cold & Wet)", [0.0, 1.0, 1.0, 0.0]),
156 };
157 add("Solar Season", season.to_string(), q, 2.0);
158 }
159
160 if let Some(moon) = moon {
161 let element = element_of(moon.sign).unwrap_or("Water");
162 add(
163 "Moon Sign",
164 format!("{} ({element})", moon.sign),
165 element_qualities(element),
166 4.0,
167 );
168 if let Some(sun) = sun {
169 let diff = pyfloat::rem(moon.ecliptic_longitude - sun.ecliptic_longitude, 360.0);
170 let (phase, q) = if diff < 90.0 {
171 ("1st Quarter Waxing (Hot & Wet)", [1.0, 0.0, 1.0, 0.0])
172 } else if diff < 180.0 {
173 ("2nd Quarter Gibbous (Hot & Dry)", [1.0, 0.0, 0.0, 1.0])
174 } else if diff < 270.0 {
175 ("3rd Quarter Full/Waning (Cold & Dry)", [0.0, 1.0, 0.0, 1.0])
176 } else {
177 ("4th Quarter Crescent (Cold & Wet)", [0.0, 1.0, 1.0, 0.0])
178 };
179 add("Lunar Phase", phase.to_string(), q, 2.0);
180 }
181 }
182
183 for p in planets {
184 if matches!(
185 p.name,
186 "Ascendant" | "Midheaven" | "North Node" | "South Node" | "Part of Fortune"
187 ) {
188 continue;
189 }
190 if p.house == 1 {
191 let pq = planet_qualities(p.name).unwrap_or_default();
192 let sq = element_qualities(element_of(p.sign).unwrap_or("Fire"));
193 let combined = [0, 1, 2, 3].map(|i| pq[i] + sq[i]);
194 add(
195 "Planet in House 1",
196 format!("{} in {}", p.name, p.sign),
197 combined,
198 1.0,
199 );
200 }
201 }
202
203 for a in aspects {
204 if a.body1 == "Ascendant" || a.body2 == "Ascendant" {
205 let other = if a.body1 == "Ascendant" {
206 &a.body2
207 } else {
208 &a.body1
209 };
210 if let Some(q) = planet_qualities(other) {
211 add(
212 "Aspect to Ascendant",
213 format!("{other} {} ASC", a.aspect_type),
214 q,
215 1.0,
216 );
217 }
218 }
219 }
220
221 let [hot, cold, wet, dry] = totals;
222 let (choleric, sanguine, phlegmatic, melancholic) =
223 (hot + dry, hot + wet, cold + wet, cold + dry);
224 let total_q = (hot + cold + wet + dry).max(1.0);
225 let total_t = (choleric + sanguine + phlegmatic + melancholic).max(1.0);
226 let pct = |v: f64, total: f64| pyfloat::round((v / total) * 100.0, 1);
227
228 let mut ranked = [
230 ("Choleric", choleric),
231 ("Sanguine", sanguine),
232 ("Phlegmatic", phlegmatic),
233 ("Melancholic", melancholic),
234 ];
235 ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
236
237 Temperament {
238 primary_temperament: ranked[0].0,
239 secondary_temperament: if ranked[1].1 > 0.0 { ranked[1].0 } else { "" },
240 scores: Scores {
241 choleric: pyfloat::round(choleric, 1),
242 sanguine: pyfloat::round(sanguine, 1),
243 phlegmatic: pyfloat::round(phlegmatic, 1),
244 melancholic: pyfloat::round(melancholic, 1),
245 },
246 percentages: Scores {
247 choleric: pct(choleric, total_t),
248 sanguine: pct(sanguine, total_t),
249 phlegmatic: pct(phlegmatic, total_t),
250 melancholic: pct(melancholic, total_t),
251 },
252 qualities: Qualities {
253 hot: pyfloat::round(hot, 1),
254 cold: pyfloat::round(cold, 1),
255 wet: pyfloat::round(wet, 1),
256 dry: pyfloat::round(dry, 1),
257 },
258 quality_percentages: Qualities {
259 hot: pct(hot, total_q),
260 cold: pct(cold, total_q),
261 wet: pct(wet, total_q),
262 dry: pct(dry, total_q),
263 },
264 breakdown,
265 }
266}