response_base.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use serde_json::Value;
  2. use tokio::time::Instant;
  3. /**交易所返回数据处理之后,同意保存格式,为了内部其他接口调用*/
  4. #[derive(Debug, Clone)]
  5. pub struct ResponseData {
  6. pub label: String,
  7. pub code: i16,
  8. pub message: String,
  9. pub channel: String,
  10. pub data: Value,
  11. pub ins: Instant, // 数据接收的ins
  12. pub time: i64, // 数据接受的时间
  13. pub reach_time: i64, // 远程数据时间 弃用
  14. pub data_type: String // 數據類型, 例如 bybit 深度信息:snapshot(全量),delta(增量)
  15. }
  16. impl ResponseData {
  17. pub fn new(label: String, code: i16, message: String, data: Value) -> ResponseData {
  18. ResponseData {
  19. label,
  20. code,
  21. message,
  22. data,
  23. channel: "".to_string(),
  24. time: 0,
  25. reach_time: 0,
  26. data_type: String::new(),
  27. ins: Instant::now(),
  28. }
  29. }
  30. pub fn error(label: String, message: String) -> ResponseData {
  31. ResponseData {
  32. label,
  33. code: -1,
  34. message: format!("{}", &message),
  35. data: Value::Null,
  36. channel: "".to_string(),
  37. time: 0,
  38. reach_time: 0,
  39. data_type: String::new(),
  40. ins: Instant::now(),
  41. }
  42. }
  43. pub fn to_string(&self) -> String {
  44. format!("{:?}", self)
  45. }
  46. }