msg.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use bytes::Bytes;
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use std::convert::TryInto;
  4. use tokio_tungstenite::tungstenite::Message as TokioMessage;
  5. #[derive(ProtoBuf, Debug, Clone, Default)]
  6. pub struct WebSocketRawMessage {
  7. #[pb(index = 1)]
  8. pub channel: WSChannel,
  9. #[pb(index = 2)]
  10. pub data: Vec<u8>,
  11. }
  12. // The lib-ws crate should not contain business logic.So WSChannel should be removed into another place.
  13. #[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Hash)]
  14. pub enum WSChannel {
  15. Document = 0,
  16. Folder = 1,
  17. Grid = 2,
  18. }
  19. impl std::default::Default for WSChannel {
  20. fn default() -> Self {
  21. WSChannel::Document
  22. }
  23. }
  24. impl ToString for WSChannel {
  25. fn to_string(&self) -> String {
  26. match self {
  27. WSChannel::Document => "0".to_string(),
  28. WSChannel::Folder => "1".to_string(),
  29. WSChannel::Grid => "2".to_string(),
  30. }
  31. }
  32. }
  33. impl std::convert::From<WebSocketRawMessage> for TokioMessage {
  34. fn from(msg: WebSocketRawMessage) -> Self {
  35. let result: Result<Bytes, ::protobuf::ProtobufError> = msg.try_into();
  36. match result {
  37. Ok(bytes) => TokioMessage::Binary(bytes.to_vec()),
  38. Err(e) => {
  39. log::error!("WsMessage serialize error: {:?}", e);
  40. TokioMessage::Binary(vec![])
  41. }
  42. }
  43. }
  44. }