financial_api/a_share/
corporate_actions.rs1use crate::endpoints;
2use crate::{
3 AShareCode, AdjustmentFactorsData, Client, Error, NaturalDate, OptionalInput, Response,
4 ValidationError,
5};
6
7use super::validate_date_order;
8
9impl Client {
10 #[doc = concat!(
14 "```no_run\n",
15 include_str!("../../examples/corp_actions_adjustment_factors.rs"),
16 "\n```"
17 )]
18 pub async fn corp_actions_adjustment_factors(
19 &self,
20 thscode: impl TryInto<AShareCode, Error: Into<ValidationError>>,
21 from: impl TryInto<OptionalInput<NaturalDate>, Error: Into<ValidationError>>,
22 to: impl TryInto<OptionalInput<NaturalDate>, Error: Into<ValidationError>>,
23 ) -> Result<Response<AdjustmentFactorsData>, Error> {
24 let thscode: AShareCode = thscode.try_into().map_err(Into::into)?;
25 let from: OptionalInput<NaturalDate> = from.try_into().map_err(Into::into)?;
26 let to: OptionalInput<NaturalDate> = to.try_into().map_err(Into::into)?;
27 let from = from.into_inner();
28 let to = to.into_inner();
29 let mut query = vec![("thscode", thscode.into_string())];
30 if let (Some(from), Some(to)) = (from, to) {
31 validate_date_order(from, to, "to")?;
32 }
33 if let Some(from) = from {
34 query.push(("from", from.to_string()));
35 }
36 if let Some(to) = to {
37 query.push(("to", to.to_string()));
38 }
39 self.get(endpoints::ADJUSTMENT_FACTORS, &query).await
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46 use crate::ApiKey;
47
48 #[tokio::test]
49 async fn adjustment_factors_rejects_reversed_dates_before_transport() {
50 let client = Client::builder(ApiKey::new("test-api-key").unwrap())
51 .base_url("http://127.0.0.1:9")
52 .build()
53 .unwrap();
54 let stock = AShareCode::new("600519.SH").unwrap();
55 let from = NaturalDate::parse("2026-08-25").unwrap();
56 let to = NaturalDate::parse("2026-08-24").unwrap();
57
58 let error = client
59 .corp_actions_adjustment_factors(&stock, Some(from), Some(to))
60 .await
61 .unwrap_err();
62
63 let Error::InvalidInput(error) = error else {
64 panic!("expected an invalid corporate-action date range");
65 };
66 assert_eq!(error.field(), "to");
67 }
68}