@@ -15,9 +15,9 @@ use bindings::tsk_site_table_t;
15
15
#[ non_exhaustive]
16
16
#[ derive( Error , Debug ) ]
17
17
pub enum Error {
18
- #[ error( "{}" , 0 ) ]
18
+ #[ error( "{}" , * . 0 ) ]
19
19
Message ( String ) ,
20
- #[ error( "{}" , 0 ) ]
20
+ #[ error( "{}" , get_tskit_error_message ( * . 0 ) ) ]
21
21
Code ( i32 ) ,
22
22
}
23
23
@@ -272,3 +272,46 @@ pub fn generate_slice_mut<'a, L: Into<bindings::tsk_size_t>, I, O>(
272
272
// SAFETY: pointer is not null, length comes from C API
273
273
unsafe { std:: slice:: from_raw_parts_mut ( data. cast :: < O > ( ) , length. into ( ) as usize ) }
274
274
}
275
+
276
+ pub fn get_tskit_error_message ( code : i32 ) -> String {
277
+ let c_str = unsafe { std:: ffi:: CStr :: from_ptr ( crate :: bindings:: tsk_strerror ( code) ) } ;
278
+ c_str
279
+ . to_str ( )
280
+ . expect ( "failed to convert c_str to &str" )
281
+ . to_owned ( )
282
+ }
283
+
284
+ #[ test]
285
+ fn test_error_message ( ) {
286
+ fn foo ( ) -> Result < ( ) , Error > {
287
+ Err ( Error :: Message ( "foobar" . to_owned ( ) ) )
288
+ }
289
+
290
+ let msg = "foobar" . to_owned ( ) ;
291
+ match foo ( ) {
292
+ Err ( Error :: Message ( m) ) => assert_eq ! ( m, msg) ,
293
+ _ => panic ! ( "unexpected match" ) ,
294
+ }
295
+ }
296
+
297
+ #[ test]
298
+ fn test_error_code ( ) {
299
+ fn foo ( ) -> Result < ( ) , Error > {
300
+ Err ( Error :: Code ( -202 ) )
301
+ }
302
+
303
+ match foo ( ) {
304
+ Err ( Error :: Code ( x) ) => {
305
+ assert_eq ! ( x, -202 ) ;
306
+ }
307
+ _ => panic ! ( "unexpected match" ) ,
308
+ }
309
+
310
+ match foo ( ) {
311
+ Err ( e) => {
312
+ let m = format ! ( "{}" , e) ;
313
+ assert_eq ! ( & m, "Node out of bounds. (TSK_ERR_NODE_OUT_OF_BOUNDS)" ) ;
314
+ }
315
+ _ => panic ! ( "unexpected match" ) ,
316
+ }
317
+ }
0 commit comments