Use custom serde errors, make all sparse errs lowercase w/o punctuation

This commit is contained in:
Fabian Loeschner 2021-11-09 17:06:24 +01:00
parent e9b7718292
commit 7e67d920a7
4 changed files with 66 additions and 47 deletions

View File

@ -127,12 +127,12 @@ impl<T> CooMatrix<T> {
if row_indices.len() != col_indices.len() { if row_indices.len() != col_indices.len() {
return Err(SparseFormatError::from_kind_and_msg( return Err(SparseFormatError::from_kind_and_msg(
InvalidStructure, InvalidStructure,
"Number of row and col indices must be the same.", "number of row and col indices must be the same",
)); ));
} else if col_indices.len() != values.len() { } else if col_indices.len() != values.len() {
return Err(SparseFormatError::from_kind_and_msg( return Err(SparseFormatError::from_kind_and_msg(
InvalidStructure, InvalidStructure,
"Number of col indices and values must be the same.", "number of col indices and values must be the same",
)); ));
} }
@ -142,12 +142,12 @@ impl<T> CooMatrix<T> {
if !row_indices_in_bounds { if !row_indices_in_bounds {
Err(SparseFormatError::from_kind_and_msg( Err(SparseFormatError::from_kind_and_msg(
IndexOutOfBounds, IndexOutOfBounds,
"Row index out of bounds.", "row index out of bounds",
)) ))
} else if !col_indices_in_bounds { } else if !col_indices_in_bounds {
Err(SparseFormatError::from_kind_and_msg( Err(SparseFormatError::from_kind_and_msg(
IndexOutOfBounds, IndexOutOfBounds,
"Col index out of bounds.", "col index out of bounds",
)) ))
} else { } else {
Ok(Self { Ok(Self {
@ -327,9 +327,14 @@ where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let de = CooMatrixDeserializationData::deserialize(deserializer)?; let de = CooMatrixDeserializationData::deserialize(deserializer)?;
CooMatrix::try_from_triplets(de.nrows, de.ncols, de.row_indices, de.col_indices, de.values) CooMatrix::try_from_triplets(
.map(|m| m.into()) de.nrows,
// TODO: More specific error de.ncols,
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid COO matrix"), &"a valid COO matrix")) de.row_indices,
de.col_indices,
de.values,
)
.map(|m| m.into())
.map_err(|e| de::Error::custom(e))
} }
} }

View File

@ -183,7 +183,7 @@ impl<T> CscMatrix<T> {
} else { } else {
Err(SparseFormatError::from_kind_and_msg( Err(SparseFormatError::from_kind_and_msg(
SparseFormatErrorKind::InvalidStructure, SparseFormatErrorKind::InvalidStructure,
"Number of values and row indices must be the same", "number of values and row indices must be the same",
)) ))
} }
} }
@ -572,10 +572,15 @@ where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let de = CscMatrixDeserializationData::deserialize(deserializer)?; let de = CscMatrixDeserializationData::deserialize(deserializer)?;
CscMatrix::try_from_csc_data(de.nrows, de.ncols, de.col_offsets, de.row_indices, de.values) CscMatrix::try_from_csc_data(
.map(|m| m.into()) de.nrows,
// TODO: More specific error de.ncols,
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSC matrix"), &"a valid CSC matrix")) de.col_offsets,
de.row_indices,
de.values,
)
.map(|m| m.into())
.map_err(|e| de::Error::custom(e))
} }
} }
@ -592,28 +597,28 @@ fn pattern_format_error_to_csc_error(err: SparsityPatternFormatError) -> SparseF
match err { match err {
InvalidOffsetArrayLength => E::from_kind_and_msg( InvalidOffsetArrayLength => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"Length of col offset array is not equal to ncols + 1.", "length of col offset array is not equal to ncols + 1",
), ),
InvalidOffsetFirstLast => E::from_kind_and_msg( InvalidOffsetFirstLast => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"First or last col offset is inconsistent with format specification.", "first or last col offset is inconsistent with format specification",
), ),
NonmonotonicOffsets => E::from_kind_and_msg( NonmonotonicOffsets => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"Col offsets are not monotonically increasing.", "col offsets are not monotonically increasing",
), ),
NonmonotonicMinorIndices => E::from_kind_and_msg( NonmonotonicMinorIndices => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"Row indices are not monotonically increasing (sorted) within each column.", "row indices are not monotonically increasing (sorted) within each column",
), ),
MajorIndexOutOfBounds => { MajorIndexOutOfBounds => {
E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") E::from_kind_and_msg(K::IndexOutOfBounds, "column indices are out of bounds")
} }
MinorIndexOutOfBounds => { MinorIndexOutOfBounds => {
E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") E::from_kind_and_msg(K::IndexOutOfBounds, "row indices are out of bounds")
} }
PatternDuplicateEntry => { PatternDuplicateEntry => {
E::from_kind_and_msg(K::DuplicateEntry, "Matrix data contains duplicate entries.") E::from_kind_and_msg(K::DuplicateEntry, "matrix data contains duplicate entries")
} }
} }
} }

