Skip to main content

stock_rust/
types.rs

1use anyhow::Result as AnyResult;
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4
5pub type Result<T> = AnyResult<T>;
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub struct Stock {
9    pub name: String,
10    pub code: String,
11    pub now: f64,
12    pub low: f64,
13    pub high: f64,
14    pub percent: f64,
15    pub yesterday: f64,
16}
17
18impl Stock {
19    pub fn default_with_code(code: &str) -> Self {
20        Self {
21            name: "---".to_string(),
22            code: code.to_string(),
23            now: 0.0,
24            low: 0.0,
25            high: 0.0,
26            percent: 0.0,
27            yesterday: 0.0,
28        }
29    }
30}
31
32#[async_trait]
33pub trait StockApi {
34    async fn get_stock(&self, code: &str) -> Result<Stock>;
35    async fn get_stocks(&self, codes: &[String]) -> Result<Vec<Stock>>;
36    async fn search_stocks(&self, key: &str) -> Result<Vec<Stock>>;
37}