simple_stock_conversion/
simple_stock_conversion.rs

1use tushare_api::{TushareClient, Api, request, TushareEntityList, TushareRequest, params, fields};
2use tushare_api::DeriveFromTushareData;
3
4#[derive(Debug, Clone, DeriveFromTushareData)]
5struct Stock {
6    ts_code: String,
7    symbol: String,
8    name: String,
9}
10
11#[tokio::main]
12async fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Create client (you need to set TUSHARE_TOKEN environment variable)
14    let client = TushareClient::from_env()?;
15
16    // Create request for stock basic data
17    let request = request!(Api::StockBasic, {
18        "list_status" => "L"
19    }, [
20        "ts_code", "symbol", "name"
21    ]);
22
23    // Method 1: Use the generic call_api_as method with TushareEntityList
24    println!("=== Method 1: Using call_api_as with TushareEntityList ===");
25    let stock_list: TushareEntityList<Stock> = client.call_api_as(request.clone()).await?;
26    
27    println!("Found {} stocks:", stock_list.len());
28    for (i, stock) in stock_list.iter().take(5).enumerate() {
29        println!("{}. {} ({}) - {}", i + 1, stock.ts_code, stock.symbol, stock.name);
30    }
31    
32    // Show pagination info
33    println!("Total records: {}", stock_list.count());
34    println!("Has more pages: {}", stock_list.has_more());
35
36    // Method 2: Use the traditional call_api and manual conversion
37    println!("\n=== Method 2: Using call_api + manual conversion ===");
38    let response = client.call_api(request).await?;
39    let stocks: Vec<Stock> = tushare_api::utils::response_to_vec(response)?;
40    
41    println!("Found {} stocks:", stocks.len());
42    for (i, stock) in stocks.iter().take(5).enumerate() {
43        println!("{}. {} ({}) - {}", i + 1, stock.ts_code, stock.symbol, stock.name);
44    }
45
46    Ok(())
47}