pcd_rs/
error.rs

1//! The module defines most error type used by this crate.
2
3use crate::metas::{FieldDef, ValueKind};
4use std::{
5    io,
6    num::{ParseFloatError, ParseIntError},
7};
8
9pub type Result<T, E = Error> = std::result::Result<T, E>;
10
11/// The error returned from the crate.
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14    #[error("parsing error at line {line}: {desc}")]
15    ParseError { line: usize, desc: String },
16
17    #[error("reader schema mismatch error, expect {expect:?}, but found {found:?}")]
18    ReaderSchemaMismatchError {
19        expect: Vec<(Option<String>, ValueKind, Option<usize>)>,
20        found: Vec<FieldDef>,
21    },
22
23    #[error("writer schema mismatch error, expect {expect:?}, but found {found:?}")]
24    WriterSchemaMismatchError {
25        expect: Vec<FieldDef>,
26        found: Vec<FieldDef>,
27    },
28
29    #[error(
30        r#"field size mismatch, expect {expect} elements in "{field_name}" field, but found {found} elements in record"#,
31    )]
32    FieldSizeMismatchError {
33        field_name: String,
34        expect: usize,
35        found: usize,
36    },
37
38    #[error("record has {expect} fields, but the line has {found} tokens")]
39    TextTokenMismatchError { expect: usize, found: usize },
40
41    #[error("Invalid argument: {desc}")]
42    InvalidArgumentError { desc: String },
43
44    #[error("Invalid writer configuration: {desc}")]
45    InvalidWriterConfiguration { desc: String },
46
47    #[error("I/O error: {0}")]
48    IoError(#[from] io::Error),
49
50    #[error("{0}")]
51    ParseIntError(#[from] ParseIntError),
52
53    #[error("{0}")]
54    ParseFloatError(#[from] ParseFloatError),
55}
56
57impl Error {
58    pub fn new_parse_error(line: usize, desc: &str) -> Error {
59        Error::ParseError {
60            line,
61            desc: desc.to_owned(),
62        }
63    }
64
65    pub fn new_reader_schema_mismatch_error(
66        expect: Vec<(Option<String>, ValueKind, Option<usize>)>,
67        found: Vec<FieldDef>,
68    ) -> Error {
69        Error::ReaderSchemaMismatchError { expect, found }
70    }
71
72    pub fn new_writer_schema_mismatch_error(expect: Vec<FieldDef>, found: Vec<FieldDef>) -> Error {
73        Error::WriterSchemaMismatchError { expect, found }
74    }
75
76    pub fn new_field_size_mismatch_error(field_name: &str, expect: usize, found: usize) -> Error {
77        Error::FieldSizeMismatchError {
78            field_name: field_name.to_owned(),
79            expect,
80            found,
81        }
82    }
83
84    pub fn new_text_token_mismatch_error(expect: usize, found: usize) -> Error {
85        Error::TextTokenMismatchError { expect, found }
86    }
87
88    pub fn new_invalid_argument_error(desc: &str) -> Error {
89        Error::InvalidArgumentError {
90            desc: desc.to_owned(),
91        }
92    }
93
94    pub fn new_invalid_writer_configuration_error(desc: &str) -> Error {
95        Error::InvalidWriterConfiguration {
96            desc: desc.to_owned(),
97        }
98    }
99}