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
use std::os::raw::c_int;

/// Simple wrapper around minilzo error types
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    Error,
    OutOfMemory,
    NotCompressible,
    InputOverrun,
    OutputOverrun,
    LookbehindOverrun,
    EOFNotFound,
    InputNotConsumed,
    NotYetImplemented,
    InvalidArgument,
    InvalidAlignment,
    OutputNotConsumed,
    InternalError,
    Unknown,
}

impl From<c_int> for Error {
    fn from(value: c_int) -> Self {
        match value {
            -1 => Error::Error,
            -2 => Error::OutOfMemory,
            -3 => Error::NotCompressible,
            -4 => Error::InputOverrun,
            -5 => Error::OutputOverrun,
            -6 => Error::LookbehindOverrun,
            -7 => Error::EOFNotFound,
            -8 => Error::InputNotConsumed,
            -9 => Error::NotYetImplemented,
            -10 => Error::InvalidArgument,
            -11 => Error::InvalidAlignment,
            -12 => Error::OutputNotConsumed,
            -99 => Error::InternalError,
            _ => Error::Unknown,
        }
    }
}

pub type Result<T> = std::result::Result<T, Error>;

/// Simple internal check function
pub fn check(value: c_int) -> Result<()> {
    if value == 0 {
        Ok(())
    } else {
        Err(value.into())
    }
}