Skip to main content

financial_api/fund/
holders.rs

1use crate::endpoints;
2use crate::{
3    Client, Error, FundHoldersData, FundTopHoldersData, FundType, HolderMergeScope, Response,
4    Thscode, ValidationError,
5};
6
7use super::FundTarget;
8
9impl Client {
10    /// 获取基金持有人结构。
11    ///
12    /// # 示例
13    #[doc = concat!(
14        "```no_run\n",
15        include_str!("../../examples/fund_holders_detail.rs"),
16        "\n```"
17    )]
18    pub async fn fund_holders_detail(
19        &self,
20        fund_type: FundType,
21        thscode: &Thscode,
22        merge_scope: HolderMergeScope,
23    ) -> Result<Response<FundHoldersData>, Error> {
24        let mut query = FundTarget::new(fund_type, thscode)?.query();
25        query.push(("merge_scope", merge_scope.to_string()));
26        self.get(endpoints::FUND_HOLDERS_DETAIL, &query).await
27    }
28
29    /// 获取最多十名主要基金持有人。
30    ///
31    /// # 示例
32    #[doc = concat!(
33        "```no_run\n",
34        include_str!("../../examples/fund_holders_top.rs"),
35        "\n```"
36    )]
37    pub async fn fund_holders_top(
38        &self,
39        fund_type: FundType,
40        thscode: &Thscode,
41        limit: Option<u8>,
42    ) -> Result<Response<FundTopHoldersData>, Error> {
43        let target = FundTarget::new(fund_type, thscode)?;
44        if limit.is_some_and(|limit| limit == 0 || limit > 10) {
45            return Err(ValidationError::new("limit", "must be in the range 1..=10").into());
46        }
47        let mut query = target.query();
48        if let Some(limit) = limit {
49            query.push(("limit", limit.to_string()));
50        }
51        self.get(endpoints::FUND_HOLDERS_TOP, &query).await
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use crate::ApiKey;
59
60    #[tokio::test]
61    async fn top_holders_limit_is_one_through_ten() {
62        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
63            .base_url("http://127.0.0.1:9")
64            .build()
65            .unwrap();
66        let fund = Thscode::new("025480.OF").unwrap();
67
68        for limit in [0, 11] {
69            let error = client
70                .fund_holders_top(FundType::Otc, &fund, Some(limit))
71                .await
72                .unwrap_err();
73            assert!(matches!(error, Error::InvalidInput(_)));
74        }
75    }
76}