diff --git a/CHANGELOG.md b/CHANGELOG.md index 020e2b07..3005e63c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `DataParseError::CodebookAndDataRowsMismatch` variant for when the number of rows in the codebook and the number of rows in the data do not match. + +### Fixed +- Initializing an engine with a codebook that has a different number of rows than the data will result in an error instead of printing a bunch on nonsense. + ## [python-0.6.0] - 2024-01-23 ### Added diff --git a/lace/src/data/init.rs b/lace/src/data/init.rs index 14693ffb..515ca427 100644 --- a/lace/src/data/init.rs +++ b/lace/src/data/init.rs @@ -287,17 +287,14 @@ pub fn df_to_col_models( }) .collect::>()?; - if col_models - .iter() - .any(|cm| cm.len() != codebook.row_names.len()) - { - dbg!( - col_models.iter().map(|cm| cm.len()).collect::>(), - codebook.row_names.len() - ); - // FIXME! - // return Err(CsvParseError::CodebookAndDataRowMismatch); + let n_codebook_rows = codebook.row_names.len(); + if let Some(cm) = col_models.iter().find(|cm| cm.len() != n_codebook_rows) { + return Err(DataParseError::CodebookAndDataRowsMismatch { + n_codebook_rows, + n_data_rows: cm.len(), + }); } + Ok((codebook, col_models)) } diff --git a/lace/src/interface/engine/error.rs b/lace/src/interface/engine/error.rs index ba113e3d..6f291641 100644 --- a/lace/src/interface/engine/error.rs +++ b/lace/src/interface/engine/error.rs @@ -18,9 +18,9 @@ pub enum DataParseError { /// The supplied data source is not currently supported for this operation #[error("Provided an unsupported data source")] UnsupportedDataSource, - /// The user supplied column_metdata in the codebook but provided an empty + /// The user supplied column_metadata in the codebook but provided an empty /// data source - #[error("non-empty column_metdata the codebook but empty DataSource")] + #[error("non-empty column_metadata the codebook but empty DataSource")] ColumnMetadataSuppliedForEmptyData, /// The user supplied row_names in the codebook but provided an empty /// data source @@ -36,6 +36,12 @@ pub enum DataParseError { /// externally #[error("Column `{col_name}` has type `{col_type}`, which is unsupported for external data sources")] UnsupportedColumnType { col_name: String, col_type: String }, + /// The codebook and the data have a different number of rows + #[error("The codebook contains {n_codebook_rows} rows, but the data contain {n_data_rows} rows")] + CodebookAndDataRowsMismatch { + n_codebook_rows: usize, + n_data_rows: usize, + }, } /// Errors that can arise when creating a new engine