ws_document.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use crate::errors::DocError;
  2. use bytes::Bytes;
  3. use lazy_static::lazy_static;
  4. use std::{convert::TryFrom, sync::Arc};
  5. pub struct WsDocumentMessage(pub Bytes);
  6. pub trait WsSender: Send + Sync {
  7. fn send_msg(&self, msg: WsDocumentMessage) -> Result<(), DocError>;
  8. }
  9. lazy_static! {
  10. pub static ref WS_ID: String = "Document".to_string();
  11. }
  12. pub struct WsDocument {
  13. sender: Arc<dyn WsSender>,
  14. }
  15. impl WsDocument {
  16. pub fn new(sender: Arc<dyn WsSender>) -> Self { Self { sender } }
  17. pub fn receive_msg(&self, _msg: WsDocumentMessage) { unimplemented!() }
  18. pub fn send_msg(&self, _msg: WsDocumentMessage) { unimplemented!() }
  19. }
  20. pub enum WsSource {
  21. Delta,
  22. }
  23. impl AsRef<str> for WsSource {
  24. fn as_ref(&self) -> &str {
  25. match self {
  26. WsSource::Delta => "delta",
  27. }
  28. }
  29. }
  30. impl ToString for WsSource {
  31. fn to_string(&self) -> String {
  32. match self {
  33. WsSource::Delta => self.as_ref().to_string(),
  34. }
  35. }
  36. }
  37. impl TryFrom<String> for WsSource {
  38. type Error = DocError;
  39. fn try_from(value: String) -> Result<Self, Self::Error> {
  40. match value.as_str() {
  41. "delta" => Ok(WsSource::Delta),
  42. _ => Err(DocError::internal().context(format!("Deserialize WsSource failed. Unknown type: {}", &value))),
  43. }
  44. }
  45. }