@@ -579,11 +579,16 @@ pub fn write<R: Runtime>(
579579}
580580
581581#[ derive( Debug , Clone , Deserialize ) ]
582+ #[ serde( rename_all = "camelCase" ) ]
582583pub struct WriteFileOptions {
583584 #[ serde( flatten) ]
584585 base : BaseOptions ,
585- append : Option < bool > ,
586- create : Option < bool > ,
586+ #[ serde( default ) ]
587+ append : bool ,
588+ #[ serde( default ) ]
589+ create : bool ,
590+ #[ serde( default ) ]
591+ create_new : bool ,
587592 #[ allow( unused) ]
588593 mode : Option < u32 > ,
589594}
@@ -597,18 +602,25 @@ fn write_file_inner<R: Runtime>(
597602 let resolved_path = resolve_path ( & app, path, options. as_ref ( ) . and_then ( |o| o. base . base_dir ) ) ?;
598603
599604 let mut opts = std:: fs:: OpenOptions :: new ( ) ;
600- opts . append ( options . as_ref ( ) . map ( |o| o . append . unwrap_or ( false ) ) . unwrap ( ) ) ;
601- opts. create ( options . as_ref ( ) . map ( |o| o . create . unwrap_or ( true ) ) . unwrap ( ) ) ;
605+ // defaults
606+ opts. read ( false ) . write ( true ) . truncate ( true ) . create ( true ) ;
602607
603- #[ cfg( unix) ]
604- {
605- use std:: os:: unix:: fs:: OpenOptionsExt ;
606- if let Some ( Some ( mode) ) = options. map ( |o| o. mode ) {
607- opts. mode ( mode & 0o777 ) ;
608+ if let Some ( options) = options {
609+ #[ cfg( unix) ]
610+ {
611+ use std:: os:: unix:: fs:: OpenOptionsExt ;
612+ if let Some ( mode) = options. mode {
613+ opts. mode ( mode) ;
614+ }
608615 }
616+
617+ opts. create ( options. create )
618+ . append ( options. append )
619+ . truncate ( !options. append )
620+ . create_new ( options. create_new ) ;
609621 }
610622
611- let mut file = opts. write ( true ) . open ( & resolved_path) . map_err ( |e| {
623+ let mut file = opts. open ( & resolved_path) . map_err ( |e| {
612624 format ! (
613625 "failed to open file at path: {} with error: {e}" ,
614626 resolved_path. display( )
0 commit comments