rperf3/
error.rs

1use thiserror::Error;
2
3/// Error types for rperf3 operations.
4///
5/// This enum covers all error cases that can occur during network performance testing,
6/// from I/O errors to protocol violations.
7///
8/// # Examples
9///
10/// ```
11/// use rperf3::Error;
12///
13/// fn check_config(server_addr: Option<String>) -> Result<(), Error> {
14///     match server_addr {
15///         Some(_) => Ok(()),
16///         None => Err(Error::Config("Server address required".to_string())),
17///     }
18/// }
19/// ```
20#[derive(Error, Debug)]
21pub enum Error {
22    /// I/O error occurred during network operations.
23    ///
24    /// This wraps standard `std::io::Error` and is used for all I/O-related failures
25    /// such as socket errors, connection failures, and read/write errors.
26    #[error("IO error: {0}")]
27    Io(#[from] std::io::Error),
28
29    /// JSON serialization or deserialization error.
30    ///
31    /// Occurs when encoding or decoding JSON data for protocol messages or output.
32    #[error("JSON error: {0}")]
33    Json(#[from] serde_json::Error),
34
35    /// Connection-related error.
36    ///
37    /// Used for errors specific to establishing or maintaining network connections,
38    /// such as connection refused, timeout, or unexpected disconnection.
39    #[error("Connection error: {0}")]
40    Connection(String),
41
42    /// Protocol violation or parsing error.
43    ///
44    /// Occurs when the client and server exchange malformed or unexpected messages.
45    #[error("Protocol error: {0}")]
46    Protocol(String),
47
48    /// Configuration error.
49    ///
50    /// Used when the provided configuration is invalid or incomplete.
51    #[error("Configuration error: {0}")]
52    Config(String),
53
54    /// Test execution error.
55    ///
56    /// Covers errors that occur during test execution that don't fit other categories.
57    #[error("Test error: {0}")]
58    Test(String),
59}
60
61/// Result type alias for rperf3 operations.
62///
63/// This is a convenience type alias that uses [`enum@Error`] as the error type.
64/// Most functions in this library return this type.
65///
66/// # Examples
67///
68/// ```
69/// use rperf3::{Result, Error};
70///
71/// fn validate_port(port: u16) -> Result<()> {
72///     if port < 1024 {
73///         Err(Error::Config("Port must be >= 1024".to_string()))
74///     } else {
75///         Ok(())
76///     }
77/// }
78/// ```
79pub type Result<T> = std::result::Result<T, Error>;