stock_conversion_example/
stock_conversion_example.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    // Use the new generic method to get TushareEntityList<Stock> directly
24    let stock_list: TushareEntityList<Stock> = client.call_api_as(request).await?;
25
26    // Display the results
27    println!("Found {} stocks:", stock_list.len());
28    for (i, stock) in stock_list.iter().take(10).enumerate() {
29        println!("{}. {} ({}) - {}", i + 1, stock.ts_code, stock.symbol, stock.name);
30    }
31
32    if stock_list.len() > 10 {
33        println!("... and {} more stocks", stock_list.len() - 10);
34    }
35    
36    // Show pagination info
37    println!("Total records: {}", stock_list.count());
38    println!("Has more pages: {}", stock_list.has_more());
39
40    Ok(())
41}