polyester/codecs/decode/
enums.rs1use crate::proto::orders::v1::{FeeAsset, OrderStatus, OrderType, Side, TimeInForce};
4use buffa::Enumeration;
5
6pub fn enum_proto_name<T: Enumeration>(value: &buffa::EnumValue<T>) -> String {
7 value
8 .as_known()
9 .map(|known| known.proto_name().to_owned())
10 .unwrap_or_else(|| format!("UNKNOWN({})", value.to_i32()))
11}
12
13pub fn order_side_name(side: Side) -> &'static str {
14 match side {
15 Side::Buy => "buy",
16 Side::Sell => "sell",
17 Side::SideUnspecified => "",
18 }
19}
20
21pub fn order_type_name(order_type: OrderType) -> &'static str {
22 match order_type {
23 OrderType::Limit => "limit",
24 OrderType::Market => "market",
25 OrderType::OrderTypeUnspecified => "",
26 }
27}
28
29pub fn time_in_force_name(tif: TimeInForce) -> &'static str {
30 match tif {
31 TimeInForce::Gtc => "gtc",
32 TimeInForce::Ioc => "ioc",
33 TimeInForce::Fok => "fok",
34 TimeInForce::TimeInForceUnspecified => "",
35 }
36}
37
38pub fn order_status_name(status: OrderStatus) -> &'static str {
39 match status {
40 OrderStatus::Pending => "pending",
41 OrderStatus::PendingCancel => "pending_cancel",
42 OrderStatus::Working => "working",
43 OrderStatus::Filled => "filled",
44 OrderStatus::Canceled => "canceled",
45 OrderStatus::Rejected => "rejected",
46 OrderStatus::OrderStatusUnspecified => "",
47 }
48}
49
50pub fn enum_value_side(value: buffa::EnumValue<Side>) -> String {
51 value
52 .as_known()
53 .map(|known| order_side_name(known).to_owned())
54 .unwrap_or_else(|| format!("UNKNOWN({})", value.to_i32()))
55}
56
57pub fn enum_value_order_type(value: buffa::EnumValue<OrderType>) -> String {
58 value
59 .as_known()
60 .map(|known| order_type_name(known).to_owned())
61 .unwrap_or_else(|| format!("UNKNOWN({})", value.to_i32()))
62}
63
64pub fn enum_value_time_in_force(value: buffa::EnumValue<TimeInForce>) -> String {
65 value
66 .as_known()
67 .map(|known| time_in_force_name(known).to_owned())
68 .unwrap_or_else(|| format!("UNKNOWN({})", value.to_i32()))
69}
70
71pub fn enum_value_order_status(value: buffa::EnumValue<OrderStatus>) -> String {
72 value
73 .as_known()
74 .map(|known| order_status_name(known).to_owned())
75 .unwrap_or_else(|| format!("UNKNOWN({})", value.to_i32()))
76}
77
78pub fn enum_value_fee_asset(value: buffa::EnumValue<FeeAsset>) -> String {
79 value
80 .as_known()
81 .map(|known| match known {
82 FeeAsset::Quote => "quote".to_owned(),
83 FeeAsset::Base => "base".to_owned(),
84 FeeAsset::FeeAssetUnspecified => String::new(),
85 })
86 .unwrap_or_else(|| format!("UNKNOWN({})", value.to_i32()))
87}