rmux_sdk/web_share/
handle.rs1use rmux_proto::{PaneTargetRef, WebShareScope};
2
3use crate::handles::Rmux;
4use crate::transport::TransportClient;
5use crate::Result;
6
7use super::{
8 list_web_shares, lookup_summary, stop_all_web_shares, stop_web_share, token_from_url,
9 web_config, WebConfigInfo, WebShareSummary,
10};
11
12#[derive(Clone)]
21pub struct WebShareHandle {
22 transport: TransportClient,
23 id: String,
24 scope: WebShareScope,
25 spectator_url: Option<String>,
26 operator_url: Option<String>,
27 expires_at_unix: Option<u64>,
28 operator_pairing_code: Option<String>,
29 spectator_pairing_code: Option<String>,
30 max_operators: Option<u16>,
31 max_spectators: Option<u16>,
32 operator: bool,
33 spectator: bool,
34 kill_session_on_expire: bool,
35}
36
37impl WebShareHandle {
38 pub(crate) fn new(
39 transport: TransportClient,
40 created: rmux_proto::WebShareCreatedResponse,
41 ) -> Self {
42 Self {
43 transport,
44 id: created.share_id,
45 scope: created.scope,
46 spectator_url: created.spectator_url,
47 operator_url: created.operator_url,
48 expires_at_unix: created.expires_at_unix,
49 operator_pairing_code: created.operator_pairing_code,
50 spectator_pairing_code: created.spectator_pairing_code,
51 max_operators: created.max_operators,
52 max_spectators: created.max_spectators,
53 operator: created.operator,
54 spectator: created.spectator,
55 kill_session_on_expire: created.kill_session_on_expire,
56 }
57 }
58
59 #[must_use]
61 pub fn id(&self) -> &str {
62 &self.id
63 }
64
65 #[must_use]
67 pub const fn scope(&self) -> &WebShareScope {
68 &self.scope
69 }
70
71 #[must_use]
73 pub fn pane_target(&self) -> Option<&PaneTargetRef> {
74 match &self.scope {
75 WebShareScope::Pane(target) => Some(target),
76 WebShareScope::Session(_) => None,
77 }
78 }
79
80 #[must_use]
82 pub const fn operator(&self) -> bool {
83 self.operator
84 }
85
86 #[must_use]
88 pub const fn spectator(&self) -> bool {
89 self.spectator
90 }
91
92 #[must_use]
94 pub const fn kill_session_on_expire(&self) -> bool {
95 self.kill_session_on_expire
96 }
97
98 #[must_use]
100 pub fn spectator_url(&self) -> Option<&str> {
101 self.spectator_url.as_deref()
102 }
103
104 #[must_use]
106 pub fn spectator_token(&self) -> Option<&str> {
107 self.spectator_url.as_deref().and_then(token_from_url)
108 }
109
110 #[must_use]
112 pub fn operator_url(&self) -> Option<&str> {
113 self.operator_url.as_deref()
114 }
115
116 #[must_use]
118 pub fn operator_token(&self) -> Option<&str> {
119 self.operator_url.as_deref().and_then(token_from_url)
120 }
121
122 #[must_use]
124 pub fn operator_pairing_code(&self) -> Option<&str> {
125 self.operator_pairing_code.as_deref()
126 }
127
128 #[must_use]
130 pub fn spectator_pairing_code(&self) -> Option<&str> {
131 self.spectator_pairing_code.as_deref()
132 }
133
134 #[must_use]
136 pub const fn max_spectators(&self) -> Option<u16> {
137 self.max_spectators
138 }
139
140 #[must_use]
142 pub const fn max_operators(&self) -> Option<u16> {
143 self.max_operators
144 }
145
146 #[must_use]
148 pub const fn expires_at_unix(&self) -> Option<u64> {
149 self.expires_at_unix
150 }
151
152 pub async fn summary(&self) -> Result<WebShareSummary> {
154 lookup_summary(&self.transport, &self.id).await
155 }
156
157 pub async fn spectators_active(&self) -> Result<u16> {
159 Ok(self.summary().await?.active_spectators)
160 }
161
162 pub async fn operators_active(&self) -> Result<u16> {
164 Ok(self.summary().await?.active_operators)
165 }
166
167 pub async fn stop(self) -> Result<()> {
169 stop_web_share(&self.transport, &self.id).await.map(|_| ())
170 }
171}
172
173#[derive(Clone)]
175pub struct WebShareLookup {
176 transport: TransportClient,
177 summary: WebShareSummary,
178}
179
180impl WebShareLookup {
181 pub(crate) fn new(transport: TransportClient, summary: WebShareSummary) -> Self {
182 Self { transport, summary }
183 }
184
185 #[must_use]
187 pub fn id(&self) -> &str {
188 &self.summary.id
189 }
190
191 #[must_use]
193 pub const fn scope(&self) -> &WebShareScope {
194 &self.summary.scope
195 }
196
197 #[must_use]
199 pub fn pane_target(&self) -> Option<&PaneTargetRef> {
200 self.summary.pane_target()
201 }
202
203 #[must_use]
205 pub const fn operator(&self) -> bool {
206 self.summary.operator
207 }
208
209 #[must_use]
211 pub const fn spectator(&self) -> bool {
212 self.summary.spectator
213 }
214
215 #[must_use]
217 pub fn spectator_url_redacted(&self) -> Option<&str> {
218 self.summary.spectator_url_redacted.as_deref()
219 }
220
221 #[must_use]
223 pub const fn cached_summary(&self) -> &WebShareSummary {
224 &self.summary
225 }
226
227 pub async fn summary(&self) -> Result<WebShareSummary> {
229 lookup_summary(&self.transport, &self.summary.id).await
230 }
231
232 pub async fn stop(self) -> Result<()> {
234 stop_web_share(&self.transport, &self.summary.id)
235 .await
236 .map(|_| ())
237 }
238}
239
240impl Rmux {
241 pub async fn list_web_shares(&self) -> Result<Vec<WebShareSummary>> {
243 let transport = self
244 .connect_transport_for_operation(self.resolved_timeout(None))
245 .await?;
246 list_web_shares(&transport).await
247 }
248
249 pub async fn stop_web_share(&self, id: &str) -> Result<bool> {
251 let transport = self
252 .connect_transport_for_operation(self.resolved_timeout(None))
253 .await?;
254 stop_web_share(&transport, id).await
255 }
256
257 pub async fn stop_all_web_shares(&self) -> Result<usize> {
259 let transport = self
260 .connect_transport_for_operation(self.resolved_timeout(None))
261 .await?;
262 stop_all_web_shares(&transport).await
263 }
264
265 pub async fn web_share_by_id(&self, id: &str) -> Result<WebShareLookup> {
267 let transport = self
268 .connect_transport_for_operation(self.resolved_timeout(None))
269 .await?;
270 let summary = lookup_summary(&transport, id).await?;
271 Ok(WebShareLookup::new(transport, summary))
272 }
273
274 pub async fn web_config(&self) -> Result<WebConfigInfo> {
276 let transport = self
277 .connect_transport_for_operation(self.resolved_timeout(None))
278 .await?;
279 web_config(&transport).await
280 }
281}