use tushare_api::DeriveFromTushareData;
use tushare_api::{Api, TushareClient, TushareEntityList, TushareRequest, fields, params, request};
#[derive(Debug, Clone, DeriveFromTushareData)]
struct Stock {
ts_code: String,
symbol: String,
name: String,
area: Option<String>,
industry: Option<String>,
market: String,
list_date: Option<String>,
}
#[derive(Debug, Clone, DeriveFromTushareData)]
struct StockDaily {
ts_code: String,
trade_date: String,
open: Option<f64>,
high: Option<f64>,
low: Option<f64>,
close: Option<f64>,
pre_close: Option<f64>,
change: Option<f64>,
pct_chg: Option<f64>,
vol: Option<f64>,
amount: Option<f64>,
}
#[derive(Debug, Clone, DeriveFromTushareData)]
struct SimpleStock {
ts_code: String,
symbol: String,
name: String,
}
#[derive(Debug, Clone, DeriveFromTushareData)]
struct Fund {
ts_code: String,
name: String,
management: Option<String>,
custodian: Option<String>,
fund_type: Option<String>,
found_date: Option<String>,
due_date: Option<String>,
list_date: Option<String>,
issue_date: Option<String>,
delist_date: Option<String>,
status: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = TushareClient::from_env()?;
println!("=== 示例1: 使用 FromTushareData 派生宏获取股票基本信息 ===");
let request = request!(Api::StockBasic, {
"list_status" => "L"
}, [
"ts_code", "symbol", "name", "area", "industry", "market", "list_date"
]);
let stock_list: TushareEntityList<Stock> = client.call_api_as(request).await?;
println!("找到 {} 只股票:", stock_list.len());
for (i, stock) in stock_list.iter().take(5).enumerate() {
println!(
"{}. {} ({}) - {} [{}] {}",
i + 1,
stock.ts_code,
stock.symbol,
stock.name,
stock.area.as_deref().unwrap_or("未知"),
stock.industry.as_deref().unwrap_or("未知行业")
);
}
println!(
"总记录数: {}, 是否有更多页面: {}",
stock_list.count(),
stock_list.has_more()
);
println!("\n=== 示例2: 获取简单股票信息 ===");
let simple_request = request!(Api::StockBasic, {
"list_status" => "L"
}, [
"ts_code", "symbol", "name"
]);
let simple_stock_list: TushareEntityList<SimpleStock> =
client.call_api_as(simple_request).await?;
println!("找到 {} 只股票 (简化版):", simple_stock_list.len());
for (i, stock) in simple_stock_list.iter().take(3).enumerate() {
println!(
"{}. {} ({}) - {}",
i + 1,
stock.ts_code,
stock.symbol,
stock.name
);
}
println!("\n=== 示例3: 获取基金信息 ===");
let fund_request = request!(Api::FundBasic, {
"market" => "E"
}, [
"ts_code", "name", "management", "custodian", "fund_type", "found_date", "list_date", "status"
]);
let fund_list: TushareEntityList<Fund> = client.call_api_as(fund_request).await?;
println!("找到 {} 只基金:", fund_list.len());
for (i, fund) in fund_list.iter().take(3).enumerate() {
println!(
"{}. {} - {} [{}] 管理人: {}",
i + 1,
fund.ts_code,
fund.name,
fund.fund_type.as_deref().unwrap_or("未知类型"),
fund.management.as_deref().unwrap_or("未知")
);
}
Ok(())
}