View File

@ -194,14 +194,14 @@ impl<T> CsrMatrix<T> {
if col_indices.len() != values.len() { if col_indices.len() != values.len() {
return Err(SparseFormatError::from_kind_and_msg( return Err(SparseFormatError::from_kind_and_msg(
SparseFormatErrorKind::InvalidStructure, SparseFormatErrorKind::InvalidStructure,
"Number of values and column indices must be the same", "number of values and column indices must be the same",
)); ));
} }
if row_offsets.len() == 0 { if row_offsets.len() == 0 {
return Err(SparseFormatError::from_kind_and_msg( return Err(SparseFormatError::from_kind_and_msg(
SparseFormatErrorKind::InvalidStructure, SparseFormatErrorKind::InvalidStructure,
"Number of offsets should be greater than 0", "number of offsets should be greater than 0",
)); ));
} }
@ -210,7 +210,7 @@ impl<T> CsrMatrix<T> {
if next_offset > count { if next_offset > count {
return Err(SparseFormatError::from_kind_and_msg( return Err(SparseFormatError::from_kind_and_msg(
SparseFormatErrorKind::InvalidStructure, SparseFormatErrorKind::InvalidStructure,
"No row offset should be greater than the number of column indices", "no row offset should be greater than the number of column indices",
)); ));
} }
if offset > next_offset { if offset > next_offset {
@ -254,7 +254,7 @@ impl<T> CsrMatrix<T> {
} else { } else {
Err(SparseFormatError::from_kind_and_msg( Err(SparseFormatError::from_kind_and_msg(
SparseFormatErrorKind::InvalidStructure, SparseFormatErrorKind::InvalidStructure,
"Number of values and column indices must be the same", "number of values and column indices must be the same",
)) ))
} }
} }
@ -643,10 +643,15 @@ where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let de = CsrMatrixDeserializationData::deserialize(deserializer)?; let de = CsrMatrixDeserializationData::deserialize(deserializer)?;
CsrMatrix::try_from_csr_data(de.nrows, de.ncols, de.row_offsets, de.col_indices, de.values) CsrMatrix::try_from_csr_data(
.map(|m| m.into()) de.nrows,
// TODO: More specific error de.ncols,
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSR matrix"), &"a valid CSR matrix")) de.row_offsets,
de.col_indices,
de.values,
)
.map(|m| m.into())
.map_err(|e| de::Error::custom(e))
} }
} }
@ -663,28 +668,28 @@ fn pattern_format_error_to_csr_error(err: SparsityPatternFormatError) -> SparseF
match err { match err {
InvalidOffsetArrayLength => E::from_kind_and_msg( InvalidOffsetArrayLength => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"Length of row offset array is not equal to nrows + 1.", "length of row offset array is not equal to nrows + 1",
), ),
InvalidOffsetFirstLast => E::from_kind_and_msg( InvalidOffsetFirstLast => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"First or last row offset is inconsistent with format specification.", "first or last row offset is inconsistent with format specification",
), ),
NonmonotonicOffsets => E::from_kind_and_msg( NonmonotonicOffsets => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"Row offsets are not monotonically increasing.", "row offsets are not monotonically increasing",
), ),
NonmonotonicMinorIndices => E::from_kind_and_msg( NonmonotonicMinorIndices => E::from_kind_and_msg(
K::InvalidStructure, K::InvalidStructure,
"Column indices are not monotonically increasing (sorted) within each row.", "column indices are not monotonically increasing (sorted) within each row",
), ),
MajorIndexOutOfBounds => { MajorIndexOutOfBounds => {
E::from_kind_and_msg(K::IndexOutOfBounds, "Row indices are out of bounds.") E::from_kind_and_msg(K::IndexOutOfBounds, "row indices are out of bounds")
} }
MinorIndexOutOfBounds => { MinorIndexOutOfBounds => {
E::from_kind_and_msg(K::IndexOutOfBounds, "Column indices are out of bounds.") E::from_kind_and_msg(K::IndexOutOfBounds, "column indices are out of bounds")
} }
PatternDuplicateEntry => { PatternDuplicateEntry => {
E::from_kind_and_msg(K::DuplicateEntry, "Matrix data contains duplicate entries.") E::from_kind_and_msg(K::DuplicateEntry, "matrix data contains duplicate entries")
} }
} }
} }

