1use std::ffi::CStr;
2use std::fmt::Display;
3use std::fmt::Formatter;
4use std::io::Error;
5use std::io::ErrorKind;
6use std::str;
7
8pub use lz4_sys::*;
9
10#[derive(Debug)]
11pub struct LZ4Error(String);
12
13impl Display for LZ4Error {
14 fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
15 write!(f, "LZ4 error: {}", &self.0)
16 }
17}
18
19impl ::std::error::Error for LZ4Error {
20 fn description(&self) -> &str {
21 &self.0
22 }
23
24 fn cause(&self) -> Option<&dyn (::std::error::Error)> {
25 None
26 }
27}
28
29pub fn check_error(code: LZ4FErrorCode) -> Result<usize, Error> {
30 unsafe {
31 if LZ4F_isError(code) != 0 {
32 let error_name = LZ4F_getErrorName(code);
33 return Err(Error::new(
34 ErrorKind::Other,
35 LZ4Error(
36 str::from_utf8(CStr::from_ptr(error_name).to_bytes())
37 .unwrap()
38 .to_string(),
39 ),
40 ));
41 }
42 }
43 Ok(code as usize)
44}
45
46pub fn version() -> i32 {
47 unsafe { LZ4_versionNumber() }
48}
49
50#[test]
51fn test_version_number() {
52 version();
53}