gtfs_structures/
error.rs

1//! Module for the error management
2use thiserror::Error;
3
4/// Specific line from a CSV file that could not be read
5#[derive(Debug)]
6pub struct LineError {
7    /// Headers of the CSV file
8    pub headers: Vec<String>,
9    /// Values of the line that could not be parsed
10    pub values: Vec<String>,
11}
12
13/// An error that can occur when processing GTFS data.
14#[derive(Error, Debug)]
15pub enum Error {
16    /// A mandatory file is not present in the archive
17    #[error("Cound not find file {0}")]
18    MissingFile(String),
19    /// A file references an Id that is not present
20    #[error("The id {0} is not known")]
21    ReferenceError(String),
22    /// The given path to the GTFS is neither a file nor a directory
23    #[error("Could not read GTFS: {0} is neither a file nor a directory")]
24    NotFileNorDirectory(String),
25    /// The time is not given in the HH:MM:SS format
26    #[error("'{0}' is not a valid time; HH:MM:SS format is expected.")]
27    InvalidTime(String),
28    /// The color is not given in the RRGGBB format, without a leading `#`
29    #[error("'{0}' is not a valid color; RRGGBB format is expected, without a leading `#`")]
30    InvalidColor(String),
31    /// Generic Input/Output error while reading a file
32    #[error("impossible to read file")]
33    IO(#[from] std::io::Error),
34    /// Impossible to read a file
35    #[error("impossible to read '{file_name}'")]
36    NamedFileIO {
37        /// The file name that could not be read
38        file_name: String,
39        /// The inital error that caused the unability to read the file
40        #[source]
41        source: Box<dyn std::error::Error + Send + Sync>,
42    },
43    /// Impossible to fetch the remote archive by the URL
44    #[cfg(feature = "read-url")]
45    #[error("impossible to remotely access file")]
46    Fetch(#[from] reqwest::Error),
47    /// Impossible to read a CSV file
48    #[error("impossible to read csv file '{file_name}'")]
49    CSVError {
50        /// File name that could not be parsed as CSV
51        file_name: String,
52        /// The initial error by the csv library
53        #[source]
54        source: csv::Error,
55        /// The line that could not be parsed by the csv library
56        line_in_error: Option<LineError>,
57    },
58    /// Error when trying to unzip the GTFS archive
59    #[error(transparent)]
60    Zip(#[from] zip::result::ZipError),
61}