@@ -90,6 +90,7 @@ pub struct MediaFile {
9090}
9191
9292impl MediaFile {
93+ #[ must_use]
9394 pub fn new ( title : String , content : Vec < Bytes > ) -> Self {
9495 Self {
9596 id : Uuid :: new_v4 ( ) ,
@@ -98,26 +99,30 @@ impl MediaFile {
9899 }
99100 }
100101
101- pub fn from_u8 ( filename : String , data : Vec < u8 > ) -> Self {
102+ #[ must_use]
103+ pub fn from_u8 ( filename : String , data : & [ u8 ] ) -> Self {
102104 let chunk_size = 1024 ;
103105 let content: Vec < Bytes > = data
104106 . chunks ( chunk_size)
105- . map ( |chunk| chunk . to_vec ( ) )
107+ . map ( < [ u8 ] > :: to_vec)
106108 . collect ( ) ;
107109
108110 Self :: new ( filename, content)
109111 }
110112
113+ #[ must_use]
111114 pub fn get_title ( & self ) -> & str {
112115 & self . title
113116 }
114117
118+ #[ must_use]
115119 pub fn get_content ( & self ) -> & Vec < Bytes > {
116120 & self . content
117121 }
118122
123+ #[ must_use]
119124 pub fn get_size ( & self ) -> usize {
120- self . content . iter ( ) . map ( |chunk| chunk . len ( ) ) . sum ( )
125+ self . content . iter ( ) . map ( Vec :: len) . sum ( )
121126 }
122127}
123128
@@ -317,29 +322,41 @@ pub enum ServerType {
317322}
318323
319324pub mod file_conversion {
320- use super :: * ;
325+ use super :: { MediaFile , TextFile } ;
321326 use std:: fs;
322327 use std:: path:: Path ;
323328
329+ /// Converts a file path into a `MediaFile`.
330+ ///
331+ /// # Errors
332+ ///
333+ /// Returns an error if the file cannot be read, parsed, or converted
334+ /// into a `MediaFile`.
324335 pub fn file_to_media_file ( file_path : & str ) -> Result < MediaFile , Box < dyn std:: error:: Error > > {
325336 let filename = Path :: new ( file_path)
326337 . file_name ( )
327338 . and_then ( |name| name. to_str ( ) )
328339 . unwrap_or ( "unknown" )
329340 . to_string ( ) ;
330341
331- let data = fs:: read ( file_path. to_string ( ) ) ?;
332- Ok ( MediaFile :: from_u8 ( filename, data) )
342+ let data = fs:: read ( file_path) ?;
343+ Ok ( MediaFile :: from_u8 ( filename, & data) )
333344 }
334345
346+ /// Converts a file path into a `TextFile`.
347+ ///
348+ /// # Errors
349+ ///
350+ /// Returns an error if the file cannot be read, parsed, or converted
351+ /// into a `TextFile`.
335352 pub fn file_to_text_file ( file_path : & str ) -> Result < TextFile , Box < dyn std:: error:: Error > > {
336353 let filename = Path :: new ( file_path)
337354 . file_stem ( )
338355 . and_then ( |name| name. to_str ( ) )
339356 . unwrap_or ( "unknown" )
340357 . to_string ( ) ;
341358
342- let content = fs:: read_to_string ( file_path. to_string ( ) ) ?;
359+ let content = fs:: read_to_string ( file_path) ?;
343360
344361 Ok ( TextFile :: new ( filename, content, vec ! [ ] ) )
345362 }
0 commit comments