Skip to main content

financial_api/
a_share.rs

1mod auction;
2mod calendar;
3mod corporate_actions;
4mod financials;
5mod prices;
6mod special_data;
7mod valuations;
8
9pub use financials::FinancialRange;
10pub use prices::PriceSnapshotSelection;
11
12use crate::types::TEN_YEARS_MS;
13use crate::{NaturalDate, UnixMillis, ValidationError};
14
15fn validate_millis_window(
16    start: UnixMillis,
17    end: UnixMillis,
18    maximum: Option<i64>,
19) -> Result<(), ValidationError> {
20    if end < start {
21        return Err(ValidationError::new(
22            "end",
23            "must not be earlier than start",
24        ));
25    }
26    if maximum.is_some_and(|maximum| end.get() - start.get() > maximum) {
27        return Err(ValidationError::new(
28            "end",
29            "requested time window exceeds the endpoint limit",
30        ));
31    }
32    Ok(())
33}
34
35fn validate_date_order(
36    start: NaturalDate,
37    end: NaturalDate,
38    end_field: &'static str,
39) -> Result<(), ValidationError> {
40    if end < start {
41        return Err(ValidationError::new(
42            end_field,
43            "must not be earlier than the start date",
44        ));
45    }
46    Ok(())
47}
48
49#[cfg(test)]
50mod tests {
51    use super::{TEN_YEARS_MS, validate_date_order, validate_millis_window};
52    use crate::{NaturalDate, UnixMillis};
53
54    #[test]
55    fn shared_ranges_preserve_endpoint_order_and_limit_rules() {
56        let start = UnixMillis::new(1_000_000_000_000).unwrap();
57        let before_start = UnixMillis::new(start.get() - 1).unwrap();
58        let limit = UnixMillis::new(start.get() + TEN_YEARS_MS).unwrap();
59        let after_limit = UnixMillis::new(limit.get() + 1).unwrap();
60
61        assert!(validate_millis_window(start, before_start, None).is_err());
62        assert!(validate_millis_window(start, limit, Some(TEN_YEARS_MS)).is_ok());
63        assert!(validate_millis_window(start, after_limit, Some(TEN_YEARS_MS)).is_err());
64
65        let first = NaturalDate::parse("2026-08-24").unwrap();
66        let second = NaturalDate::parse("2026-08-25").unwrap();
67        assert!(validate_date_order(first, second, "to").is_ok());
68        let error = validate_date_order(second, first, "to").unwrap_err();
69        assert_eq!(error.field(), "to");
70    }
71}