11use std:: path:: { Path , PathBuf } ;
22
3- use sha2:: { Sha256 , Digest } ;
3+ use sha2:: { Digest , Sha256 } ;
44
55use serde:: { Deserialize , Serialize } ;
66
@@ -71,17 +71,10 @@ pub(crate) fn cache_key(url: &str, git_ref: Option<&str>) -> String {
7171 let hash: String = digest. iter ( ) . take ( 8 ) . map ( |b| format ! ( "{b:02x}" ) ) . collect ( ) ;
7272
7373 // Sanitize components to prevent path traversal
74- let sanitize = |s : & str | -> String {
75- s. replace ( [ '/' , '\\' ] , "_" ) . replace ( ".." , "_" )
76- } ;
74+ let sanitize = |s : & str | -> String { s. replace ( [ '/' , '\\' ] , "_" ) . replace ( ".." , "_" ) } ;
7775
7876 // Build a human-readable prefix from the URL
79- let prefix = sanitize (
80- normalized
81- . rsplit ( '/' )
82- . next ( )
83- . unwrap_or ( "template" ) ,
84- ) ;
77+ let prefix = sanitize ( normalized. rsplit ( '/' ) . next ( ) . unwrap_or ( "template" ) ) ;
8578
8679 match git_ref {
8780 Some ( r) => format ! ( "{prefix}-{}-{hash}" , sanitize( r) ) ,
@@ -120,9 +113,10 @@ pub fn get_or_clone(url: &str, git_ref: Option<&str>) -> Result<PathBuf> {
120113 git_ref : git_ref. map ( String :: from) ,
121114 cached_at : unix_timestamp_secs ( ) ,
122115 } ;
123- let metadata_toml = toml:: to_string_pretty ( & metadata) . map_err ( |e| DicecutError :: CacheMetadata {
124- context : format ! ( "serializing cache metadata: {e}" ) ,
125- } ) ?;
116+ let metadata_toml =
117+ toml:: to_string_pretty ( & metadata) . map_err ( |e| DicecutError :: CacheMetadata {
118+ context : format ! ( "serializing cache metadata: {e}" ) ,
119+ } ) ?;
126120 std:: fs:: write ( tmp_dir. path ( ) . join ( CACHE_METADATA_FILE ) , metadata_toml) . map_err ( |e| {
127121 DicecutError :: Io {
128122 context : "writing cache metadata" . into ( ) ,
@@ -143,9 +137,7 @@ pub fn get_or_clone(url: &str, git_ref: Option<&str>) -> Result<PathBuf> {
143137 std:: fs:: rename ( tmp_dir. path ( ) , & cached_path) . or_else ( |rename_err| {
144138 // rename can fail across filesystems; fall back to copy + delete
145139 copy_dir_all ( tmp_dir. path ( ) , & cached_path) . map_err ( |e| DicecutError :: Io {
146- context : format ! (
147- "copying cloned template to cache (rename failed: {rename_err}): {e}"
148- ) ,
140+ context : format ! ( "copying cloned template to cache (rename failed: {rename_err}): {e}" ) ,
149141 source : std:: io:: Error :: other ( e. to_string ( ) ) ,
150142 } ) ?;
151143 Ok ( ( ) )
@@ -187,20 +179,18 @@ pub fn list_cached() -> Result<Vec<CachedTemplate>> {
187179 continue ;
188180 }
189181
190- let metadata_str = std:: fs:: read_to_string ( & metadata_path) . map_err ( |e| DicecutError :: Io {
191- context : format ! ( "reading cache metadata {}" , metadata_path. display( ) ) ,
192- source : e,
193- } ) ?;
182+ let metadata_str =
183+ std:: fs:: read_to_string ( & metadata_path) . map_err ( |e| DicecutError :: Io {
184+ context : format ! ( "reading cache metadata {}" , metadata_path. display( ) ) ,
185+ source : e,
186+ } ) ?;
194187
195188 let metadata: CacheMetadata =
196189 toml:: from_str ( & metadata_str) . map_err ( |e| DicecutError :: CacheMetadata {
197190 context : format ! ( "parsing cache metadata: {e}" ) ,
198191 } ) ?;
199192
200- let key = entry
201- . file_name ( )
202- . to_string_lossy ( )
203- . into_owned ( ) ;
193+ let key = entry. file_name ( ) . to_string_lossy ( ) . into_owned ( ) ;
204194
205195 entries. push ( CachedTemplate {
206196 key,
@@ -364,9 +354,18 @@ mod tests {
364354
365355 #[ test]
366356 fn normalize_url_strips_trailing_git_and_slash ( ) {
367- assert_eq ! ( normalize_url( "https://github.com/user/repo.git" ) , "https://github.com/user/repo" ) ;
368- assert_eq ! ( normalize_url( "https://github.com/user/repo/" ) , "https://github.com/user/repo" ) ;
369- assert_eq ! ( normalize_url( "https://github.com/user/repo" ) , "https://github.com/user/repo" ) ;
357+ assert_eq ! (
358+ normalize_url( "https://github.com/user/repo.git" ) ,
359+ "https://github.com/user/repo"
360+ ) ;
361+ assert_eq ! (
362+ normalize_url( "https://github.com/user/repo/" ) ,
363+ "https://github.com/user/repo"
364+ ) ;
365+ assert_eq ! (
366+ normalize_url( "https://github.com/user/repo" ) ,
367+ "https://github.com/user/repo"
368+ ) ;
370369 }
371370
372371 #[ test]
0 commit comments