Expand description
§tushare-rs-pro — Tushare Pro API Rust SDK
面向量化交易和金融数据分析的 Rust 客户端库,提供对 Tushare Pro 全部 API 的异步、类型安全访问。
§核心特性
- 77 个预定义模型 — 覆盖股票、基金、指数、债券、ETF、期货、期权、港股、美股、 外汇、宏观经济、行业分类等 12 大领域,开箱即用
- 84 个 API 枚举 — 编译期类型安全,杜绝 API 名称拼写错误
- Derive 宏 —
#[derive(DeriveFromTushareData)]自动将 API 响应映射到自定义 struct - 异步架构 — 基于
tokio+reqwest,高性能并发请求 - 生产就绪 — 内置限流、指数退避重试、超时控制、全面错误处理
§快速开始
§使用预定义模型(推荐)
use tushare_api::{TushareClient, Api, TushareEntityList, TushareRequest, request, params, fields};
use tushare_api::models::DailyModel;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = TushareClient::from_env()?;
let req = request!(Api::Daily, {
"ts_code" => "000001.SZ",
"start_date" => "20260101",
"end_date" => "20260401"
}, [
"ts_code", "trade_date", "open", "high", "low", "close", "vol"
]);
let data: TushareEntityList<DailyModel> = client.call_api_as(req).await?;
for d in data.iter().take(3) {
println!("{} 收盘: {:.2}", d.trade_date, d.close.unwrap_or(0.0));
}
Ok(())
}§自定义 Struct
use tushare_api::{TushareClient, Api, TushareEntityList, TushareRequest, request, params, fields, DeriveFromTushareData};
#[derive(Debug, Clone, DeriveFromTushareData)]
pub struct MyStock {
pub ts_code: String,
pub name: String,
#[tushare(field = "list_date")]
pub listing_date: Option<String>,
}
let client = TushareClient::from_env()?;
let stocks: TushareEntityList<MyStock> = client.call_api_as(
request!(Api::StockBasic, { "list_status" => "L" }, ["ts_code", "name", "list_date"])
).await?;
println!("共 {} 只股票", stocks.len());§模块概览
| 模块 | 说明 |
|---|---|
client | 核心客户端 TushareClient,支持 from_env() / new() / with_timeout() |
client_ex | 增强客户端 TushareClientEx,带限流和重试 |
models | 77 个预定义数据模型,按领域分 12 个子模块 |
api | Api 枚举(84 个变体),所有 Tushare API 的类型安全标识 |
types | 请求/响应类型、request! 宏、TushareEntityList |
traits | FromTushareData / FromTushareValue 转换 trait |
error | TushareError 错误类型 |
logging | 日志配置 |
Re-exports§
pub use api::Api;pub use client::HttpClientConfig;pub use client_ex::RetryConfig;pub use logging::LogConfig;pub use logging::LogLevel;pub use logging::Logger;pub use utils::response_to_vec;pub use serde_json;
Modules§
- api
- basic_
types - Basic Rust type implementations for FromTushareValue and FromOptionalTushareValue traits
- client
- client_
ex - custom_
date_ format - Custom date format implementations for FromTushareValueWithFormat trait
- error
- logging
- models
- Predefined data models for Tushare API responses.
- third_
party_ types - Third-party type implementations for FromTushareValue and FromOptionalTushareValue traits
- traits
- Traits for Tushare API data conversion
- types
- utils
- Utility functions for working with Tushare API responses
Macros§
- fields
- Macro for creating fields Vec
- params
- Macro for creating parameter HashMap
- request
- More concise builder macro - directly create TushareRequest
Derive Macros§
- Derive
From Tushare Data - Derive macro for automatically implementing FromTushareData trait