vtse/vtse-server/src/operations.rs

77 lines
1.9 KiB
Rust
Raw Normal View History

2021-02-04 01:40:06 +00:00
use serde::Deserialize;
2021-02-09 01:54:00 +00:00
use vtse_common::stock::StockSymbol;
2021-02-09 00:37:19 +00:00
use vtse_common::user::{ApiKey, Password, Username};
2021-02-08 17:33:24 +00:00
2021-02-04 01:40:06 +00:00
#[derive(Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub(crate) enum ServerOperation {
Query(QueryOperation),
2021-02-08 17:33:24 +00:00
User(UserOperation),
Market(MarketOperation),
2021-02-04 01:40:06 +00:00
}
#[derive(Deserialize, Debug, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub(crate) enum QueryOperation {
2021-02-09 01:54:00 +00:00
StockInfo { stock: StockSymbol },
User { username: Username },
2021-02-04 01:40:06 +00:00
}
#[derive(Deserialize, Debug, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
2021-02-08 17:33:24 +00:00
pub(crate) enum UserOperation {
Login {
api_key: ApiKey,
},
Register {
username: Username,
password: Password,
},
GetKey {
username: Username,
password: Password,
},
}
#[derive(Deserialize, Debug, PartialEq)]
2021-02-11 18:55:28 +00:00
#[serde(tag = "type", rename_all = "snake_case")]
2021-02-08 17:33:24 +00:00
pub(crate) enum MarketOperation {
2021-02-09 01:54:00 +00:00
Buy { symbol: StockSymbol, amount: usize },
Sell { symbol: StockSymbol, amount: usize },
2021-02-04 01:40:06 +00:00
}
#[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 {
2021-02-09 01:54:00 +00:00
stock: StockSymbol::from_str("Gura").unwrap()
2021-02-04 01:40:06 +00:00
})
)
}
#[test]
fn query_op() {
assert_eq!(
serde_json::from_value::<QueryOperation>(json!({
"type": "stock_info",
"stock": "Gura",
}))
.unwrap(),
QueryOperation::StockInfo {
2021-02-09 01:54:00 +00:00
stock: StockSymbol::from_str("Gura").unwrap()
2021-02-04 01:40:06 +00:00
}
)
}
}