Skip to main content

financial_api/a_share/
corporate_actions.rs

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