From 7e67d920a7bddbbd2e6cf8ccb423cfa8f32f85cd Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Tue, 9 Nov 2021 17:06:24 +0100 Subject: [PATCH] Use custom serde errors, make all sparse errs lowercase w/o punctuation --- nalgebra-sparse/src/coo.rs | 21 ++++++++++++-------- nalgebra-sparse/src/csc.rs | 29 ++++++++++++++++------------ nalgebra-sparse/src/csr.rs | 35 +++++++++++++++++++--------------- nalgebra-sparse/src/pattern.rs | 28 +++++++++++++++------------ 4 files changed, 66 insertions(+), 47 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index b84b2cc5..15b23566 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -127,12 +127,12 @@ impl CooMatrix { 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 CooMatrix { 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) - .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")) + CooMatrix::try_from_triplets( + de.nrows, + de.ncols, + de.row_indices, + de.col_indices, + de.values, + ) + .map(|m| m.into()) + .map_err(|e| de::Error::custom(e)) } } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index e6eb1589..54383b01 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -183,7 +183,7 @@ impl CscMatrix { } 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) - .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")) + CscMatrix::try_from_csc_data( + de.nrows, + de.ncols, + 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 { 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") } } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 7bd996da..804606ca 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -194,14 +194,14 @@ impl CsrMatrix { 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 CsrMatrix { 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 CsrMatrix { } 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) - .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")) + CsrMatrix::try_from_csr_data( + de.nrows, + de.ncols, + 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 { 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") } } } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index 1f9dde84..1d92dae3 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -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) - .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")) + SparsityPattern::try_from_offsets_and_indices( + de.major_dim, + de.minor_dim, + 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 { 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" ) } }