1use std::{
4 ffi::{CString, c_int},
5 fmt::Display,
6};
7
8use crate::bindings::{vnet_error, vnet_error_t};
9
10#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
12pub struct VnetError(vnet_error_t);
13
14impl VnetError {
15 pub fn context<M: Display>(self, message: M) -> crate::vppinfra::error::ErrorStack {
21 unsafe {
25 let message_c = CString::new(message.to_string())
26 .expect("message should not contain nul terminator");
27 let ptr = vnet_error(self.0, c"%s".as_ptr().cast_mut().cast(), message_c.as_ptr());
28 crate::vppinfra::error::ErrorStack::from_raw(ptr)
29 }
30 }
31}
32
33impl From<c_int> for VnetError {
34 fn from(value: c_int) -> Self {
35 Self(value)
36 }
37}
38
39impl From<VnetError> for i32 {
40 fn from(value: VnetError) -> Self {
41 value.0
42 }
43}
44
45pub const VNET_ERR_INVALID_ARGUMENT: VnetError = VnetError(-73);