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
132
133
134
135
136
137
138
139
140
141
use super::data::{vec_get, vec_size};
use graphannis::errors;
use libc::{c_char, size_t};
use log;
use std;
use std::error::Error as StdError;
use std::ffi::CString;
pub struct Error {
pub msg: CString,
pub kind: CString,
}
pub type ErrorList = Vec<Error>;
struct CauseIterator<'a> {
current: Option<&'a dyn StdError>,
}
impl<'a> std::iter::Iterator for CauseIterator<'a> {
type Item = Error;
fn next(&mut self) -> std::option::Option<Error> {
let std_error = self.current?;
let result = Error {
msg: CString::new(std_error.to_string()).unwrap_or(CString::default()),
kind: CString::new("Cause").unwrap_or(CString::default()),
};
self.current = std_error.source();
Some(result)
}
}
fn error_kind(e: &Box<dyn StdError>) -> &'static str {
if let Some(annis_err) = e.downcast_ref::<errors::GraphAnnisError>() {
match annis_err {
errors::GraphAnnisError::AQLSyntaxError { .. } => "AQLSyntaxError",
errors::GraphAnnisError::AQLSemanticError { .. } => "AQLSemanticError",
errors::GraphAnnisError::LoadingGraphFailed { .. } => "LoadingGraphFailed",
errors::GraphAnnisError::ImpossibleSearch(_) => "ImpossibleSearch",
errors::GraphAnnisError::NoSuchCorpus(_) => "NoSuchCorpus",
errors::GraphAnnisError::CorpusExists(_) => "CorpusExists",
}
} else {
if e.is::<std::io::Error>() {
"IO"
} else if e.is::<log::SetLoggerError>() {
"SetLoggerError"
} else {
"Unknown"
}
}
}
pub fn create_error_list(e: Box<dyn StdError>) -> ErrorList {
let mut result = ErrorList::new();
result.push(Error {
msg: CString::new(e.to_string()).unwrap_or(CString::default()),
kind: CString::new(error_kind(&e)).unwrap_or(CString::default()),
});
let cause_it = CauseIterator {
current: e.source(),
};
for e in cause_it {
result.push(e)
}
result
}
impl From<log::SetLoggerError> for Error {
fn from(e: log::SetLoggerError) -> Error {
let err = if let Ok(error_msg) = CString::new(e.to_string()) {
Error {
msg: error_msg,
kind: CString::new("SetLoggerError").unwrap(),
}
} else {
Error {
msg: CString::new(String::from("Some error occurred")).unwrap(),
kind: CString::new("SetLoggerError").unwrap(),
}
};
return err;
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
let err = if let Ok(error_msg) = CString::new(e.to_string()) {
Error {
msg: error_msg,
kind: CString::new("std::io::Error").unwrap(),
}
} else {
Error {
msg: CString::new(String::from("Some error occurred")).unwrap(),
kind: CString::new("std::io::Error").unwrap(),
}
};
return err;
}
}
pub fn new(err: Box<dyn StdError>) -> *mut ErrorList {
Box::into_raw(Box::new(create_error_list(err)))
}
#[no_mangle]
pub extern "C" fn annis_error_size(ptr: *const ErrorList) -> size_t {
vec_size(ptr)
}
#[no_mangle]
pub extern "C" fn annis_error_get_msg(ptr: *const ErrorList, i: size_t) -> *const c_char {
let item = vec_get(ptr, i);
if item.is_null() {
return std::ptr::null();
}
let err: &Error = cast_const!(item);
return err.msg.as_ptr();
}
#[no_mangle]
pub extern "C" fn annis_error_get_kind(ptr: *const ErrorList, i: size_t) -> *const c_char {
let item = vec_get(ptr, i);
if item.is_null() {
return std::ptr::null();
}
let err: &Error = cast_const!(item);
return err.kind.as_ptr();
}