1use super::NetworkError;
2use std::error::Error;
3use std::fmt::{Display, Formatter};
4
5#[derive(Debug)]
6pub enum RankError {
7 RankOutOfRange { rank: usize, world_size: usize },
8 InvalidLength(&'static str),
9 Overflow(&'static str),
10 Network(NetworkError),
11}
12
13impl Display for RankError {
14 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
15 match self {
16 Self::RankOutOfRange { rank, world_size } => {
17 write!(formatter, "rank {rank} is outside collective world size {world_size}")
18 }
19 Self::InvalidLength(message) => {
20 write!(formatter, "invalid collective length: {message}")
21 }
22 Self::Overflow(what) => {
23 write!(formatter, "collective size overflow while computing {what}")
24 }
25 Self::Network(error) => Display::fmt(error, formatter),
26 }
27 }
28}
29
30impl Error for RankError {
31 fn source(&self) -> Option<&(dyn Error + 'static)> {
32 match self {
33 Self::Network(error) => Some(error),
34 _ => None,
35 }
36 }
37}
38
39impl From<NetworkError> for RankError {
40 fn from(error: NetworkError) -> Self {
41 Self::Network(error)
42 }
43}