@@ -420,7 +420,7 @@ impl File {
420420 let current = env:: current_dir ( ) . unwrap_or_default ( ) ;
421421 let absolute_path = if path. is_absolute ( ) { path } else { & current. join ( path) } ;
422422 let absolute_str = absolute_path. display ( ) . to_string ( ) ;
423- let metadata = fs:: metadata ( path) . expect ( "Could not get metadata" ) ;
423+ let metadata = fs:: metadata ( path) . unwrap_or_else ( |_| panic ! ( "Could not get metadata for {absolute_str}" ) ) ;
424424 let mut hasher = Sha1 :: new ( ) ;
425425 let hash = fs:: read ( path) . ok ( ) . map ( |f| {
426426 hasher. update ( & f) ;
@@ -442,6 +442,30 @@ impl File {
442442 ..Default :: default ( )
443443 }
444444 }
445+
446+ pub fn preload ( & self ) -> Self {
447+ let loc = self . location . clone ( ) . unwrap_or_default ( ) ;
448+ let path = Path :: new ( & loc) ;
449+
450+ let mut item = self . clone ( ) ;
451+ if item. path . is_none ( ) {
452+ item. path = Some ( path. display ( ) . to_string ( ) ) ;
453+ }
454+
455+ if item. basename . is_none ( ) {
456+ item. basename = path. file_name ( ) . map ( |f| f. to_string_lossy ( ) . into_owned ( ) ) ;
457+ }
458+
459+ if item. nameroot . is_none ( ) {
460+ item. nameroot = path. file_stem ( ) . map ( |f| f. to_string_lossy ( ) . into_owned ( ) ) ;
461+ }
462+
463+ if item. nameext . is_none ( ) {
464+ item. nameext = path. extension ( ) . map ( |f| format ! ( ".{}" , f. to_string_lossy( ) ) ) ;
465+ }
466+
467+ item
468+ }
445469}
446470
447471fn resolve_format ( format : Option < String > ) -> Option < String > {
@@ -467,6 +491,45 @@ impl PathItem for File {
467491 }
468492}
469493
494+ #[ derive( Serialize , Debug , Default , PartialEq , Clone ) ]
495+ pub struct SecondaryFileSchema {
496+ pub pattern : String ,
497+ pub required : bool ,
498+ }
499+
500+ impl From < String > for SecondaryFileSchema {
501+ fn from ( pattern : String ) -> Self {
502+ if pattern. ends_with ( "?" ) {
503+ let pattern = pattern. trim_end_matches ( '?' ) . to_string ( ) ;
504+ SecondaryFileSchema { pattern, required : false }
505+ } else {
506+ SecondaryFileSchema { pattern, required : true }
507+ }
508+ }
509+ }
510+
511+ impl < ' de > Deserialize < ' de > for SecondaryFileSchema {
512+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
513+ where
514+ D : Deserializer < ' de > ,
515+ {
516+ let value: Value = Deserialize :: deserialize ( deserializer) ?;
517+ match value {
518+ Value :: String ( pattern) => Ok ( SecondaryFileSchema :: from ( pattern) ) ,
519+ Value :: Mapping ( map) => {
520+ let pattern = map
521+ . get ( "pattern" )
522+ . and_then ( |v| v. as_str ( ) )
523+ . ok_or_else ( || serde:: de:: Error :: custom ( "Expected string for pattern" ) ) ?
524+ . to_string ( ) ;
525+ let required = map. get ( "required" ) . and_then ( |v| v. as_bool ( ) ) . unwrap_or ( true ) ;
526+ Ok ( SecondaryFileSchema { pattern, required } )
527+ }
528+ _ => Err ( serde:: de:: Error :: custom ( "Expected string or mapping for secondary file schema" ) ) ,
529+ }
530+ }
531+ }
532+
470533#[ derive( Serialize , Deserialize , Debug , PartialEq , Clone ) ]
471534#[ serde( rename_all = "camelCase" ) ]
472535pub struct Directory {
0 commit comments