Skip to main content

valgrind_requests/
error.rs

1//! Provide the `ClientRequestError`
2
3use core::fmt::Display;
4
5use crate::__alloc::ffi::FromVecWithNulError;
6
7/// The `ClientRequestError`
8#[derive(Debug)]
9pub enum ClientRequestError {
10    /// The error when printing with valgrind's `VALGRIND_PRINTF` fails
11    ValgrindPrintError(FromVecWithNulError),
12}
13
14impl core::error::Error for ClientRequestError {}
15
16impl From<FromVecWithNulError> for ClientRequestError {
17    fn from(value: FromVecWithNulError) -> Self {
18        Self::ValgrindPrintError(value)
19    }
20}
21
22impl Display for ClientRequestError {
23    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24        match self {
25            Self::ValgrindPrintError(inner) => {
26                write!(
27                    f,
28                    "client requests: print error: {}: '{}'",
29                    inner,
30                    crate::__alloc::string::String::from_utf8_lossy(inner.as_bytes())
31                )
32            }
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use crate::__alloc::string::ToString;
41
42    #[test]
43    fn test_client_request_error_display_valgrind_print_error() {
44        let expected = "client requests: print error: data provided contains an interior nul byte \
45                        at pos 1: 'f\0o'";
46        let error: ClientRequestError =
47            crate::__alloc::ffi::CString::from_vec_with_nul(b"f\0o".to_vec())
48                .unwrap_err()
49                .into();
50        assert_eq!(expected, error.to_string());
51    }
52}