iai_callgrind/client_requests/
error.rs

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
//! Provide the `ClientRequestError`

use core::fmt::Display;
use std::ffi::FromVecWithNulError;

/// The `ClientRequestError`
#[derive(Debug)]
pub enum ClientRequestError {
    /// The error when printing with valgrind's `VALGRIND_PRINTF` fails
    ValgrindPrintError(FromVecWithNulError),
}

impl std::error::Error for ClientRequestError {}

impl From<FromVecWithNulError> for ClientRequestError {
    fn from(value: FromVecWithNulError) -> Self {
        ClientRequestError::ValgrindPrintError(value)
    }
}

impl Display for ClientRequestError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ClientRequestError::ValgrindPrintError(inner) => {
                write!(
                    f,
                    "client requests: print error: {}: '{}'",
                    inner,
                    String::from_utf8_lossy(inner.as_bytes())
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_client_request_error_display_valgrind_print_error() {
        let expected = "client requests: print error: data provided contains an interior nul byte \
                        at pos 1: 'f\0o'";
        let error: ClientRequestError = std::ffi::CString::from_vec_with_nul(b"f\0o".to_vec())
            .unwrap_err()
            .into();
        assert_eq!(expected, error.to_string());
    }
}