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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub enum BrokerError {
TopicError(String),
PartitionError(String),
AckError(String),
IoError(std::io::Error),
}
impl From<std::io::Error> for BrokerError {
/// Converts a standard I/O error into a BrokerError.
///
/// # Examples
///
/// ```
/// use rust_kafka_like::broker::error::BrokerError;
/// use std::io;
///
/// let io_error = io::Error::new(io::ErrorKind::Other, "an I/O error");
/// let broker_error: BrokerError = io_error.into();
/// if let BrokerError::IoError(err) = broker_error {
/// assert_eq!(err.to_string(), "an I/O error");
/// }
/// ```
fn from(error: std::io::Error) -> Self {
BrokerError::IoError(error)
}
}
impl fmt::Display for BrokerError {
/// Formats the BrokerError for display purposes.
///
/// # Examples
///
/// ```
/// use rust_kafka_like::broker::error::BrokerError;
///
/// let error = BrokerError::TopicError("topic not found".to_string());
/// assert_eq!(format!("{}", error), "Topic error: topic not found");
/// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BrokerError::TopicError(msg) => write!(f, "Topic error: {}", msg),
BrokerError::PartitionError(msg) => write!(f, "Partition error: {}", msg),
BrokerError::AckError(msg) => write!(f, "Acknowledgment error: {}", msg),
BrokerError::IoError(err) => write!(f, "IO error: {}", err),
}
}
}
impl Error for BrokerError {
/// Returns the source of the error, if any.
///
/// # Examples
///
/// ```
/// use rust_kafka_like::broker::error::BrokerError;
/// use std::error::Error;
/// use std::io;
///
/// let io_error = io::Error::new(io::ErrorKind::Other, "an I/O error");
/// let broker_error: BrokerError = io_error.into();
/// assert!(broker_error.source().is_some());
/// ```
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
BrokerError::IoError(err) => Some(err),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io;
#[test]
fn test_topic_error() {
let error = BrokerError::TopicError("Test topic error".to_string());
assert_eq!(format!("{}", error), "Topic error: Test topic error");
}
#[test]
fn test_partition_error() {
let error = BrokerError::PartitionError("Test partition error".to_string());
assert_eq!(
format!("{}", error),
"Partition error: Test partition error"
);
}
#[test]
fn test_ack_error() {
let error = BrokerError::AckError("Test ack error".to_string());
assert_eq!(format!("{}", error), "Acknowledgment error: Test ack error");
}
#[test]
fn test_io_error() {
let io_error = io::Error::new(io::ErrorKind::Other, "Test IO error");
let error = BrokerError::IoError(io_error);
assert_eq!(format!("{}", error), "IO error: Test IO error");
}
#[test]
fn test_from_io_error() {
let io_error = io::Error::new(io::ErrorKind::Other, "Test IO error");
let error: BrokerError = io_error.into();
assert_eq!(format!("{}", error), "IO error: Test IO error");
}
}