1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use crate::errors::{map_custom_definition_error, EphemerisError};
use crate::metadata::BackendMetadata;
use core::fmt;
use pleiades_types::{
Apparentness, CelestialBody, CoordinateFrame, Instant, ObserverLocation, TimeScale,
TimeScaleConversion, TimeScaleConversionError, ZodiacMode,
};
use std::time::Duration;
/// A backend request.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct EphemerisRequest {
/// Requested body.
pub body: CelestialBody,
/// Requested instant.
pub instant: Instant,
/// Optional observer location for topocentric calculations.
pub observer: Option<ObserverLocation>,
/// Requested coordinate frame.
pub frame: CoordinateFrame,
/// Requested zodiac mode.
pub zodiac_mode: ZodiacMode,
/// Whether apparent or mean values are preferred.
pub apparent: Apparentness,
}
impl EphemerisRequest {
/// Creates a new request with sensible defaults for a tropical geocentric query.
///
/// The default apparentness is mean geometric output so a bare request stays
/// compatible with the current mean-only first-party backends.
///
/// # Example
///
/// ```
/// use pleiades_backend::{Apparentness, EphemerisRequest};
/// use pleiades_types::{CelestialBody, CoordinateFrame, Instant, JulianDay, TimeScale, ZodiacMode};
///
/// let request = EphemerisRequest::new(
/// CelestialBody::Mars,
/// Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt),
/// );
///
/// assert_eq!(request.frame, CoordinateFrame::Ecliptic);
/// assert_eq!(request.zodiac_mode, ZodiacMode::Tropical);
/// assert_eq!(request.apparent, Apparentness::Mean);
/// assert!(request.observer.is_none());
/// ```
pub fn new(body: CelestialBody, instant: Instant) -> Self {
Self {
body,
instant,
observer: None,
frame: CoordinateFrame::Ecliptic,
zodiac_mode: ZodiacMode::Tropical,
apparent: Apparentness::Mean,
}
}
/// Returns a compact one-line rendering of the request shape.
pub fn summary_line(&self) -> String {
let observer = self
.observer
.as_ref()
.map(ToString::to_string)
.unwrap_or_else(|| "geocentric".to_string());
format!(
"body={}; instant={}; frame={}; zodiac={}; apparent={}; observer={}",
self.body, self.instant, self.frame, self.zodiac_mode, self.apparent, observer
)
}
/// Validates any custom request identifiers embedded in this request.
///
/// Built-in bodies and sidereal labels are always accepted. Custom bodies
/// and custom ayanamsas are validated through their structured descriptor
/// records so malformed user-defined entries fail before request dispatch.
pub fn validate_custom_definitions(&self) -> Result<(), EphemerisError> {
self.body
.validate()
.map_err(|error| map_custom_definition_error("request body", error))?;
if let ZodiacMode::Sidereal { ayanamsa } = &self.zodiac_mode {
ayanamsa
.validate()
.map_err(|error| map_custom_definition_error("sidereal ayanamsa", error))?;
}
Ok(())
}
/// Replaces the request instant with a caller-supplied offset.
///
/// This is the backend-level counterpart to [`Instant::with_time_scale_offset`].
/// It preserves the rest of the request shape while letting direct backend
/// callers stage explicit Delta T or TDB offsets before dispatch.
pub fn with_instant_time_scale_offset(
mut self,
target_scale: TimeScale,
offset_seconds: f64,
) -> Self {
self.instant = self
.instant
.with_time_scale_offset(target_scale, offset_seconds);
self
}
/// Replaces the request instant with a caller-supplied offset after validation.
///
/// This is the checked counterpart to [`EphemerisRequest::with_instant_time_scale_offset`].
/// It rejects non-finite offsets and mismatched source scales before the
/// request is retagged, which keeps the backend-level convenience available
/// in a release-grade form.
///
/// # Example
///
/// ```
/// use pleiades_backend::EphemerisRequest;
/// use pleiades_types::{CelestialBody, Instant, JulianDay, TimeScale};
///
/// let request = EphemerisRequest::new(
/// CelestialBody::Sun,
/// Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1),
/// );
/// let converted = request
/// .with_instant_time_scale_offset_checked(TimeScale::Tt, 64.184)
/// .expect("validated backend offset");
///
/// assert_eq!(converted.instant.scale, TimeScale::Tt);
/// ```
pub fn with_instant_time_scale_offset_checked(
mut self,
target_scale: TimeScale,
offset_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self
.instant
.with_time_scale_offset_checked(target_scale, offset_seconds)?;
Ok(self)
}
/// Applies a caller-supplied time-scale conversion policy to the request instant.
///
/// This is the generic counterpart to the source-specific offset helpers.
/// It keeps the explicit source, target, and offset choice available as a
/// typed policy object while preserving the rest of the request shape.
pub fn with_time_scale_conversion(
mut self,
conversion: TimeScaleConversion,
) -> Result<Self, TimeScaleConversionError> {
self.instant = conversion.apply(self.instant)?;
Ok(self)
}
/// Converts the request instant from TT to TDB using a caller-supplied offset.
///
/// This is the backend-level counterpart to [`Instant::tdb_from_tt`].
pub fn with_tdb_from_tt(mut self, offset: Duration) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tdb_from_tt(offset)?;
Ok(self)
}
/// Converts the request instant from TT to TDB using a caller-supplied signed offset.
///
/// This is the backend-level counterpart to [`Instant::tdb_from_tt_signed`].
pub fn with_tdb_from_tt_signed(
mut self,
offset_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tdb_from_tt_signed(offset_seconds)?;
Ok(self)
}
/// Converts the request instant from TDB to TT using a caller-supplied offset.
///
/// This is the backend-level counterpart to [`Instant::tt_from_tdb`].
pub fn with_tt_from_tdb(
mut self,
offset_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tt_from_tdb(offset_seconds)?;
Ok(self)
}
/// Converts the request instant from TDB to TT using a caller-supplied signed offset.
///
/// This is the backend-level counterpart to [`Instant::tt_from_tdb_signed`].
pub fn with_tt_from_tdb_signed(
mut self,
offset_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tt_from_tdb_signed(offset_seconds)?;
Ok(self)
}
/// Converts the request instant from UT1 to TT using a caller-supplied offset.
///
/// This is the backend-level counterpart to [`Instant::tt_from_ut1`].
pub fn with_tt_from_ut1(mut self, delta_t: Duration) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tt_from_ut1(delta_t)?;
Ok(self)
}
/// Converts the request instant from UT1 to TT using a caller-supplied signed offset.
///
/// This is the backend-level counterpart to [`Instant::tt_from_ut1_signed`].
pub fn with_tt_from_ut1_signed(
mut self,
delta_t_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tt_from_ut1_signed(delta_t_seconds)?;
Ok(self)
}
/// Converts the request instant from UTC to TT using a caller-supplied offset.
///
/// This is the backend-level counterpart to [`Instant::tt_from_utc`].
pub fn with_tt_from_utc(mut self, delta_t: Duration) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tt_from_utc(delta_t)?;
Ok(self)
}
/// Converts the request instant from UTC to TT using a caller-supplied signed offset.
///
/// This is the backend-level counterpart to [`Instant::tt_from_utc_signed`].
pub fn with_tt_from_utc_signed(
mut self,
delta_t_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tt_from_utc_signed(delta_t_seconds)?;
Ok(self)
}
/// Converts the request instant from UT1 to TDB using caller-supplied TT-UT1 and TDB-TT offsets.
///
/// This is the backend-level counterpart to [`Instant::tdb_from_ut1`].
pub fn with_tdb_from_ut1(
mut self,
tt_offset: Duration,
tdb_offset: Duration,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tdb_from_ut1(tt_offset, tdb_offset)?;
Ok(self)
}
/// Converts the request instant from UT1 to TDB using caller-supplied TT-UT1 and signed TDB-TT offsets.
///
/// This is the backend-level counterpart to [`Instant::tdb_from_ut1_signed`].
pub fn with_tdb_from_ut1_signed(
mut self,
tt_offset: Duration,
tdb_offset_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self
.instant
.tdb_from_ut1_signed(tt_offset, tdb_offset_seconds)?;
Ok(self)
}
/// Converts the request instant from UTC to TDB using caller-supplied TT-UTC and TDB-TT offsets.
///
/// This is the backend-level counterpart to [`Instant::tdb_from_utc`].
pub fn with_tdb_from_utc(
mut self,
tt_offset: Duration,
tdb_offset: Duration,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self.instant.tdb_from_utc(tt_offset, tdb_offset)?;
Ok(self)
}
/// Converts the request instant from UTC to TDB using caller-supplied TT-UTC and signed TDB-TT offsets.
///
/// This is the backend-level counterpart to [`Instant::tdb_from_utc_signed`].
pub fn with_tdb_from_utc_signed(
mut self,
tt_offset: Duration,
tdb_offset_seconds: f64,
) -> Result<Self, TimeScaleConversionError> {
self.instant = self
.instant
.tdb_from_utc_signed(tt_offset, tdb_offset_seconds)?;
Ok(self)
}
/// Validates a caller-supplied time-scale conversion policy without mutating the request.
///
/// This mirrors [`TimeScaleConversion::validate`] at the backend-request
/// layer so direct backend callers can preflight the explicit source/target/
/// offset contract before choosing whether to apply it.
///
/// # Example
///
/// ```
/// use pleiades_backend::EphemerisRequest;
/// use pleiades_types::{CelestialBody, Instant, JulianDay, TimeScale, TimeScaleConversion};
///
/// let request = EphemerisRequest::new(
/// CelestialBody::Sun,
/// Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Utc),
/// );
/// let policy = TimeScaleConversion::new(TimeScale::Utc, TimeScale::Tt, 64.184);
///
/// assert!(request.validate_time_scale_conversion(policy).is_ok());
/// ```
pub fn validate_time_scale_conversion(
&self,
conversion: TimeScaleConversion,
) -> Result<(), TimeScaleConversionError> {
self.instant.validate_time_scale_conversion(conversion)
}
/// Validates this request against backend metadata.
///
/// This is the method-form counterpart to [`crate::validate_request_against_metadata`].
/// It keeps direct backend callers from having to import the free helper when
/// they want to preflight a request before dispatch.
pub fn validate_against_metadata(
&self,
metadata: &BackendMetadata,
) -> Result<(), EphemerisError> {
crate::policy::current::validate_request_against_metadata(self, metadata)
}
}
impl fmt::Display for EphemerisRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.summary_line())
}
}
#[cfg(test)]
#[path = "request_tests.rs"]
mod tests;