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() {
return Err(SparseFormatError::from_kind_and_msg(
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() {
return Err(SparseFormatError::from_kind_and_msg(
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 {
Err(SparseFormatError::from_kind_and_msg(
IndexOutOfBounds,
"Row index out of bounds.",
"row index out of bounds",
))
} else if !col_indices_in_bounds {
Err(SparseFormatError::from_kind_and_msg(
IndexOutOfBounds,
"Col index out of bounds.",
"col index out of bounds",
))
} else {
Ok(Self {
@ -327,9 +327,14 @@ where
D: Deserializer<'de>,
{
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(
de.nrows,
de.ncols,
de.row_indices,
de.col_indices,
de.values,
)
.map(|m| m.into())
// TODO: More specific error
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid COO matrix"), &"a valid COO matrix"))
.map_err(|e| de::Error::custom(e))
}
}

View File

@ -183,7 +183,7 @@ impl<T> CscMatrix<T> {
} else {
Err(SparseFormatError::from_kind_and_msg(
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>,
{
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(
de.nrows,
de.ncols,
de.col_offsets,
de.row_indices,
de.values,
)
.map(|m| m.into())
// TODO: More specific error
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSC matrix"), &"a valid CSC matrix"))
.map_err(|e| de::Error::custom(e))
}
}
@ -592,28 +597,28 @@ fn pattern_format_error_to_csc_error(err: SparsityPatternFormatError) -> SparseF
match err {
InvalidOffsetArrayLength => E::from_kind_and_msg(
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(
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(
K::InvalidStructure,
"Col offsets are not monotonically increasing.",
"col offsets are not monotonically increasing",
),
NonmonotonicMinorIndices => E::from_kind_and_msg(
K::InvalidStructure,
"Row indices are not monotonically increasing (sorted) within each column.",
"row indices are not monotonically increasing (sorted) within each column",
),
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 => {
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 => {
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() {
return Err(SparseFormatError::from_kind_and_msg(
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 {
return Err(SparseFormatError::from_kind_and_msg(
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 {
return Err(SparseFormatError::from_kind_and_msg(
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 {
@ -254,7 +254,7 @@ impl<T> CsrMatrix<T> {
} else {
Err(SparseFormatError::from_kind_and_msg(
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>,
{
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(
de.nrows,
de.ncols,
de.row_offsets,
de.col_indices,
de.values,
)
.map(|m| m.into())
// TODO: More specific error
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSR matrix"), &"a valid CSR matrix"))
.map_err(|e| de::Error::custom(e))
}
}
@ -663,28 +668,28 @@ fn pattern_format_error_to_csr_error(err: SparsityPatternFormatError) -> SparseF
match err {
InvalidOffsetArrayLength => E::from_kind_and_msg(
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(
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(
K::InvalidStructure,
"Row offsets are not monotonically increasing.",
"row offsets are not monotonically increasing",
),
NonmonotonicMinorIndices => E::from_kind_and_msg(
K::InvalidStructure,
"Column indices are not monotonically increasing (sorted) within each row.",
"column indices are not monotonically increasing (sorted) within each row",
),
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 => {
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 => {
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_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>,
{
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(
de.major_dim,
de.minor_dim,
de.major_offsets,
de.minor_indices,
)
.map(|m| m.into())
// TODO: More specific error
.map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid sparsity pattern"), &"a valid sparsity pattern"))
.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 {
match self {
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 => {
write!(f, "First or last offset is incompatible with format.")
write!(f, "first or last offset is incompatible with format")
}
SparsityPatternFormatError::NonmonotonicOffsets => {
write!(f, "Offsets are not monotonically increasing.")
write!(f, "offsets are not monotonically increasing")
}
SparsityPatternFormatError::MajorIndexOutOfBounds => {
write!(f, "A major index is out of bounds.")
write!(f, "a major index is out of bounds")
}
SparsityPatternFormatError::MinorIndexOutOfBounds => {
write!(f, "A minor index is out of bounds.")
write!(f, "a minor index is out of bounds")
}
SparsityPatternFormatError::DuplicateEntry => {
write!(f, "Input data contains duplicate entries.")
write!(f, "input data contains duplicate entries")
}
SparsityPatternFormatError::NonmonotonicMinorIndices => {
write!(
f,
"Minor indices are not monotonically increasing within each lane."
"minor indices are not monotonically increasing within each lane"
)
}
}