macro_conversion_example/
macro_conversion_example.rs1use tushare_api::{TushareClient, Api, request, TushareEntityList, TushareRequest, params, fields};
2use tushare_api::DeriveFromTushareData;
3
4#[derive(Debug, Clone, DeriveFromTushareData)]
6struct Stock {
7 ts_code: String,
9 symbol: String,
11 name: String,
13 area: Option<String>,
15 industry: Option<String>,
17 market: String,
19 list_date: Option<String>,
21}
22
23#[derive(Debug, Clone, DeriveFromTushareData)]
25struct StockDaily {
26 ts_code: String,
28 trade_date: String,
30 open: Option<f64>,
32 high: Option<f64>,
34 low: Option<f64>,
36 close: Option<f64>,
38 pre_close: Option<f64>,
40 change: Option<f64>,
42 pct_chg: Option<f64>,
44 vol: Option<f64>,
46 amount: Option<f64>,
48}
49
50#[derive(Debug, Clone, DeriveFromTushareData)]
52struct SimpleStock {
53 ts_code: String,
54 symbol: String,
55 name: String,
56}
57
58#[derive(Debug, Clone, DeriveFromTushareData)]
60struct Fund {
61 ts_code: String,
63 name: String,
65 management: Option<String>,
67 custodian: Option<String>,
69 fund_type: Option<String>,
71 found_date: Option<String>,
73 due_date: Option<String>,
75 list_date: Option<String>,
77 issue_date: Option<String>,
79 delist_date: Option<String>,
81 status: Option<String>,
83}
84
85#[tokio::main]
86async fn main() -> Result<(), Box<dyn std::error::Error>> {
87 let client = TushareClient::from_env()?;
89
90 println!("=== 示例1: 使用 FromTushareData 派生宏获取股票基本信息 ===");
91
92 let request = request!(Api::StockBasic, {
93 "list_status" => "L"
94 }, [
95 "ts_code", "symbol", "name", "area", "industry", "market", "list_date"
96 ]);
97
98 let stock_list: TushareEntityList<Stock> = client.call_api_as(request).await?;
100
101 println!("找到 {} 只股票:", stock_list.len());
102 for (i, stock) in stock_list.iter().take(5).enumerate() {
103 println!("{}. {} ({}) - {} [{}] {}",
104 i + 1,
105 stock.ts_code,
106 stock.symbol,
107 stock.name,
108 stock.area.as_deref().unwrap_or("未知"),
109 stock.industry.as_deref().unwrap_or("未知行业")
110 );
111 }
112
113 println!("总记录数: {}, 是否有更多页面: {}", stock_list.count(), stock_list.has_more());
115
116 println!("\n=== 示例2: 获取简单股票信息 ===");
117
118 let simple_request = request!(Api::StockBasic, {
119 "list_status" => "L"
120 }, [
121 "ts_code", "symbol", "name"
122 ]);
123
124 let simple_stock_list: TushareEntityList<SimpleStock> = client.call_api_as(simple_request).await?;
125
126 println!("找到 {} 只股票 (简化版):", simple_stock_list.len());
127 for (i, stock) in simple_stock_list.iter().take(3).enumerate() {
128 println!("{}. {} ({}) - {}", i + 1, stock.ts_code, stock.symbol, stock.name);
129 }
130
131 println!("\n=== 示例3: 获取基金信息 ===");
132
133 let fund_request = request!(Api::FundBasic, {
134 "market" => "E"
135 }, [
136 "ts_code", "name", "management", "custodian", "fund_type", "found_date", "list_date", "status"
137 ]);
138
139 let fund_list: TushareEntityList<Fund> = client.call_api_as(fund_request).await?;
140
141 println!("找到 {} 只基金:", fund_list.len());
142 for (i, fund) in fund_list.iter().take(3).enumerate() {
143 println!("{}. {} - {} [{}] 管理人: {}",
144 i + 1,
145 fund.ts_code,
146 fund.name,
147 fund.fund_type.as_deref().unwrap_or("未知类型"),
148 fund.management.as_deref().unwrap_or("未知")
149 );
150 }
151
152 Ok(())
153}