financial_api/fund/
market.rs1use crate::endpoints;
2use crate::{
3 Client, Error, FundMarketHistoricalData, FundMarketSnapshotData, Response, Thscode, UnixMillis,
4 ValidationError,
5};
6
7use super::validate_history_range;
8
9impl Client {
10 #[doc = concat!(
14 "```no_run\n",
15 include_str!("../../examples/fund_market_snapshot.rs"),
16 "\n```"
17 )]
18 pub async fn fund_market_snapshot(
19 &self,
20 thscode: impl TryInto<Thscode, Error: Into<ValidationError>>,
21 ) -> Result<Response<FundMarketSnapshotData>, Error> {
22 let thscode: Thscode = thscode.try_into().map_err(Into::into)?;
23 validate_exchange_fund(&thscode)?;
24 self.get(
25 endpoints::FUND_MARKET_SNAPSHOT,
26 &[("thscode", thscode.as_str())],
27 )
28 .await
29 }
30
31 #[doc = concat!(
35 "```no_run\n",
36 include_str!("../../examples/fund_market_historical.rs"),
37 "\n```"
38 )]
39 pub async fn fund_market_historical(
40 &self,
41 thscode: impl TryInto<Thscode, Error: Into<ValidationError>>,
42 start: impl TryInto<UnixMillis, Error: Into<ValidationError>>,
43 end: impl TryInto<UnixMillis, Error: Into<ValidationError>>,
44 ) -> Result<Response<FundMarketHistoricalData>, Error> {
45 let thscode: Thscode = thscode.try_into().map_err(Into::into)?;
46 let start: UnixMillis = start.try_into().map_err(Into::into)?;
47 let end: UnixMillis = end.try_into().map_err(Into::into)?;
48 validate_exchange_fund(&thscode)?;
49 validate_history_range(start, end)?;
50 let query = [
51 ("thscode", thscode.into_string()),
52 ("interval", "1d".to_owned()),
53 ("start", start.to_string()),
54 ("end", end.to_string()),
55 ];
56 self.get(endpoints::FUND_MARKET_HISTORICAL, &query).await
57 }
58}
59
60fn validate_exchange_fund(thscode: &Thscode) -> Result<(), ValidationError> {
61 let (ticker, suffix) = thscode.ticker_and_suffix();
62 let valid = ticker.len() == 6
63 && ticker.bytes().all(|byte| byte.is_ascii_digit())
64 && matches!(suffix, "SH" | "SZ");
65 if !valid {
66 return Err(ValidationError::new(
67 "thscode",
68 "exchange fund code must be six digits ending in SH or SZ",
69 ));
70 }
71 Ok(())
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::ApiKey;
78
79 #[test]
80 fn exchange_market_data_requires_a_six_digit_exchange_code() {
81 assert!(validate_exchange_fund(&Thscode::new("510300.SH").unwrap()).is_ok());
82 assert!(validate_exchange_fund(&Thscode::new("159915.SZ").unwrap()).is_ok());
83
84 for invalid in ["025480.OF", "ABCDEF.SH", "51030.SH", "510300.BJ"] {
85 let error = validate_exchange_fund(&Thscode::new(invalid).unwrap()).unwrap_err();
86 assert_eq!(error.field(), "thscode");
87 }
88 }
89
90 #[tokio::test]
91 async fn snapshot_keeps_exchange_validation_on_its_call_edge() {
92 let client = Client::builder(ApiKey::new("test-api-key").unwrap())
93 .base_url("http://127.0.0.1:9")
94 .build()
95 .unwrap();
96 let otc_fund = Thscode::new("025480.OF").unwrap();
97
98 let error = client.fund_market_snapshot(&otc_fund).await.unwrap_err();
99
100 assert!(matches!(error, Error::InvalidInput(_)));
101 }
102}