msg.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // Opti: using four bytes of the data to represent the source
  6. #[derive(ProtoBuf, Debug, Clone, Default)]
  7. pub struct WsMessage {
  8. #[pb(index = 1)]
  9. pub module: WsModule,
  10. #[pb(index = 2)]
  11. pub data: Vec<u8>,
  12. }
  13. #[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Hash)]
  14. pub enum WsModule {
  15. Doc = 0,
  16. }
  17. impl std::default::Default for WsModule {
  18. fn default() -> Self { WsModule::Doc }
  19. }
  20. impl ToString for WsModule {
  21. fn to_string(&self) -> String {
  22. match self {
  23. WsModule::Doc => "0".to_string(),
  24. }
  25. }
  26. }
  27. impl std::convert::Into<TokioMessage> for WsMessage {
  28. fn into(self) -> TokioMessage {
  29. let result: Result<Bytes, ::protobuf::ProtobufError> = self.try_into();
  30. match result {
  31. Ok(bytes) => TokioMessage::Binary(bytes.to_vec()),
  32. Err(e) => {
  33. log::error!("WsMessage serialize error: {:?}", e);
  34. TokioMessage::Binary(vec![])
  35. },
  36. }
  37. }
  38. }