@@ -21,17 +21,27 @@ pub fn image(args: &SpaceImageArgs) -> Result<()> {
2121
2222pub fn image_with ( args : & SpaceImageArgs , api : & dyn BacklogApi ) -> Result < ( ) > {
2323 let ( bytes, filename) = api. download_space_image ( ) ?;
24- let path = args. output . clone ( ) . unwrap_or_else ( || {
25- let base = std:: path:: Path :: new ( & filename)
26- . file_name ( )
27- . unwrap_or ( std:: ffi:: OsStr :: new ( "space_image" ) ) ;
28- PathBuf :: from ( base)
29- } ) ;
24+ let path = args
25+ . output
26+ . clone ( )
27+ . unwrap_or_else ( || default_output_path ( & filename) ) ;
3028 std:: fs:: write ( & path, & bytes) . with_context ( || format ! ( "Failed to write {}" , path. display( ) ) ) ?;
3129 println ! ( "Saved: {} ({} bytes)" , path. display( ) , bytes. len( ) ) ;
3230 Ok ( ( ) )
3331}
3432
33+ fn default_output_path ( filename : & str ) -> PathBuf {
34+ let effective = if filename. is_empty ( ) || filename == "attachment" {
35+ "space_image"
36+ } else {
37+ filename
38+ } ;
39+ let base = std:: path:: Path :: new ( effective)
40+ . file_name ( )
41+ . unwrap_or ( std:: ffi:: OsStr :: new ( "space_image" ) ) ;
42+ PathBuf :: from ( base)
43+ }
44+
3545#[ cfg( test) ]
3646mod tests {
3747 use super :: * ;
@@ -95,4 +105,25 @@ mod tests {
95105 let err = image_with ( & args ( None ) , & api) . unwrap_err ( ) ;
96106 assert ! ( err. to_string( ) . contains( "download failed" ) ) ;
97107 }
108+
109+ #[ test]
110+ fn default_output_path_uses_server_filename ( ) {
111+ assert_eq ! (
112+ default_output_path( "space_image.png" ) ,
113+ PathBuf :: from( "space_image.png" )
114+ ) ;
115+ }
116+
117+ #[ test]
118+ fn default_output_path_falls_back_for_attachment ( ) {
119+ assert_eq ! (
120+ default_output_path( "attachment" ) ,
121+ PathBuf :: from( "space_image" )
122+ ) ;
123+ }
124+
125+ #[ test]
126+ fn default_output_path_falls_back_for_empty ( ) {
127+ assert_eq ! ( default_output_path( "" ) , PathBuf :: from( "space_image" ) ) ;
128+ }
98129}
0 commit comments