1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::TskReturnValue;
use thiserror::Error;
#[derive(Error, Debug, PartialEq)]
pub enum TskitError {
#[error("we received {} but expected {}",*got, *expected)]
ValueError { got: String, expected: String },
#[error("Invalid index")]
IndexError,
#[error("Not tracking samples in Trees")]
NotTrackingSamples,
#[error("{}", get_tskit_error_message(*code))]
ErrorCode { code: i32 },
#[error("{value:?}")]
MetadataError {
#[from]
value: crate::metadata::MetadataError,
},
}
pub fn panic_on_tskit_error(code: i32) {
panic_on_tskit_error!(code);
}
pub fn get_tskit_error_message(code: i32) -> String {
let c_str = unsafe { std::ffi::CStr::from_ptr(crate::bindings::tsk_strerror(code)) };
c_str.to_str().unwrap().to_owned()
}
pub fn extract_error_message(x: TskReturnValue) -> Option<String> {
x.map_or_else(|e: TskitError| Some(format!("{}", e)), |_| None)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_get_tskit_error_message() {
let m = get_tskit_error_message(0);
assert_eq!(m, "Normal exit condition. This is not an error!");
}
fn mock_error() -> TskReturnValue {
handle_tsk_return_value!(-207)
}
fn mock_success() -> TskReturnValue {
handle_tsk_return_value!(0)
}
#[test]
fn test_error_formatting() {
let x = mock_error();
let mut s: String = "nope!".to_string();
x.map_or_else(|e: TskitError| s = format!("{}", e), |_| ());
assert_eq!(s, "Individual out of bounds");
}
#[test]
fn test_extract_error_message() {
let x = mock_error();
match extract_error_message(x) {
Some(s) => assert_eq!(s, "Individual out of bounds"),
None => panic!(),
}
if extract_error_message(mock_success()).is_some() {
panic!();
}
}
}