nikau/msgs/
bulk.rs

1use std::net::SocketAddr;
2
3use serde::{Deserialize, Serialize};
4
5/// A serialized bulk message sent from the server to the client.
6/// This is sent on a separate 'bulk' stream from the main 'events' stream, to avoid blocking events.
7#[derive(Debug, Deserialize, Serialize)]
8pub enum ServerBulk<'a> {
9    /// Request for clipboard content of the specified type from the client.
10    #[serde(borrow)]
11    ClipboardRequest(ServerClipboardRequest<'a>),
12
13    /// Sends requested clipboard contents to the client.
14    #[serde(borrow)]
15    ClipboardHeader(ServerClipboardHeader<'a>),
16}
17
18impl<'a> std::fmt::Display for ServerBulk<'a> {
19    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20        match self {
21            ServerBulk::ClipboardRequest(e) => e.fmt(f),
22            ServerBulk::ClipboardHeader(e) => e.fmt(f),
23        }
24    }
25}
26
27/// A serialized bulk message sent either from the client to the server.
28/// This is sent on a separate 'bulk' stream from the main 'events' stream, to avoid blocking events.
29#[derive(Debug, Deserialize, Serialize)]
30pub enum ClientBulk<'a> {
31    /// Request for clipboard content of the specified type from the server (may then route to a client).
32    #[serde(borrow)]
33    ClipboardRequest(ClientClipboardRequest<'a>),
34
35    /// Sends requested clipboard contents to the server.
36    #[serde(borrow)]
37    ClipboardHeader(ClientClipboardHeader<'a>),
38}
39
40impl<'a> std::fmt::Display for ClientBulk<'a> {
41    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42        match self {
43            ClientBulk::ClipboardRequest(e) => e.fmt(f),
44            ClientBulk::ClipboardHeader(e) => e.fmt(f),
45        }
46    }
47}
48
49// ServerClipboardRequest
50
51/// Request to retrieve a previously advertised clipboard, sent from the server to a client
52#[derive(Debug, Deserialize, Serialize)]
53pub struct ServerClipboardRequest<'a> {
54    /// The desired type to be retrieved from the client,
55    /// from a prior ClipboardTypes event advertised by the client
56    pub requested_type: &'a str,
57
58    /// Request that any sent clipboards not exceed this size
59    pub max_size_bytes: u64,
60
61    /// The client that requested the clipboard, or None if it was the server.
62    /// Used by the server to route the clipboard back to the requestor.
63    pub request_client: Option<SocketAddr>,
64}
65
66impl<'a> std::fmt::Display for ServerClipboardRequest<'a> {
67    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
68        f.write_str(
69            format!(
70                "ServerClipboardRequest(requested_type={}, max_size_bytes={}, request_client={:?})",
71                self.requested_type, self.max_size_bytes, self.request_client,
72            )
73            .as_str(),
74        )
75    }
76}
77
78// ServerClipboardHeader
79
80/// Metadata about requested clipboard content which follows, sent from the server to a client
81#[derive(Debug, Deserialize, Serialize)]
82pub struct ServerClipboardHeader<'a> {
83    /// The mime type that had originally been requested in the ClientClipboardRequest
84    pub requested_type: &'a str,
85
86    /// The actual type being returned, or None if it matches requested_type.
87    /// This is used for sending compressed or packaged payloads as needed for some types.
88    pub data_type: Option<&'a str>,
89
90    /// The length of the clipboard content that follows this header
91    pub content_len_bytes: u64,
92}
93
94impl<'a> std::fmt::Display for ServerClipboardHeader<'a> {
95    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
96        f.write_str(
97            format!(
98                "ServerClipboardHeader(requested_type={}, data_type={:?}, content_len_bytes={})",
99                self.requested_type, self.data_type, self.content_len_bytes,
100            )
101            .as_str(),
102        )
103    }
104}
105
106// ClientClipboardRequest
107
108/// Request to retrieve a previously advertised clipboard, sent from a client to the server
109#[derive(Debug, Deserialize, Serialize)]
110pub struct ClientClipboardRequest<'a> {
111    /// The desired type to be retrieved from the server,
112    /// from a prior ClipboardTypes event adverstised by the server
113    pub requested_type: &'a str,
114
115    /// Request that any sent clipboards not exceed this size
116    pub max_size_bytes: u64,
117}
118
119impl<'a> std::fmt::Display for ClientClipboardRequest<'a> {
120    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
121        f.write_str(
122            format!(
123                "ClientClipboardRequest(requested_type={}, max_size_bytes={})",
124                self.requested_type, self.max_size_bytes,
125            )
126            .as_str(),
127        )
128    }
129}
130
131// ClientClipboardHeader
132
133/// Metadata about requested clipboard content which follows, sent from a client to the server
134#[derive(Debug, Deserialize, Serialize)]
135pub struct ClientClipboardHeader<'a> {
136    /// The mime type that had originally been requested in the ServerClipboardRequest
137    pub requested_type: &'a str,
138
139    /// The actual type being returned, or None if it matches requested_type.
140    /// This is used for sending compressed or packaged payloads as needed for some types.
141    pub data_type: Option<&'a str>,
142
143    /// The length of the clipboard content that follows this header
144    pub content_len_bytes: u64,
145
146    /// The client that requested the clipboard, or None if it was the server.
147    /// Copied from the preceding ServerClipboardRequest
148    pub request_client: Option<SocketAddr>,
149}
150
151impl<'a> std::fmt::Display for ClientClipboardHeader<'a> {
152    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
153        f.write_str(
154            format!(
155                "ClientClipboardHeader(requested_type={}, data_type={:?}, content_len_bytes={}, request_client={:?})",
156                self.requested_type, self.data_type, self.content_len_bytes, self.request_client,
157            )
158            .as_str(),
159        )
160    }
161}