1- use std:: { io, path:: PathBuf } ;
1+ use std:: { io, path:: Path } ;
22
33use tokio:: sync:: { mpsc, oneshot} ;
44
@@ -10,134 +10,71 @@ pub struct Client {
1010}
1111
1212impl Client {
13- pub async fn read ( & self , path : PathBuf ) -> io:: Result < Vec < u8 > > {
14- let ( sender, receiver) = oneshot:: channel ( ) ;
15-
16- let task =FsTask :: Read { path, sender} ;
17-
18- self . sender
19- . send ( task)
20- . await
21- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
22-
23- receiver
24- . await
25- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
13+ pub async fn read ( & self , path : impl AsRef < Path > ) -> io:: Result < Vec < u8 > > {
14+ let path = path. as_ref ( ) . into ( ) ;
15+ self . dispatch ( |sender|FsTask :: Read { path, sender} ) . await
2616}
2717
28- pub async fn write ( & self , path : PathBuf , content : Vec < u8 > ) -> io:: Result < ( ) > {
29- let ( sender , receiver ) = oneshot :: channel ( ) ;
30-
31- let task = FsTask :: Write {
18+ pub async fn write ( & self , path : impl AsRef < Path > , content : & [ u8 ] ) -> io:: Result < ( ) > {
19+ let path = path . as_ref ( ) . into ( ) ;
20+ let content = content . to_vec ( ) ;
21+ self . dispatch ( |sender| FsTask :: Write {
3222 path,
3323 sender,
3424 content,
35- } ;
36-
37- self . sender
38- . send ( task)
39- . await
40- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
41-
42- receiver
43- . await
44- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
25+ } )
26+ . await
4527}
4628
47- pub async fn read_dir ( & self , path : PathBuf ) -> io:: Result < ReadDir > {
48- let ( sender, receiver) = oneshot:: channel ( ) ;
49-
50- let task =FsTask :: ReadDir { path, sender} ;
51-
52- self . sender
53- . send ( task)
54- . await
55- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
56-
57- receiver
29+ pub async fn read_dir ( & self , path : impl AsRef < Path > ) -> io:: Result < ReadDir > {
30+ let path = path. as_ref ( ) . into ( ) ;
31+ self . dispatch ( |sender|FsTask :: ReadDir { path, sender} )
5832. await
59- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
6033}
6134
62- pub async fn create_dir ( & self , path : PathBuf ) -> io:: Result < ( ) > {
63- let ( sender, receiver) = oneshot:: channel ( ) ;
64-
65- let task =FsTask :: CreateDir { path, sender} ;
66-
67- self . sender
68- . send ( task)
69- . await
70- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
71-
72- receiver
35+ pub async fn create_dir ( & self , path : impl AsRef < Path > ) -> io:: Result < ( ) > {
36+ let path = path. as_ref ( ) . into ( ) ;
37+ self . dispatch ( |sender|FsTask :: CreateDir { path, sender} )
7338. await
74- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
7539}
7640
77- pub async fn create_dir_all ( & self , path : PathBuf ) -> io:: Result < ( ) > {
78- let ( sender, receiver) = oneshot:: channel ( ) ;
79-
80- let task =FsTask :: CreateDirAll { path, sender} ;
81-
82- self . sender
83- . send ( task)
84- . await
85- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
86-
87- receiver
41+ pub async fn create_dir_all ( & self , path : impl AsRef < Path > ) -> io:: Result < ( ) > {
42+ let path = path. as_ref ( ) . into ( ) ;
43+ self . dispatch ( |sender|FsTask :: CreateDirAll { path, sender} )
8844. await
89- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
9045}
9146
92- pub async fn remove_file ( & self , path : PathBuf ) -> io:: Result < ( ) > {
93- let ( sender, receiver) = oneshot:: channel ( ) ;
94-
95- let task =FsTask :: RemoveFile { path, sender} ;
96-
97- self . sender
98- . send ( task)
99- . await
100- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
101-
102- receiver
47+ pub async fn remove_file ( & self , path : impl AsRef < Path > ) -> io:: Result < ( ) > {
48+ let path = path. as_ref ( ) . into ( ) ;
49+ self . dispatch ( |sender|FsTask :: RemoveFile { path, sender} )
10350. await
104- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
10551}
10652
107- pub async fn remove_dir ( & self , path : PathBuf ) -> io:: Result < ( ) > {
108- let ( sender, receiver) = oneshot:: channel ( ) ;
109-
110- let task =FsTask :: RemoveDir { path, sender} ;
111-
112- self . sender
113- . send ( task)
114- . await
115- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?;
116-
117- receiver
53+ pub async fn remove_dir ( & self , path : impl AsRef < Path > ) -> io:: Result < ( ) > {
54+ let path = path. as_ref ( ) . into ( ) ;
55+ self . dispatch ( |sender|FsTask :: RemoveDir { path, sender} )
11856. await
119- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
12057}
12158
122- pub async fn remove_dir_all ( & self , path : PathBuf ) -> io:: Result < ( ) > {
123- let ( sender, receiver) = oneshot:: channel ( ) ;
124-
125- let task =FsTask :: RemoveDirAll { path, sender} ;
126-
127- self . sender
128- . send ( task)
59+ pub async fn remove_dir_all ( & self , path : impl AsRef < Path > ) -> io:: Result < ( ) > {
60+ let path = path. as_ref ( ) . into ( ) ;
61+ self . dispatch ( |sender|FsTask :: RemoveDirAll { path, sender} )
12962. await
130- . map_err ( |_| io :: Error :: from ( io :: ErrorKind :: ConnectionAborted ) ) ? ;
63+ }
13164
132- receiver
65+ pub async fn metadata ( & self , path : impl AsRef < Path > ) -> io:: Result < Metadata > {
66+ let path = path. as_ref ( ) . into ( ) ;
67+ self . dispatch ( |sender|FsTask :: Metadata { path, sender} )
13368. await
134- . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: ConnectionAborted ) ) ?
13569}
13670
137- pub async fn metadata ( & self , path : PathBuf ) -> io:: Result < Metadata > {
71+ async fn dispatch < T , F > ( & self , create_task : F ) -> io:: Result < T >
72+ where
73+ F : FnOnce ( oneshot:: Sender < io:: Result < T > > ) ->FsTask ,
74+ {
13875let ( sender, receiver) = oneshot:: channel ( ) ;
13976
140- let task =FsTask :: Metadata { path , sender} ;
77+ let task =create_task ( sender) ;
14178
14279self . sender
14380. send ( task)