77 lines
1.9 KiB
Rust
77 lines
1.9 KiB
Rust
use serde::Deserialize;
|
|
use vtse_common::stock::StockSymbol;
|
|
use vtse_common::user::{ApiKey, Password, Username};
|
|
|
|
#[derive(Deserialize, Debug, PartialEq)]
|
|
#[serde(untagged)]
|
|
pub(crate) enum ServerOperation {
|
|
Query(QueryOperation),
|
|
User(UserOperation),
|
|
Market(MarketOperation),
|
|
}
|
|
|
|
#[derive(Deserialize, Debug, PartialEq)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
pub(crate) enum QueryOperation {
|
|
StockInfo { stock: StockSymbol },
|
|
User { username: Username },
|
|
}
|
|
|
|
#[derive(Deserialize, Debug, PartialEq)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
pub(crate) enum UserOperation {
|
|
Login {
|
|
api_key: ApiKey,
|
|
},
|
|
Register {
|
|
username: Username,
|
|
password: Password,
|
|
},
|
|
GetKey {
|
|
username: Username,
|
|
password: Password,
|
|
},
|
|
}
|
|
|
|
#[derive(Deserialize, Debug, PartialEq)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
pub(crate) enum MarketOperation {
|
|
Buy { symbol: StockSymbol, amount: usize },
|
|
Sell { symbol: StockSymbol, amount: usize },
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod deserialize {
|
|
use super::*;
|
|
use serde_json::json;
|
|
use std::str::FromStr;
|
|
|
|
#[test]
|
|
fn server_op_is_transparent() {
|
|
assert_eq!(
|
|
serde_json::from_value::<ServerOperation>(json!({
|
|
"type": "stock_info",
|
|
"stock": "Gura",
|
|
}))
|
|
.unwrap(),
|
|
ServerOperation::Query(QueryOperation::StockInfo {
|
|
stock: StockSymbol::from_str("Gura").unwrap()
|
|
})
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn query_op() {
|
|
assert_eq!(
|
|
serde_json::from_value::<QueryOperation>(json!({
|
|
"type": "stock_info",
|
|
"stock": "Gura",
|
|
}))
|
|
.unwrap(),
|
|
QueryOperation::StockInfo {
|
|
stock: StockSymbol::from_str("Gura").unwrap()
|
|
}
|
|
)
|
|
}
|
|
}
|