iai_callgrind/client_requests/
error.rs

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