@@ -75,10 +75,13 @@ impl KVStore for FilesystemStore {
7575 dest_file_path. push ( namespace) ;
7676 dest_file_path. push ( key) ;
7777
78- let msg = format ! ( "Could not retrieve parent directory of {}." , dest_file_path. display( ) ) ;
7978 let parent_directory = dest_file_path
8079 . parent ( )
81- . ok_or ( std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , msg) ) ?
80+ . ok_or_else ( || {
81+ let msg =
82+ format ! ( "Could not retrieve parent directory of {}." , dest_file_path. display( ) ) ;
83+ std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , msg)
84+ } ) ?
8285 . to_path_buf ( ) ;
8386 fs:: create_dir_all ( parent_directory. clone ( ) ) ?;
8487
@@ -151,11 +154,11 @@ impl KVStore for FilesystemStore {
151154 fs:: remove_file ( & dest_file_path) ?;
152155 #[ cfg( not( target_os = "windows" ) ) ]
153156 {
154- let msg =
155- format ! ( "Could not retrieve parent directory of {}." , dest_file_path . display ( ) ) ;
156- let parent_directory = dest_file_path
157- . parent ( )
158- . ok_or ( std :: io :: Error :: new ( std :: io :: ErrorKind :: InvalidInput , msg ) ) ?;
157+ let parent_directory = dest_file_path . parent ( ) . ok_or_else ( || {
158+ let msg =
159+ format ! ( "Could not retrieve parent directory of {}." , dest_file_path. display ( ) ) ;
160+ std :: io :: Error :: new ( std :: io :: ErrorKind :: InvalidInput , msg )
161+ } ) ?;
159162 let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( parent_directory) ?;
160163 unsafe {
161164 // The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes
@@ -245,20 +248,21 @@ impl Read for FilesystemReader {
245248
246249impl KVStorePersister for FilesystemStore {
247250 fn persist < W : Writeable > ( & self , prefixed_key : & str , object : & W ) -> lightning:: io:: Result < ( ) > {
248- let msg = format ! ( "Could not persist file for key {}." , prefixed_key) ;
249251 let dest_file_path = PathBuf :: from_str ( prefixed_key) . map_err ( |_| {
250- lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg. clone ( ) )
252+ let msg = format ! ( "Could not persist file for key {}." , prefixed_key) ;
253+ lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg)
251254 } ) ?;
252255
253- let parent_directory = dest_file_path. parent ( ) . ok_or ( lightning :: io :: Error :: new (
254- lightning :: io :: ErrorKind :: InvalidInput ,
255- msg . clone ( ) ,
256- ) ) ?;
256+ let parent_directory = dest_file_path. parent ( ) . ok_or_else ( || {
257+ let msg = format ! ( "Could not persist file for key {}." , prefixed_key ) ;
258+ lightning :: io :: Error :: new ( lightning :: io :: ErrorKind :: InvalidInput , msg )
259+ } ) ?;
257260 let namespace = parent_directory. display ( ) . to_string ( ) ;
258261
259- let dest_without_namespace = dest_file_path
260- . strip_prefix ( & namespace)
261- . map_err ( |_| lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg) ) ?;
262+ let dest_without_namespace = dest_file_path. strip_prefix ( & namespace) . map_err ( |_| {
263+ let msg = format ! ( "Could not persist file for key {}." , prefixed_key) ;
264+ lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: InvalidInput , msg)
265+ } ) ?;
262266 let key = dest_without_namespace. display ( ) . to_string ( ) ;
263267
264268 self . write ( & namespace, & key, & object. encode ( ) ) ?;
0 commit comments