#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum CommunicationMode {
#[default]
Sync,
Async,
Oneway,
}
impl CommunicationMode {
#[inline]
pub const fn as_str(self) -> &'static str {
match self {
CommunicationMode::Sync => "SYNC",
CommunicationMode::Async => "ASYNC",
CommunicationMode::Oneway => "ONEWAY",
}
}
}
impl std::fmt::Display for CommunicationMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_communication_mode_default() {
let default = CommunicationMode::default();
assert_eq!(default, CommunicationMode::Sync);
}
#[test]
fn test_communication_mode_equality() {
assert_eq!(CommunicationMode::Sync, CommunicationMode::Sync);
assert_eq!(CommunicationMode::Async, CommunicationMode::Async);
assert_eq!(CommunicationMode::Oneway, CommunicationMode::Oneway);
assert_ne!(CommunicationMode::Sync, CommunicationMode::Async);
assert_ne!(CommunicationMode::Sync, CommunicationMode::Oneway);
assert_ne!(CommunicationMode::Async, CommunicationMode::Oneway);
}
#[test]
fn test_communication_mode_clone() {
let sync_mode = CommunicationMode::Sync;
let cloned_sync = sync_mode;
assert_eq!(sync_mode, cloned_sync);
let async_mode = CommunicationMode::Async;
let cloned_async = async_mode;
assert_eq!(async_mode, cloned_async);
let oneway_mode = CommunicationMode::Oneway;
let cloned_oneway = oneway_mode;
assert_eq!(oneway_mode, cloned_oneway);
}
#[test]
fn test_communication_mode_copy() {
let original = CommunicationMode::Async;
let copied = original; assert_eq!(original, copied);
assert_eq!(original, CommunicationMode::Async);
}
#[test]
fn test_communication_mode_debug() {
let sync_debug = format!("{:?}", CommunicationMode::Sync);
assert_eq!(sync_debug, "Sync");
let async_debug = format!("{:?}", CommunicationMode::Async);
assert_eq!(async_debug, "Async");
let oneway_debug = format!("{:?}", CommunicationMode::Oneway);
assert_eq!(oneway_debug, "Oneway");
}
#[test]
fn display_matches_java_enum_names() {
assert_eq!(CommunicationMode::Sync.to_string(), "SYNC");
assert_eq!(CommunicationMode::Async.to_string(), "ASYNC");
assert_eq!(CommunicationMode::Oneway.to_string(), "ONEWAY");
}
#[test]
fn test_communication_mode_pattern_matching() {
let sync = CommunicationMode::Sync;
let async_mode = CommunicationMode::Async;
let oneway = CommunicationMode::Oneway;
match sync {
CommunicationMode::Sync => {
}
_ => panic!("Pattern matching failed for Sync"),
}
match async_mode {
CommunicationMode::Async => {
}
_ => panic!("Pattern matching failed for Async"),
}
match oneway {
CommunicationMode::Oneway => {
}
_ => panic!("Pattern matching failed for Oneway"),
}
}
#[test]
fn test_communication_mode_exhaustive_match() {
fn process_mode(mode: CommunicationMode) -> &'static str {
match mode {
CommunicationMode::Sync => "synchronous",
CommunicationMode::Async => "asynchronous",
CommunicationMode::Oneway => "oneway",
}
}
assert_eq!(process_mode(CommunicationMode::Sync), "synchronous");
assert_eq!(process_mode(CommunicationMode::Async), "asynchronous");
assert_eq!(process_mode(CommunicationMode::Oneway), "oneway");
}
#[test]
fn test_communication_mode_in_collections() {
let modes = [
CommunicationMode::Sync,
CommunicationMode::Async,
CommunicationMode::Oneway,
CommunicationMode::Sync, ];
assert_eq!(modes.len(), 4);
assert_eq!(modes[0], CommunicationMode::Sync);
assert_eq!(modes[1], CommunicationMode::Async);
assert_eq!(modes[2], CommunicationMode::Oneway);
assert_eq!(modes[3], CommunicationMode::Sync);
let unique_count = modes.iter().fold(0, |acc, mode| {
if modes.iter().filter(|&m| m == mode).count() == 1 {
acc + 1
} else {
acc
}
});
assert_eq!(unique_count, 2); }
#[test]
fn test_communication_mode_option() {
let some_mode: Option<CommunicationMode> = Some(CommunicationMode::Sync);
let none_mode: Option<CommunicationMode> = None;
assert!(some_mode.is_some());
if let Some(mode) = some_mode {
assert_eq!(mode, CommunicationMode::Sync);
}
assert!(none_mode.is_none());
}
#[test]
fn test_communication_mode_result() {
fn get_mode(valid: bool) -> Result<CommunicationMode, &'static str> {
if valid {
Ok(CommunicationMode::Async)
} else {
Err("Invalid mode")
}
}
let valid_result = get_mode(true);
assert!(valid_result.is_ok());
assert_eq!(valid_result.unwrap(), CommunicationMode::Async);
let invalid_result = get_mode(false);
assert!(invalid_result.is_err());
}
#[test]
fn test_all_variants_distinct() {
let variants = [
CommunicationMode::Sync,
CommunicationMode::Async,
CommunicationMode::Oneway,
];
for (i, variant1) in variants.iter().enumerate() {
for (j, variant2) in variants.iter().enumerate() {
if i == j {
assert_eq!(variant1, variant2);
} else {
assert_ne!(variant1, variant2);
}
}
}
}
#[test]
fn test_communication_mode_in_struct() {
#[derive(Debug, Clone, PartialEq)]
struct MessageConfig {
mode: CommunicationMode,
timeout_ms: u64,
}
let config1 = MessageConfig {
mode: CommunicationMode::Sync,
timeout_ms: 3000,
};
let config2 = MessageConfig {
mode: CommunicationMode::Sync,
timeout_ms: 3000,
};
let config3 = MessageConfig {
mode: CommunicationMode::Async,
timeout_ms: 3000,
};
assert_eq!(config1, config2);
assert_ne!(config1, config3);
let cloned = config1.clone();
assert_eq!(config1, cloned);
assert_eq!(config1.mode, CommunicationMode::Sync);
}
#[test]
fn test_communication_mode_thread_safety() {
use std::sync::Arc;
use std::thread;
let mode = Arc::new(CommunicationMode::Async);
let mode_clone = Arc::clone(&mode);
let handle = thread::spawn(move || {
assert_eq!(*mode_clone, CommunicationMode::Async);
});
handle.join().unwrap();
assert_eq!(*mode, CommunicationMode::Async);
}
#[test]
fn test_default_is_sync() {
let default_mode: CommunicationMode = Default::default();
match default_mode {
CommunicationMode::Sync => {
}
_ => panic!("Default should be Sync variant"),
}
}
#[test]
fn test_communication_mode_size() {
use std::mem;
let size = mem::size_of::<CommunicationMode>();
assert_eq!(size, 1, "CommunicationMode should be 1 byte");
}
#[test]
fn test_discriminant_consistency() {
let sync1 = CommunicationMode::Sync;
let sync2 = CommunicationMode::Sync;
let async1 = CommunicationMode::Async;
let async2 = CommunicationMode::Async;
assert_eq!(sync1, sync2);
assert_eq!(async1, async2);
assert_ne!(sync1, async1);
}
}