use crate::enums::SampleFormatCode;
#[derive(Debug)]
pub enum RsgError {
TryFromSlice(std::array::TryFromSliceError),
TryFromUtf8(std::string::FromUtf8Error),
StdIoError(std::io::Error),
BinHeaderLength { l: usize },
SEGYSettingsError { msg: String },
FileTooShort,
FloatConversion {
float: f32,
format: SampleFormatCode,
},
IncompleteTrace,
TraceNotFound { i: usize },
TracePointOutOfBounds { idx: usize },
SEGYTooShort,
ShortSEGY { a: usize, b: usize },
TraceDivisibility {
a: usize,
b: usize,
format: SampleFormatCode,
},
TraceHeaderLength { l: usize },
BitConversionError { msg: String },
InvalidHeader { msg: String },
LongDataVector { l_data: usize },
BadDataVector {
l_data: u16,
l_bin: u16,
l_trace: u16,
},
ParseEnum { f: String, code: u16 },
MapFile(std::io::Error),
#[cfg(feature = "to_json")]
SerdeError(serde_json::Error),
}
impl From<std::array::TryFromSliceError> for RsgError {
fn from(e: std::array::TryFromSliceError) -> Self {
Self::TryFromSlice(e)
}
}
impl From<std::io::Error> for RsgError {
fn from(e: std::io::Error) -> Self {
Self::StdIoError(e)
}
}
impl std::fmt::Display for RsgError {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use self::RsgError::*;
match self {
#[cfg(feature = "to_json")]
SerdeError(x) => write!(fmt, "{}", x),
StdIoError(x) => write!(fmt, "{}", x),
TryFromSlice(x) => write!(fmt, "{}", x),
TryFromUtf8(x) => write!(fmt, "{}", x),
BinHeaderLength { l } => write!(fmt, "Binary header length should be 400 but is {}", l),
SEGYSettingsError { msg } => write!(fmt, "Error in settings: {}", msg),
FileTooShort => write!(fmt, "File is too short to be SEG-Y"),
FloatConversion { float, format } => write!(fmt, "Could not convert {} to {}.", float, format),
IncompleteTrace => write!(fmt, "Last trace incomplete: File may be corrupt."),
TraceNotFound { i } => write!(fmt, "Trace no. {} not found.", i),
TracePointOutOfBounds { idx } => write!(fmt, "Error getting trace: Idx ({}) trace point is out of bounds.", idx),
SEGYTooShort => write!(fmt, "Mapped file is too short to be a SEG-Y file, or too many Extended Text Headers are counted"),
ShortSEGY { a, b } => write!(fmt, "Error getting trace: SEG-Y Mapping is too short (is {}-bytes, needs to be {}-bytes)", a, b),
TraceDivisibility { a, b, format } => write!(fmt, "Error getting trace: data binary length ({}) not divisible by datum length ({}-bit ({}))", a, b, format),
TraceHeaderLength { l } => write!(fmt, "Trace header length should be 240 but is {}", l),
BitConversionError { msg } => write!(fmt, "Bit conversion failed: {}", msg),
InvalidHeader { msg } => write!(fmt, "Invalid header: {}", msg),
LongDataVector { l_data } => write!(fmt, "Data vector has {} points, but max length is 65535.", l_data),
BadDataVector { l_data, l_bin, l_trace } => write!(fmt, "Data length is {}, but was declared as {} (binary header) or {} (trace header).", l_data, l_bin, l_trace),
ParseEnum { f, code } => write!(fmt, "Could not parse source ({}) to {}.", code, f),
MapFile(e) => write!(fmt, "Could not create file map: {}", e),
}
}
}
impl From<RsgError> for String {
fn from(e: RsgError) -> String {
e.to_string()
}
}
impl std::error::Error for RsgError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::RsgError::*;
match self {
#[cfg(feature = "to_json")]
SerdeError(x) => x.source(),
StdIoError(x) | MapFile(x) => x.source(),
TryFromSlice(x) => x.source(),
TryFromUtf8(x) => x.source(),
_ => None,
}
}
}