1- use crate :: Body ;
1+ use crate :: { bail , Body , Mime } ;
22
33use std:: fmt:: { self , Debug } ;
4- use std:: path:: PathBuf ;
4+ use std:: path:: Path ;
55
66/// A single multipart entry.
77///
@@ -20,14 +20,64 @@ impl Entry {
2020 }
2121 }
2222
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+
2347 /// Get the entry name.
2448 pub fn name ( & self ) -> & String {
2549 & self . name
2650 }
2751
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+
2870 /// 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) ;
3181 }
3282}
3383
0 commit comments