1use csv::Error as CsvError;
5use std::fmt;
6use std::io;
7use toml::de::Error as TomlError;
8use toml::ser::Error as TomlSerError;
9
10#[derive(Debug)]
12pub enum GitSheetsError {
13 IoError(io::Error),
15 TomlError(TomlError),
17 TomlSerError(TomlSerError),
19 CsvError(CsvError),
21 DependencyHashMismatch(String),
23 EmptyTable,
25 NoPrimaryKey,
27 InvalidRowIndex(String),
29 FileSystemError(String),
31}
32
33impl PartialEq for GitSheetsError {
34 fn eq(&self, other: &Self) -> bool {
35 match (self, other) {
36 (GitSheetsError::TomlError(e1), GitSheetsError::TomlError(e2)) => e1 == e2,
37 (GitSheetsError::TomlSerError(e1), GitSheetsError::TomlSerError(e2)) => e1 == e2,
38 (
39 GitSheetsError::DependencyHashMismatch(s1),
40 GitSheetsError::DependencyHashMismatch(s2),
41 ) => s1 == s2,
42 (GitSheetsError::EmptyTable, GitSheetsError::EmptyTable) => true,
43 (GitSheetsError::NoPrimaryKey, GitSheetsError::NoPrimaryKey) => true,
44 (GitSheetsError::InvalidRowIndex(s1), GitSheetsError::InvalidRowIndex(s2)) => s1 == s2,
45 (GitSheetsError::FileSystemError(s1), GitSheetsError::FileSystemError(s2)) => s1 == s2,
46 _ => false,
47 }
48 }
49}
50
51impl fmt::Display for GitSheetsError {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 GitSheetsError::IoError(e) => write!(f, "IO Error: {}", e),
55 GitSheetsError::TomlError(e) => write!(f, "TOML Error: {}", e),
56 GitSheetsError::TomlSerError(e) => write!(f, "TOML Serialization Error: {}", e),
57 GitSheetsError::CsvError(e) => write!(f, "CSV Error: {}", e),
58 GitSheetsError::DependencyHashMismatch(msg) => {
59 write!(f, "Dependency Hash Mismatch: {}", msg)
60 }
61 GitSheetsError::EmptyTable => write!(f, "Empty Table"),
62 GitSheetsError::NoPrimaryKey => write!(f, "No Primary Key"),
63 GitSheetsError::InvalidRowIndex(msg) => write!(f, "Invalid Row Index: {}", msg),
64 GitSheetsError::FileSystemError(msg) => write!(f, "File System Error: {}", msg),
65 }
66 }
67}
68
69impl std::error::Error for GitSheetsError {
70 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71 match self {
72 GitSheetsError::IoError(e) => Some(e),
73 GitSheetsError::TomlError(e) => Some(e),
74 GitSheetsError::TomlSerError(e) => Some(e),
75 GitSheetsError::CsvError(e) => Some(e),
76 GitSheetsError::DependencyHashMismatch(_) => None,
77 GitSheetsError::EmptyTable => None,
78 GitSheetsError::NoPrimaryKey => None,
79 GitSheetsError::InvalidRowIndex(_) => None,
80 GitSheetsError::FileSystemError(_) => None,
81 }
82 }
83}
84
85impl From<io::Error> for GitSheetsError {
86 fn from(error: io::Error) -> Self {
87 GitSheetsError::IoError(error)
88 }
89}
90
91impl From<TomlError> for GitSheetsError {
92 fn from(error: TomlError) -> Self {
93 GitSheetsError::TomlError(error)
94 }
95}
96
97impl From<TomlSerError> for GitSheetsError {
98 fn from(error: TomlSerError) -> Self {
99 GitSheetsError::TomlSerError(error)
100 }
101}
102
103impl From<CsvError> for GitSheetsError {
104 fn from(error: CsvError) -> Self {
105 GitSheetsError::CsvError(error)
106 }
107}
108
109pub type Result<T> = std::result::Result<T, GitSheetsError>;