use std::error::Error;
use std::fmt;
use std::io;
use std::str;
pub type PointCloudResult<T> = Result<T, PointCloudError>;
#[derive(Debug)]
pub enum PointCloudError {
DataAccessError {
index: usize,
reason: String,
},
MetricError,
NotSorted,
UnknownName,
IoError(io::Error),
ParsingError(ParsingError),
NodeNestingError {
message: &'static str,
},
}
impl fmt::Display for PointCloudError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PointCloudError::IoError(ref e) => write!(f, "{}", e),
PointCloudError::ParsingError(ref e) => write!(f, "{}", e),
PointCloudError::DataAccessError { .. } => {
write!(f, "there was an issue grabbing a data point or label")
}
PointCloudError::UnknownName => {
write!(f, "there was an issue grabbing a name from the known names")
}
PointCloudError::NodeNestingError { .. } => {
write!(f, "There is a temporary node in a working tree")
}
PointCloudError::MetricError => write!(
f,
"The metric failed, you probably mixed sparse and dense data"
),
PointCloudError::NotSorted => write!(f, "Passed data that wasn't sorted"),
}
}
}
#[allow(deprecated)]
impl Error for PointCloudError {
fn description(&self) -> &str {
match *self {
PointCloudError::IoError(ref e) => e.description(),
PointCloudError::ParsingError(ref e) => e.description(),
PointCloudError::DataAccessError { .. } => {
"there was an issue grabbing a data point or label"
}
PointCloudError::UnknownName => {
"there was an issue grabbing a name from the known names"
}
PointCloudError::NodeNestingError { .. } => {
"There is a temporary node in a working tree"
}
PointCloudError::MetricError => {
"The metric failed, you probably mixed sparse and dense data"
}
PointCloudError::NotSorted => "Passed data that wasn't sorted",
}
}
fn cause(&self) -> Option<&dyn Error> {
match *self {
PointCloudError::IoError(ref e) => Some(e),
PointCloudError::ParsingError(ref e) => Some(e),
PointCloudError::DataAccessError { .. } => None,
PointCloudError::UnknownName => None,
PointCloudError::NodeNestingError { .. } => None,
PointCloudError::MetricError { .. } => None,
PointCloudError::NotSorted { .. } => None,
}
}
}
impl From<io::Error> for PointCloudError {
fn from(err: io::Error) -> Self {
PointCloudError::IoError(err)
}
}
impl From<ParsingError> for PointCloudError {
fn from(err: ParsingError) -> Self {
PointCloudError::ParsingError(err)
}
}
impl From<PointCloudError> for io::Error {
fn from(err: PointCloudError) -> Self {
match err {
PointCloudError::IoError(e) => e,
e => io::Error::new(io::ErrorKind::Other, Box::new(e)),
}
}
}
impl PointCloudError {
pub fn data_access(index: usize, reason: String) -> PointCloudError {
PointCloudError::DataAccessError {
index: index as usize,
reason,
}
}
}
#[derive(Debug)]
pub enum ParsingError {
MalformedYamlError {
file_name: String,
field: String,
},
MissingYamlError {
file_name: String,
field: String,
},
CSVReadError {
file_name: String,
line_number: usize,
key: String,
},
RegularParsingError(&'static str),
}
impl fmt::Display for ParsingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl Error for ParsingError {
fn description(&self) -> &str {
match *self {
ParsingError::MalformedYamlError { .. } => "there is a error reading a yaml entry",
ParsingError::MissingYamlError { .. } => "not all message fields set",
ParsingError::CSVReadError { .. } => "issue reading a CSV entry",
ParsingError::RegularParsingError(..) => "Error parsing a string",
}
}
fn cause(&self) -> Option<&dyn Error> {
match *self {
ParsingError::MalformedYamlError { .. } => None,
ParsingError::MissingYamlError { .. } => None,
ParsingError::CSVReadError { .. } => None,
ParsingError::RegularParsingError(..) => None,
}
}
}