@@ -3,6 +3,7 @@ use crate::validation::{validate_file_path, validate_output_path};
33use langcodec:: {
44 Codec , FormatType , KeyStyle , NormalizeOptions as EngineNormalizeOptions , normalize_codec,
55} ;
6+ use std:: path:: Path ;
67
78#[ derive( Debug , Clone ) ]
89pub struct NormalizeCliOptions {
@@ -61,6 +62,12 @@ fn write_back(codec: &Codec, input_path: &str, output_path: &Option<String>) ->
6162 }
6263}
6364
65+ fn has_distinct_output_path ( input_path : & str , output_path : & Option < String > ) -> bool {
66+ output_path
67+ . as_ref ( )
68+ . is_some_and ( |output| Path :: new ( output) != Path :: new ( input_path) )
69+ }
70+
6471pub fn run_normalize_command ( opts : NormalizeCliOptions ) -> Result < ( ) , String > {
6572 let expanded = path_glob:: expand_input_globs ( & opts. inputs )
6673 . map_err ( |e| format ! ( "Failed to expand input patterns: {}" , e) ) ?;
@@ -73,9 +80,6 @@ pub fn run_normalize_command(opts: NormalizeCliOptions) -> Result<(), String> {
7380
7481 let input = & expanded[ 0 ] ;
7582 validate_file_path ( input) ?;
76- if let Some ( output) = & opts. output {
77- validate_output_path ( output) ?;
78- }
7983
8084 let mut codec = Codec :: new ( ) ;
8185 codec
@@ -92,21 +96,44 @@ pub fn run_normalize_command(opts: NormalizeCliOptions) -> Result<(), String> {
9296 )
9397 . map_err ( |e| e. to_string ( ) ) ?;
9498
95- if !report. changed {
99+ if opts. check {
100+ if report. changed {
101+ println ! ( "would change: {}" , input) ;
102+ return Err ( format ! ( "would change: {}" , input) ) ;
103+ }
104+
96105 println ! ( "No changes needed: {}" , input) ;
97106 return Ok ( ( ) ) ;
98107 }
99108
100- if opts. check {
101- println ! ( "would change: {}" , input) ;
102- return Err ( format ! ( "would change: {}" , input) ) ;
109+ if opts. dry_run {
110+ if report. changed {
111+ println ! ( "DRY-RUN: would change {}" , input) ;
112+ } else {
113+ println ! ( "No changes needed: {}" , input) ;
114+ }
115+ return Ok ( ( ) ) ;
103116 }
104117
105- if opts. dry_run {
106- println ! ( "DRY-RUN: would change {}" , input) ;
118+ if !report. changed {
119+ if has_distinct_output_path ( input, & opts. output ) {
120+ if let Some ( output) = & opts. output {
121+ validate_output_path ( output) ?;
122+ }
123+ write_back ( & codec, input, & opts. output ) ?;
124+ println ! ( "No changes needed: {}" , input) ;
125+ println ! ( "✅ Wrote output: {}" , opts. output. as_deref( ) . unwrap_or( input) ) ;
126+ return Ok ( ( ) ) ;
127+ }
128+
129+ println ! ( "No changes needed: {}" , input) ;
107130 return Ok ( ( ) ) ;
108131 }
109132
133+ if let Some ( output) = & opts. output {
134+ validate_output_path ( output) ?;
135+ }
136+
110137 write_back ( & codec, input, & opts. output ) ?;
111138 println ! ( "✅ Normalized: {}" , opts. output. as_deref( ) . unwrap_or( input) ) ;
112139 Ok ( ( ) )
0 commit comments