macro_conversion_example/
macro_conversion_example.rs

1use tushare_api::{TushareClient, Api, request, TushareEntityList, TushareRequest, params, fields};
2use tushare_api::DeriveFromTushareData;
3
4/// 股票基本信息
5#[derive(Debug, Clone, DeriveFromTushareData)]
6struct Stock {
7    /// 股票代码
8    ts_code: String,
9    /// 股票简称
10    symbol: String,
11    /// 股票名称
12    name: String,
13    /// 地区
14    area: Option<String>,
15    /// 行业
16    industry: Option<String>,
17    /// 市场类型
18    market: String,
19    /// 上市日期
20    list_date: Option<String>,
21}
22
23/// 股票日线数据
24#[derive(Debug, Clone, DeriveFromTushareData)]
25struct StockDaily {
26    /// 股票代码
27    ts_code: String,
28    /// 交易日期
29    trade_date: String,
30    /// 开盘价
31    open: Option<f64>,
32    /// 最高价
33    high: Option<f64>,
34    /// 最低价
35    low: Option<f64>,
36    /// 收盘价
37    close: Option<f64>,
38    /// 昨收价
39    pre_close: Option<f64>,
40    /// 涨跌额
41    change: Option<f64>,
42    /// 涨跌幅
43    pct_chg: Option<f64>,
44    /// 成交量
45    vol: Option<f64>,
46    /// 成交额
47    amount: Option<f64>,
48}
49
50/// 简单股票信息
51#[derive(Debug, Clone, DeriveFromTushareData)]
52struct SimpleStock {
53    ts_code: String,
54    symbol: String,
55    name: String,
56}
57
58/// 基金基本信息
59#[derive(Debug, Clone, DeriveFromTushareData)]
60struct Fund {
61    /// 基金代码
62    ts_code: String,
63    /// 基金简称
64    name: String,
65    /// 管理人
66    management: Option<String>,
67    /// 托管人
68    custodian: Option<String>,
69    /// 基金类型
70    fund_type: Option<String>,
71    /// 成立日期
72    found_date: Option<String>,
73    /// 到期日期
74    due_date: Option<String>,
75    /// 上市日期
76    list_date: Option<String>,
77    /// 发行日期
78    issue_date: Option<String>,
79    /// 退市日期
80    delist_date: Option<String>,
81    /// 基金状态
82    status: Option<String>,
83}
84
85#[tokio::main]
86async fn main() -> Result<(), Box<dyn std::error::Error>> {
87    // 创建客户端
88    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    // 直接获取 TushareEntityList<Stock> 类型
99    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    // 显示分页信息
114    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}