1
- use super :: QplibParseError ;
1
+ use super :: { ParseErrorReason , QplibParseError } ;
2
2
use std:: collections:: HashMap ;
3
3
use std:: {
4
4
fmt:: Display ,
@@ -8,7 +8,7 @@ use std::{
8
8
str:: FromStr ,
9
9
} ;
10
10
11
- pub type Result < T > = std :: result :: Result < T , QplibParseError > ;
11
+ use anyhow :: { Context , Result } ;
12
12
13
13
#[ derive( Default , Debug ) ]
14
14
pub struct QplibFile {
@@ -49,7 +49,9 @@ pub struct QplibFile {
49
49
50
50
impl QplibFile {
51
51
pub fn from_file ( path : impl AsRef < Path > ) -> Result < Self > {
52
- let f = fs:: File :: open ( path) ?;
52
+ let path = path. as_ref ( ) ;
53
+ let f = fs:: File :: open ( path)
54
+ . with_context ( || format ! ( "Failed to read file {}" , path. display( ) ) ) ?;
53
55
Self :: from_reader ( f)
54
56
}
55
57
@@ -70,7 +72,7 @@ impl QplibFile {
70
72
. split_whitespace ( )
71
73
. next ( )
72
74
. map ( |s| s. to_string ( ) )
73
- . ok_or ( QplibParseError :: InvalidLine ( cursor. line_num ) ) ?;
75
+ . ok_or ( QplibParseError :: invalid_line ( cursor. line_num ) ) ?;
74
76
let ProblemType ( okind, vkind, ckind) = cursor. next_parse ( ) ?;
75
77
let sense = cursor. next_parse ( ) ?;
76
78
let num_vars = cursor. next_parse ( ) ?;
@@ -276,10 +278,10 @@ impl Display for ProbConstrKind {
276
278
}
277
279
278
280
impl FromStr for ProblemType {
279
- type Err = QplibParseError ;
281
+ type Err = ParseErrorReason ;
280
282
281
- fn from_str ( s : & str ) -> Result < Self > {
282
- let err_out = || QplibParseError :: InvalidProblemType ( s. to_owned ( ) ) ;
283
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
284
+ let err_out = || ParseErrorReason :: InvalidProblemType ( s. to_owned ( ) ) ;
283
285
let mut chars = s. chars ( ) ;
284
286
let ( ( o, v) , c) = chars
285
287
. next ( )
@@ -322,13 +324,13 @@ pub enum ObjSense {
322
324
}
323
325
324
326
impl FromStr for ObjSense {
325
- type Err = QplibParseError ;
327
+ type Err = ParseErrorReason ;
326
328
327
- fn from_str ( s : & str ) -> Result < Self > {
329
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
328
330
match s. to_lowercase ( ) . as_str ( ) {
329
331
"minimize" => Ok ( Self :: Minimize ) ,
330
332
"maximize" => Ok ( Self :: Maximize ) ,
331
- _ => Err ( QplibParseError :: InvalidObjSense ( s. to_owned ( ) ) ) ,
333
+ _ => Err ( ParseErrorReason :: InvalidObjSense ( s. to_owned ( ) ) ) ,
332
334
}
333
335
}
334
336
}
@@ -342,14 +344,14 @@ pub enum VarType {
342
344
}
343
345
344
346
impl FromStr for VarType {
345
- type Err = QplibParseError ;
347
+ type Err = ParseErrorReason ;
346
348
347
- fn from_str ( s : & str ) -> Result < Self > {
349
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
348
350
match s {
349
351
"0" => Ok ( VarType :: Continuous ) ,
350
352
"1" => Ok ( VarType :: Integer ) ,
351
353
"2" => Ok ( VarType :: Binary ) ,
352
- _ => Err ( QplibParseError :: InvalidVarType ( s. to_owned ( ) ) ) ,
354
+ _ => Err ( ParseErrorReason :: InvalidVarType ( s. to_owned ( ) ) ) ,
353
355
}
354
356
}
355
357
}
@@ -403,30 +405,30 @@ where
403
405
return Ok ( s) ;
404
406
}
405
407
}
406
- Err ( QplibParseError :: UnexpectedEndOfFile ( self . line_num ) )
408
+ Err ( QplibParseError :: unexpected_eof ( self . line_num ) . into ( ) )
407
409
}
408
410
409
411
fn parse_or_err_with_line < T , E > ( & self , raw : & str ) -> Result < T >
410
412
where
411
413
T : FromStr < Err = E > ,
412
- E : Into < QplibParseError > ,
414
+ E : Into < ParseErrorReason > ,
413
415
{
414
416
raw. parse :: < T > ( )
415
- . map_err ( |e| e. into ( ) . with_line ( self . line_num ) )
417
+ . map_err ( |e| e. into ( ) . with_line ( self . line_num ) . into ( ) )
416
418
}
417
419
418
420
/// Consumes the next line and tries to parse the first value
419
421
/// in it (determined by whitespace).
420
422
fn next_parse < T , E > ( & mut self ) -> Result < T >
421
423
where
422
424
T : FromStr < Err = E > ,
423
- E : Into < QplibParseError > ,
425
+ E : Into < ParseErrorReason > ,
424
426
{
425
427
let line = self . expect_next ( ) ?;
426
428
let val = line
427
429
. split_whitespace ( )
428
430
. next ( )
429
- . ok_or ( QplibParseError :: InvalidLine ( self . line_num ) ) ?;
431
+ . ok_or ( QplibParseError :: invalid_line ( self . line_num ) ) ?;
430
432
self . parse_or_err_with_line ( val)
431
433
}
432
434
@@ -450,12 +452,12 @@ where
450
452
& mut self ,
451
453
// number of "segments" to split line into.
452
454
segments : usize ,
453
- f : impl Fn ( Vec < String > ) -> Result < ( K , V ) > ,
455
+ f : impl Fn ( Vec < String > ) -> Result < ( K , V ) , ParseErrorReason > ,
454
456
) -> Result < HashMap < K , V > >
455
457
where
456
458
K : Eq + std:: hash:: Hash ,
457
459
V : FromStr < Err = E > ,
458
- QplibParseError : From < E > ,
460
+ ParseErrorReason : From < E > ,
459
461
{
460
462
let num = self . next_parse ( ) ?;
461
463
let mut out = HashMap :: with_capacity ( num) ;
@@ -475,7 +477,7 @@ where
475
477
fn collect_i_val < V , E > ( & mut self ) -> Result < HashMap < usize , V > >
476
478
where
477
479
V : FromStr < Err = E > ,
478
- QplibParseError : From < E > ,
480
+ ParseErrorReason : From < E > ,
479
481
{
480
482
self . consume_map ( 2 , |parts| {
481
483
let key = parts[ 0 ] . parse :: < usize > ( ) ? - 1 ;
@@ -511,7 +513,7 @@ where
511
513
size : usize ,
512
514
// number of "segments" to split line into.
513
515
segments : usize ,
514
- f : impl Fn ( Vec < String > ) -> Result < ( usize , K , f64 ) > ,
516
+ f : impl Fn ( Vec < String > ) -> Result < ( usize , K , f64 ) , ParseErrorReason > ,
515
517
) -> Result < Vec < HashMap < K , f64 > > >
516
518
where
517
519
K : Eq + std:: hash:: Hash + Clone ,
@@ -566,7 +568,7 @@ where
566
568
fn collect_list < V , E > ( & mut self , size : usize ) -> Result < Vec < V > >
567
569
where
568
570
V : FromStr < Err = E > + Clone ,
569
- E : Into < QplibParseError > ,
571
+ E : Into < ParseErrorReason > ,
570
572
{
571
573
let default: V = self . next_parse ( ) ?;
572
574
let mut out = vec ! [ default ; size] ;
0 commit comments