View File

@ -259,7 +259,7 @@ impl SparsityPattern {
new_offsets, new_offsets,
new_indices, new_indices,
) )
.expect("Internal error: Transpose should never fail.") .expect("internal error: Transpose should never fail")
} }
} }
@ -333,10 +333,14 @@ impl<'de> Deserialize<'de> for SparsityPattern {
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let de = SparsityPatternDeserializationData::deserialize(deserializer)?; let de = SparsityPatternDeserializationData::deserialize(deserializer)?;
SparsityPattern::try_from_offsets_and_indices(de.major_dim, de.minor_dim, de.major_offsets, de.minor_indices) SparsityPattern::try_from_offsets_and_indices(
.map(|m| m.into()) de.major_dim,
// TODO: More specific error de.minor_dim,
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid sparsity pattern"), &"a valid sparsity pattern")) de.major_offsets,
de.minor_indices,
)
.map(|m| m.into())
.map_err(|e| de::Error::custom(e))
} }
} }
@ -369,27 +373,27 @@ impl fmt::Display for SparsityPatternFormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
SparsityPatternFormatError::InvalidOffsetArrayLength => { SparsityPatternFormatError::InvalidOffsetArrayLength => {
write!(f, "Length of offset array is not equal to (major_dim + 1).") write!(f, "length of offset array is not equal to (major_dim + 1)")
} }
SparsityPatternFormatError::InvalidOffsetFirstLast => { SparsityPatternFormatError::InvalidOffsetFirstLast => {
write!(f, "First or last offset is incompatible with format.") write!(f, "first or last offset is incompatible with format")
} }
SparsityPatternFormatError::NonmonotonicOffsets => { SparsityPatternFormatError::NonmonotonicOffsets => {
write!(f, "Offsets are not monotonically increasing.") write!(f, "offsets are not monotonically increasing")
} }
SparsityPatternFormatError::MajorIndexOutOfBounds => { SparsityPatternFormatError::MajorIndexOutOfBounds => {
write!(f, "A major index is out of bounds.") write!(f, "a major index is out of bounds")
} }
SparsityPatternFormatError::MinorIndexOutOfBounds => { SparsityPatternFormatError::MinorIndexOutOfBounds => {
write!(f, "A minor index is out of bounds.") write!(f, "a minor index is out of bounds")
} }
SparsityPatternFormatError::DuplicateEntry => { SparsityPatternFormatError::DuplicateEntry => {
write!(f, "Input data contains duplicate entries.") write!(f, "input data contains duplicate entries")
} }
SparsityPatternFormatError::NonmonotonicMinorIndices => { SparsityPatternFormatError::NonmonotonicMinorIndices => {
write!( write!(
f, f,
"Minor indices are not monotonically increasing within each lane." "minor indices are not monotonically increasing within each lane"
) )
} }
} }