1
- use crate :: Body ;
1
+ use crate :: { bail , Body , Mime } ;
2
2
3
3
use std:: fmt:: { self , Debug } ;
4
- use std:: path:: PathBuf ;
4
+ use std:: path:: Path ;
5
5
6
6
/// A single multipart entry.
7
7
///
@@ -20,14 +20,64 @@ impl Entry {
20
20
}
21
21
}
22
22
23
+ /// Create an empty `Entry`.
24
+ pub fn empty ( name : impl AsRef < str > ) -> Self {
25
+ Self {
26
+ name : name. as_ref ( ) . to_owned ( ) ,
27
+ body : Body :: empty ( ) ,
28
+ }
29
+ }
30
+
31
+ /// Create an `Entry` from a file.
32
+ ///
33
+ #[ cfg( all( feature = "async_std" , not( target_os = "unknown" ) ) ) ]
34
+ pub async fn from_file < P > ( path : P ) -> crate :: Result < Self >
35
+ where
36
+ P : AsRef < std:: path:: Path > ,
37
+ {
38
+ let path = path. as_ref ( ) ;
39
+ let name = match path. to_str ( ) {
40
+ Some ( p) => p. to_owned ( ) ,
41
+ None => bail ! ( "Could not convert file name to unicode" ) ,
42
+ } ;
43
+ let body = Body :: from_file ( path) . await ?;
44
+ Ok ( Self :: new ( name, body) )
45
+ }
46
+
23
47
/// Get the entry name.
24
48
pub fn name ( & self ) -> & String {
25
49
& self . name
26
50
}
27
51
52
+ /// Set the entry name.
53
+ pub fn set_name < S > ( & mut self , name : S )
54
+ where
55
+ S : AsRef < str > ,
56
+ {
57
+ self . name = name. as_ref ( ) . to_owned ( ) ;
58
+ }
59
+
60
+ /// Get the content type.
61
+ pub fn content_type ( & self ) -> Option < Mime > {
62
+ todo ! ( ) ;
63
+ }
64
+
65
+ /// Set the content type.
66
+ pub fn set_content_type ( & mut self , _mime : Option < Mime > ) {
67
+ todo ! ( ) ;
68
+ }
69
+
28
70
/// Get the file name of the entry, if it's set.
29
- pub fn file_name ( & self ) -> Option < & PathBuf > {
30
- self . body . file_name . as_ref ( )
71
+ pub fn file_name ( & self ) -> Option < & Path > {
72
+ self . body . file_name ( ) . map ( |p| p. as_path ( ) )
73
+ }
74
+
75
+ /// Set the file name of the `Body`.
76
+ pub fn set_file_name < P > ( & mut self , file_name : Option < P > )
77
+ where
78
+ P : AsRef < Path > ,
79
+ {
80
+ self . body . set_file_name ( file_name) ;
31
81
}
32
82
}
33
83
0 commit comments