Skip to main content

llama_cpp_bindings/error/
decode_error.rs

1use std::num::NonZeroI32;
2use std::os::raw::c_int;
3
4#[derive(Debug, Eq, PartialEq, thiserror::Error)]
5pub enum DecodeError {
6    #[error("no KV cache slot was available")]
7    NoKvCacheSlot,
8    #[error("decode aborted by callback")]
9    Aborted,
10    #[error("decode batch is invalid (empty, output mismatch, or initialization failure)")]
11    BatchInvalid,
12    #[error("decode ran out of memory")]
13    DecodeOutOfMemory,
14    #[error("backend compute failed during decode")]
15    ComputeFailed,
16    #[error("decode returned an unknown status code: {code}")]
17    UnknownStatus { code: c_int },
18    #[error("not enough memory")]
19    NotEnoughMemory,
20    #[error("{message}")]
21    Reported { message: String },
22}
23
24impl From<NonZeroI32> for DecodeError {
25    fn from(value: NonZeroI32) -> Self {
26        match value.get() {
27            1 => Self::NoKvCacheSlot,
28            2 => Self::Aborted,
29            -1 => Self::BatchInvalid,
30            error_code => Self::UnknownStatus { code: error_code },
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use std::num::NonZeroI32;
38
39    use super::DecodeError;
40
41    #[test]
42    fn no_kv_cache_slot_maps_from_code_one() {
43        let error = DecodeError::from(NonZeroI32::new(1).expect("1 is non-zero"));
44
45        assert_eq!(error, DecodeError::NoKvCacheSlot);
46    }
47
48    #[test]
49    fn aborted_maps_from_code_two() {
50        let error = DecodeError::from(NonZeroI32::new(2).expect("2 is non-zero"));
51
52        assert_eq!(error, DecodeError::Aborted);
53    }
54
55    #[test]
56    fn batch_invalid_maps_from_code_negative_one() {
57        let error = DecodeError::from(NonZeroI32::new(-1).expect("-1 is non-zero"));
58
59        assert_eq!(error, DecodeError::BatchInvalid);
60    }
61
62    #[test]
63    fn unrecognized_code_falls_through_to_unknown_status() {
64        let error = DecodeError::from(NonZeroI32::new(42).expect("42 is non-zero"));
65
66        assert_eq!(error, DecodeError::UnknownStatus { code: 42 });
67    }
68}