decthings_api/client/rpc/fs/
mod.rs

1mod request;
2mod response;
3
4use crate::client::StateModification;
5
6pub use request::*;
7pub use response::*;
8
9
10pub struct FsRpc {
11    rpc: crate::client::DecthingsClientRpc,
12}
13
14impl FsRpc {
15    pub(crate) fn new(rpc: crate::client::DecthingsClientRpc) -> Self {
16        Self { rpc }
17    }
18
19    pub async fn lookup(
20        &self,
21        params: LookupParams<'_, impl AsRef<[u8]>>,
22    ) -> Result<LookupResult, crate::client::DecthingsRpcError<LookupError>> {
23        let (tx, rx) = tokio::sync::oneshot::channel();
24        self.rpc
25            .raw_method_call::<_, _, &[u8]>(
26                "FS",
27                "lookup",
28                params,
29                &[],
30                crate::client::RpcProtocol::Http,
31                |x| {
32                    tx.send(x).ok();
33                    StateModification::empty()
34                },
35            )
36            .await;
37        rx.await
38            .unwrap()
39            .map_err(crate::client::DecthingsRpcError::Request)
40            .and_then(|x| {
41                let res: super::Response<LookupResult, LookupError> = serde_json::from_slice(&x.0)?;
42                match res {
43                    super::Response::Result(val) => Ok(val),
44                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
45                }
46            })
47    }
48
49    pub async fn getattr(
50        &self,
51        params: GetattrParams<'_>,
52    ) -> Result<GetattrResult, crate::client::DecthingsRpcError<GetattrError>> {
53        let (tx, rx) = tokio::sync::oneshot::channel();
54        self.rpc
55            .raw_method_call::<_, _, &[u8]>(
56                "FS",
57                "getattr",
58                params,
59                &[],
60                crate::client::RpcProtocol::Http,
61                |x| {
62                    tx.send(x).ok();
63                    StateModification::empty()
64                },
65            )
66            .await;
67        rx.await
68            .unwrap()
69            .map_err(crate::client::DecthingsRpcError::Request)
70            .and_then(|x| {
71                let res: super::Response<GetattrResult, GetattrError> =
72                    serde_json::from_slice(&x.0)?;
73                match res {
74                    super::Response::Result(val) => Ok(val),
75                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
76                }
77            })
78    }
79
80    pub async fn setattr(
81        &self,
82        params: SetattrParams<'_>,
83    ) -> Result<SetattrResult, crate::client::DecthingsRpcError<SetattrError>> {
84        let (tx, rx) = tokio::sync::oneshot::channel();
85        self.rpc
86            .raw_method_call::<_, _, &[u8]>(
87                "FS",
88                "setattr",
89                params,
90                &[],
91                crate::client::RpcProtocol::Http,
92                |x| {
93                    tx.send(x).ok();
94                    StateModification::empty()
95                },
96            )
97            .await;
98        rx.await
99            .unwrap()
100            .map_err(crate::client::DecthingsRpcError::Request)
101            .and_then(|x| {
102                let res: super::Response<SetattrResult, SetattrError> =
103                    serde_json::from_slice(&x.0)?;
104                match res {
105                    super::Response::Result(val) => Ok(val),
106                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
107                }
108            })
109    }
110
111    pub async fn mknod(
112        &self,
113        params: MknodParams<'_, impl AsRef<[u8]>>,
114    ) -> Result<MknodResult, crate::client::DecthingsRpcError<MknodError>> {
115        let (tx, rx) = tokio::sync::oneshot::channel();
116        self.rpc
117            .raw_method_call::<_, _, &[u8]>(
118                "FS",
119                "mknod",
120                params,
121                &[],
122                crate::client::RpcProtocol::Http,
123                |x| {
124                    tx.send(x).ok();
125                    StateModification::empty()
126                },
127            )
128            .await;
129        rx.await
130            .unwrap()
131            .map_err(crate::client::DecthingsRpcError::Request)
132            .and_then(|x| {
133                let res: super::Response<MknodResult, MknodError> = serde_json::from_slice(&x.0)?;
134                match res {
135                    super::Response::Result(val) => Ok(val),
136                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
137                }
138            })
139    }
140
141    pub async fn read(
142        &self,
143        params: ReadParams<'_>,
144    ) -> Result<ReadResult, crate::client::DecthingsRpcError<ReadError>> {
145        let (tx, rx) = tokio::sync::oneshot::channel();
146        self.rpc
147            .raw_method_call::<_, _, &[u8]>(
148                "FS",
149                "read",
150                params,
151                &[],
152                crate::client::RpcProtocol::Http,
153                |x| {
154                    tx.send(x).ok();
155                    StateModification::empty()
156                },
157            )
158            .await;
159        rx.await
160            .unwrap()
161            .map_err(crate::client::DecthingsRpcError::Request)
162            .and_then(|mut x| {
163                let res: super::Response<ReadResult, ReadError> = serde_json::from_slice(&x.0)?;
164                match res {
165                    super::Response::Result(ReadResult { .. }) => {
166                        if x.1.is_empty() {
167                            return Err(crate::client::DecthingsClientError::InvalidMessage.into());
168                        }
169                        Ok(ReadResult {
170                            data: x.1.remove(0),
171                        })
172                    }
173                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
174                }
175            })
176    }
177
178    pub async fn write(
179        &self,
180        params: WriteParams<'_, impl AsRef<[u8]>>,
181    ) -> Result<WriteResult, crate::client::DecthingsRpcError<WriteError>> {
182        let (tx, rx) = tokio::sync::oneshot::channel();
183        self.rpc
184            .raw_method_call(
185                "FS",
186                "write",
187                &params,
188                &[params.data.as_ref()],
189                crate::client::RpcProtocol::Http,
190                |x| {
191                    tx.send(x).ok();
192                    StateModification::empty()
193                },
194            )
195            .await;
196        rx.await
197            .unwrap()
198            .map_err(crate::client::DecthingsRpcError::Request)
199            .and_then(|x| {
200                let res: super::Response<WriteResult, WriteError> = serde_json::from_slice(&x.0)?;
201                match res {
202                    super::Response::Result(val) => Ok(val),
203                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
204                }
205            })
206    }
207
208    pub async fn symlink(
209        &self,
210        params: SymlinkParams<'_, impl AsRef<[u8]>, impl AsRef<[u8]>>,
211    ) -> Result<SymlinkResult, crate::client::DecthingsRpcError<SymlinkError>> {
212        let (tx, rx) = tokio::sync::oneshot::channel();
213        self.rpc
214            .raw_method_call::<_, _, &[u8]>(
215                "FS",
216                "symlink",
217                params,
218                &[],
219                crate::client::RpcProtocol::Http,
220                |x| {
221                    tx.send(x).ok();
222                    StateModification::empty()
223                },
224            )
225            .await;
226        rx.await
227            .unwrap()
228            .map_err(crate::client::DecthingsRpcError::Request)
229            .and_then(|x| {
230                let res: super::Response<SymlinkResult, SymlinkError> =
231                    serde_json::from_slice(&x.0)?;
232                match res {
233                    super::Response::Result(val) => Ok(val),
234                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
235                }
236            })
237    }
238
239    pub async fn readlink(
240        &self,
241        params: ReadlinkParams<'_>,
242    ) -> Result<ReadlinkResult, crate::client::DecthingsRpcError<ReadlinkError>> {
243        let (tx, rx) = tokio::sync::oneshot::channel();
244        self.rpc
245            .raw_method_call::<_, _, &[u8]>(
246                "FS",
247                "readlink",
248                params,
249                &[],
250                crate::client::RpcProtocol::Http,
251                |x| {
252                    tx.send(x).ok();
253                    StateModification::empty()
254                },
255            )
256            .await;
257        rx.await
258            .unwrap()
259            .map_err(crate::client::DecthingsRpcError::Request)
260            .and_then(|mut x| {
261                let res: super::Response<ReadlinkResult, ReadlinkError> =
262                    serde_json::from_slice(&x.0)?;
263                match res {
264                    super::Response::Result(ReadlinkResult { .. }) => {
265                        if x.1.is_empty() {
266                            return Err(crate::client::DecthingsClientError::InvalidMessage.into());
267                        }
268                        Ok(ReadlinkResult {
269                            link: x.1.remove(0),
270                        })
271                    }
272                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
273                }
274            })
275    }
276
277    pub async fn mkdir(
278        &self,
279        params: MkdirParams<'_, impl AsRef<[u8]>>,
280    ) -> Result<MkdirResult, crate::client::DecthingsRpcError<MkdirError>> {
281        let (tx, rx) = tokio::sync::oneshot::channel();
282        self.rpc
283            .raw_method_call::<_, _, &[u8]>(
284                "FS",
285                "mkdir",
286                params,
287                &[],
288                crate::client::RpcProtocol::Http,
289                |x| {
290                    tx.send(x).ok();
291                    StateModification::empty()
292                },
293            )
294            .await;
295        rx.await
296            .unwrap()
297            .map_err(crate::client::DecthingsRpcError::Request)
298            .and_then(|x| {
299                let res: super::Response<MkdirResult, MkdirError> = serde_json::from_slice(&x.0)?;
300                match res {
301                    super::Response::Result(val) => Ok(val),
302                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
303                }
304            })
305    }
306
307    pub async fn unlink(
308        &self,
309        params: UnlinkParams<'_, impl AsRef<[u8]>>,
310    ) -> Result<UnlinkResult, crate::client::DecthingsRpcError<UnlinkError>> {
311        let (tx, rx) = tokio::sync::oneshot::channel();
312        self.rpc
313            .raw_method_call::<_, _, &[u8]>(
314                "FS",
315                "unlink",
316                params,
317                &[],
318                crate::client::RpcProtocol::Http,
319                |x| {
320                    tx.send(x).ok();
321                    StateModification::empty()
322                },
323            )
324            .await;
325        rx.await
326            .unwrap()
327            .map_err(crate::client::DecthingsRpcError::Request)
328            .and_then(|x| {
329                let res: super::Response<UnlinkResult, UnlinkError> = serde_json::from_slice(&x.0)?;
330                match res {
331                    super::Response::Result(val) => Ok(val),
332                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
333                }
334            })
335    }
336
337    pub async fn rmdir(
338        &self,
339        params: RmdirParams<'_, impl AsRef<[u8]>>,
340    ) -> Result<RmdirResult, crate::client::DecthingsRpcError<RmdirError>> {
341        let (tx, rx) = tokio::sync::oneshot::channel();
342        self.rpc
343            .raw_method_call::<_, _, &[u8]>(
344                "FS",
345                "rmdir",
346                params,
347                &[],
348                crate::client::RpcProtocol::Http,
349                |x| {
350                    tx.send(x).ok();
351                    StateModification::empty()
352                },
353            )
354            .await;
355        rx.await
356            .unwrap()
357            .map_err(crate::client::DecthingsRpcError::Request)
358            .and_then(|x| {
359                let res: super::Response<RmdirResult, RmdirError> = serde_json::from_slice(&x.0)?;
360                match res {
361                    super::Response::Result(val) => Ok(val),
362                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
363                }
364            })
365    }
366
367    pub async fn rename(
368        &self,
369        params: RenameParams<'_, impl AsRef<[u8]>, impl AsRef<[u8]>>,
370    ) -> Result<RenameResult, crate::client::DecthingsRpcError<RenameError>> {
371        let (tx, rx) = tokio::sync::oneshot::channel();
372        self.rpc
373            .raw_method_call::<_, _, &[u8]>(
374                "FS",
375                "rename",
376                params,
377                &[],
378                crate::client::RpcProtocol::Http,
379                |x| {
380                    tx.send(x).ok();
381                    StateModification::empty()
382                },
383            )
384            .await;
385        rx.await
386            .unwrap()
387            .map_err(crate::client::DecthingsRpcError::Request)
388            .and_then(|x| {
389                let res: super::Response<RenameResult, RenameError> = serde_json::from_slice(&x.0)?;
390                match res {
391                    super::Response::Result(val) => Ok(val),
392                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
393                }
394            })
395    }
396
397    pub async fn link(
398        &self,
399        params: LinkParams<'_, impl AsRef<[u8]>>,
400    ) -> Result<LinkResult, crate::client::DecthingsRpcError<LinkError>> {
401        let (tx, rx) = tokio::sync::oneshot::channel();
402        self.rpc
403            .raw_method_call::<_, _, &[u8]>(
404                "FS",
405                "link",
406                params,
407                &[],
408                crate::client::RpcProtocol::Http,
409                |x| {
410                    tx.send(x).ok();
411                    StateModification::empty()
412                },
413            )
414            .await;
415        rx.await
416            .unwrap()
417            .map_err(crate::client::DecthingsRpcError::Request)
418            .and_then(|x| {
419                let res: super::Response<LinkResult, LinkError> = serde_json::from_slice(&x.0)?;
420                match res {
421                    super::Response::Result(val) => Ok(val),
422                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
423                }
424            })
425    }
426
427    pub async fn readdir(
428        &self,
429        params: ReaddirParams<'_>,
430    ) -> Result<ReaddirResult, crate::client::DecthingsRpcError<ReaddirError>> {
431        let (tx, rx) = tokio::sync::oneshot::channel();
432        self.rpc
433            .raw_method_call::<_, _, &[u8]>(
434                "FS",
435                "readdir",
436                params,
437                &[],
438                crate::client::RpcProtocol::Http,
439                |x| {
440                    tx.send(x).ok();
441                    StateModification::empty()
442                },
443            )
444            .await;
445        rx.await
446            .unwrap()
447            .map_err(crate::client::DecthingsRpcError::Request)
448            .and_then(|x| {
449                let res: super::Response<ReaddirResult, ReaddirError> =
450                    serde_json::from_slice(&x.0)?;
451                match res {
452                    super::Response::Result(val) => Ok(val),
453                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
454                }
455            })
456    }
457
458    pub async fn rmdir_all(
459        &self,
460        params: RmdirAllParams<'_, impl AsRef<[u8]>>,
461    ) -> Result<RmdirAllResult, crate::client::DecthingsRpcError<RmdirAllError>> {
462        let (tx, rx) = tokio::sync::oneshot::channel();
463        self.rpc
464            .raw_method_call::<_, _, &[u8]>(
465                "FS",
466                "rmdirAll",
467                params,
468                &[],
469                crate::client::RpcProtocol::Http,
470                |x| {
471                    tx.send(x).ok();
472                    StateModification::empty()
473                },
474            )
475            .await;
476        rx.await
477            .unwrap()
478            .map_err(crate::client::DecthingsRpcError::Request)
479            .and_then(|x| {
480                let res: super::Response<RmdirAllResult, RmdirAllError> =
481                    serde_json::from_slice(&x.0)?;
482                match res {
483                    super::Response::Result(val) => Ok(val),
484                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
485                }
486            })
487    }
488
489    pub async fn copy(
490        &self,
491        params: CopyParams<'_, impl AsRef<[u8]>>,
492    ) -> Result<CopyResult, crate::client::DecthingsRpcError<CopyError>> {
493        let (tx, rx) = tokio::sync::oneshot::channel();
494        self.rpc
495            .raw_method_call::<_, _, &[u8]>(
496                "FS",
497                "copy",
498                params,
499                &[],
500                crate::client::RpcProtocol::Http,
501                |x| {
502                    tx.send(x).ok();
503                    StateModification::empty()
504                },
505            )
506            .await;
507        rx.await
508            .unwrap()
509            .map_err(crate::client::DecthingsRpcError::Request)
510            .and_then(|x| {
511                let res: super::Response<CopyResult, CopyError> = serde_json::from_slice(&x.0)?;
512                match res {
513                    super::Response::Result(val) => Ok(val),
514                    super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
515                }
516            })
517    }
518}