use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Input {
LocalTrust,
PreTrust,
}
impl fmt::Display for Input {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Input::LocalTrust => "local trust",
Input::PreTrust => "pre-trust",
})
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum EigenTrustError {
InvalidAlpha(f64),
InvalidEpsilon(f64),
InvalidMaxIterations,
NonFiniteWeight {
input: Input,
position: usize,
},
NegativeWeight {
input: Input,
position: usize,
weight: f64,
},
InvalidPeer {
input: Input,
position: usize,
},
WeightOverflow(Input),
EmptyNetwork,
NotConverged {
iterations: usize,
residual: f64,
},
}
impl fmt::Display for EigenTrustError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EigenTrustError::InvalidAlpha(a) => write!(f, "alpha must be in [0, 1], got {}", a),
EigenTrustError::InvalidEpsilon(e) => {
write!(f, "epsilon must be finite and greater than 0, got {}", e)
}
EigenTrustError::InvalidMaxIterations => {
f.write_str("max_iterations must be at least 1")
}
EigenTrustError::NonFiniteWeight { input, position } => {
write!(f, "{} item {}: weight must be finite", input, position)
}
EigenTrustError::NegativeWeight {
input,
position,
weight,
} => write!(
f,
"{} item {}: weight {} is negative; distrust is not supported",
input, position, weight
),
EigenTrustError::InvalidPeer { input, position } => {
write!(
f,
"{} item {}: peer index usize::MAX is not allowed",
input, position
)
}
EigenTrustError::WeightOverflow(input) => {
write!(
f,
"{} weights are too large: their sum overflows f64",
input
)
}
EigenTrustError::EmptyNetwork => f.write_str("the network has no peers"),
EigenTrustError::NotConverged {
iterations,
residual,
} => write!(
f,
"did not converge in {} iterations (residual {:.3e}); \
a small alpha on a periodic trust graph can oscillate, try a larger alpha",
iterations, residual
),
}
}
}
impl std::error::Error for EigenTrustError {}