lace_codebook/
error.rs

1use polars::datatypes::DataType;
2use serde::Serialize;
3use thiserror::Error;
4
5/// Error that can occur when merging the columns of two codebooks
6#[derive(Serialize, Debug, Clone, PartialEq, Eq, Error)]
7pub enum MergeColumnsError {
8    /// The two codebooks have overlapping column names
9    #[error("The codebooks both have entries for column `{0}`")]
10    DuplicateColumnName(String),
11}
12
13/// The row already exists
14#[derive(Clone, Debug, PartialEq, Eq, Error)]
15#[error("The row `{0}` alread exists")]
16pub struct InsertRowError(pub String);
17
18#[derive(Error, Debug)]
19pub enum ReadError {
20    #[error("Io error: {0}")]
21    Io(#[from] std::io::Error),
22    #[error("Polars error: {0}")]
23    Polars(#[from] polars::prelude::PolarsError),
24}
25
26#[derive(Debug, Error)]
27pub enum ColMetadataListError {
28    #[error("Duplicate column name `{0}`")]
29    Duplicate(String),
30}
31
32#[derive(Debug, Error)]
33pub enum RowNameListError {
34    #[error("Duplicate row name `{row_name}` at index {ix_1} and {ix_1}")]
35    Duplicate {
36        row_name: String,
37        ix_1: usize,
38        ix_2: usize,
39    },
40}
41
42/// Errors that can arise when creating a codebook from a CSV file
43#[derive(Debug, Error)]
44pub enum CodebookError {
45    #[error("io error: {0}")]
46    Io(#[from] std::io::Error),
47    /// A column had no values in it.
48    #[error("column '{col_name}' is blank")]
49    BlankColumn { col_name: String },
50    /// Could not infer the data type of a column
51    #[error("cannot infer feature type of column '{col_name}'")]
52    UnableToInferColumnType { col_name: String },
53    /// Dataset contains a Arrow datatype that cannot currently be converted to
54    /// a column metadata
55    #[error("Unsupported Arrow dtype `{dtype}` for column `{col_name}`")]
56    UnsupportedDataType { col_name: String, dtype: DataType },
57    /// Too many distinct values for categorical column. A category is
58    /// represented by a u8, so there can only be 256 distinct values.
59    #[error("column '{col_name}' contains more than 256 categorical classes")]
60    CategoricalOverflow { col_name: String },
61    /// The column with name appears more than once
62    #[error("Column metadata error")]
63    ColumnMetadata(#[from] ColMetadataListError),
64    /// The column with name appears more than once
65    #[error("Row names error: {0}")]
66    RowNames(#[from] RowNameListError),
67    /// Polars error
68    #[error("Polars error: {0}")]
69    Polars(#[from] polars::prelude::PolarsError),
70    /// The user did not provide an index/ID column
71    #[error("No `ID` column (row index)")]
72    NoIdColumn,
73    /// There are null values in the index/ID column
74    #[error("Null values in ID column (row index)")]
75    NullValuesInIndex,
76    /// There the column contains only a single unique value, which can cause
77    /// zero-variance issues which in turn cause other numerical issues.
78    #[error("Column `{0}` contains only a single unique value")]
79    SingleValueColumn(String),
80    /// There is more than one column named some form of `ID`. For example,
81    /// there is a column named `ID` and `id`.
82    #[error("More than one `ID` column.")]
83    MultipleIdColumns,
84    /// Problem reading data into a DataFrame
85    #[error("ReadError: {0}")]
86    Read(#[from] ReadError),
87}