@@ -7,13 +7,32 @@ use rs_poker::open_hand_history::{HandHistory, OpenHandHistoryWrapper};
77pub enum ReaderError {
88 #[ error( "failed to read file: {0}" ) ]
99 Io ( #[ from] std:: io:: Error ) ,
10- #[ error( "failed to parse line {line} : {source }" ) ]
10+ #[ error( "failed to parse {path}:{ line} ({source}) \n line preview : {preview }" ) ]
1111 Parse {
12+ path : std:: path:: PathBuf ,
1213 line : usize ,
14+ preview : String ,
1315 source : serde_json:: Error ,
1416 } ,
1517}
1618
19+ /// Maximum number of characters of a malformed line to include in the
20+ /// error message. Long enough to spot interleaves or truncation, short
21+ /// enough not to dump a whole hand record into the terminal.
22+ const PREVIEW_LEN : usize = 160 ;
23+
24+ fn line_preview ( line : & str ) -> String {
25+ let mut out = String :: new ( ) ;
26+ for ( i, c) in line. chars ( ) . enumerate ( ) {
27+ if i >= PREVIEW_LEN {
28+ out. push ( '…' ) ;
29+ break ;
30+ }
31+ out. push ( c) ;
32+ }
33+ out
34+ }
35+
1736/// Read all `.ohh` files from a directory (sorted by filename).
1837///
1938/// Files that do not have an `.ohh` extension are skipped. Without this
@@ -57,7 +76,9 @@ pub fn read_ohh_file(path: &Path) -> Result<Vec<HandHistory>, ReaderError> {
5776 }
5877 let wrapper: OpenHandHistoryWrapper =
5978 serde_json:: from_str ( trimmed) . map_err ( |e| ReaderError :: Parse {
79+ path : path. to_path_buf ( ) ,
6080 line : line_num + 1 ,
81+ preview : line_preview ( trimmed) ,
6182 source : e,
6283 } ) ?;
6384 hands. push ( wrapper. ohh ) ;
0 commit comments