11use anyhow:: Result ;
2+ use chrono:: Utc ;
23use serialport:: SerialPort ;
34use std:: sync:: Arc ;
45use std:: sync:: atomic:: { AtomicBool , Ordering } ;
5- use tokio:: sync:: { mpsc, Mutex } ;
6- use chrono:: Utc ;
6+ use tokio:: sync:: { Mutex , mpsc} ;
77
88#[ derive( Debug , Clone ) ]
99pub enum SerialData {
@@ -82,34 +82,34 @@ impl SerialReader {
8282 fn format_hex_data ( & mut self , bytes : & [ u8 ] ) -> String {
8383 let capacity = if self . log_ts { 32 } else { 0 } + bytes. len ( ) * 3 ; // Estimate capacity
8484 let mut hex_str = String :: with_capacity ( capacity) ;
85-
85+
8686 if self . log_ts {
8787 hex_str. push_str ( "[" ) ;
8888 hex_str. push_str ( & Utc :: now ( ) . format ( "%Y-%m-%d %H:%M:%S%.3f" ) . to_string ( ) ) ;
8989 hex_str. push_str ( "] " ) ;
9090 }
91-
91+
9292 // Optimize hex formatting with pre-allocated string
9393 for ( i, b) in bytes. iter ( ) . enumerate ( ) {
9494 if i > 0 {
9595 hex_str. push ( ' ' ) ;
9696 }
9797 hex_str. push_str ( & format ! ( "{:02X}" , b) ) ;
9898 }
99-
99+
100100 hex_str
101101 }
102102
103103 fn format_text_data ( & mut self , bytes : & [ u8 ] ) -> String {
104104 let capacity = if self . log_ts { 32 } else { 0 } + bytes. len ( ) ;
105105 let mut text = String :: with_capacity ( capacity) ;
106-
106+
107107 if self . log_ts {
108108 text. push_str ( "[" ) ;
109109 text. push_str ( & Utc :: now ( ) . format ( "%Y-%m-%d %H:%M:%S%.3f" ) . to_string ( ) ) ;
110110 text. push_str ( "] " ) ;
111111 }
112-
112+
113113 // Use from_utf8_lossy but avoid extra allocations where possible
114114 text. push_str ( & String :: from_utf8_lossy ( bytes) ) ;
115115 text
@@ -119,11 +119,11 @@ impl SerialReader {
119119 if let Some ( w) = & self . rx_log_writer {
120120 if let Ok ( mut lw) = w. lock ( ) {
121121 use std:: io:: Write ;
122-
122+
123123 if self . log_ts {
124124 let _ = write ! ( lw, "[{}] " , Utc :: now( ) . format( "%Y-%m-%d %H:%M:%S%.3f" ) ) ;
125125 }
126-
126+
127127 if self . hex_mode {
128128 for ( i, b) in bytes. iter ( ) . enumerate ( ) {
129129 let separator = if i + 1 == bytes. len ( ) { "" } else { " " } ;
@@ -133,7 +133,7 @@ impl SerialReader {
133133 } else {
134134 let _ = lw. write_all ( bytes) ;
135135 }
136-
136+
137137 let _ = lw. flush ( ) ;
138138 }
139139 }
@@ -148,4 +148,4 @@ pub async fn write_bytes_async(
148148 guard. write_all ( bytes) ?;
149149 guard. flush ( ) ?;
150150 Ok ( ( ) )
151- }
151+ }
0 commit comments