use std::fmt::Display;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LtrError {
FeatureIndexOutOfBounds(usize),
RankListIndexOutOfBounds(usize),
InvalidDataPoint(&'static str),
EvaluationError(&'static str),
ParseError(&'static str),
IOError(String),
NoRankers,
}
impl Display for LtrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LtrError::FeatureIndexOutOfBounds(i) => write!(f, "Feature index out of bounds: {}", i),
LtrError::RankListIndexOutOfBounds(i) => {
write!(f, "RankList index out of bounds: {}", i)
}
LtrError::InvalidDataPoint(msg) => write!(f, "Invalid datapoint: {}", msg),
LtrError::EvaluationError(msg) => write!(f, "Evaluation error: {}", msg),
LtrError::ParseError(msg) => write!(f, "Error while parsing an input: {}", msg),
LtrError::IOError(msg) => write!(f, "Error while reading or writing an input: {}", msg),
LtrError::NoRankers => write!(f, "No rankers were built. Run `fit` first."),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display_error_message() {
assert_eq!(
"Feature index out of bounds: 2",
LtrError::FeatureIndexOutOfBounds(2).to_string()
);
assert_eq!(
"RankList index out of bounds: 10",
LtrError::RankListIndexOutOfBounds(10).to_string()
);
assert_eq!(
"Invalid datapoint: Test",
LtrError::InvalidDataPoint("Test").to_string()
);
assert_eq!(
"Evaluation error: Foo",
LtrError::EvaluationError("Foo").to_string()
);
assert_eq!(
"Error while parsing an input: Unknown",
LtrError::ParseError("Unknown").to_string()
);
assert_eq!(
"Error while reading or writing an input: I/O",
LtrError::IOError("I/O".to_string()).to_string()
);
assert_eq!(
"No rankers were built. Run `fit` first.",
LtrError::NoRankers.to_string()
);
}
}