graphrecords_core/errors/
conversion.rs1use crate::graphrecord::{GraphRecordAttribute, GraphRecordValue};
2use std::{
3 error::Error,
4 fmt::{Display, Formatter, Result as FmtResult},
5 io::ErrorKind,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum ConversionError {
10 ValueToAttribute { value: GraphRecordValue },
11 UnsupportedPolarsValue { value: String },
12 UnsupportedPolarsAttribute { value: String },
13 TimestampOutOfRange { timestamp: i64 },
14 ColumnNotFound { column_name: String },
15 ReservedAttributeName { attribute: GraphRecordAttribute },
16 NodeDataFrameCreation { group: String },
17 EdgeDataFrameCreation { group: String },
18 FileRead { path: String, kind: ErrorKind },
19 FileWrite { path: String, kind: ErrorKind },
20 DirectoryCreation { path: String, kind: ErrorKind },
21 RonSerialization,
22 RonDeserialization { path: String },
23 BinarySerialization,
24 BinaryDeserialization,
25}
26
27impl Error for ConversionError {}
28
29impl Display for ConversionError {
30 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
31 match self {
32 Self::ValueToAttribute { value } => {
33 write!(f, "Cannot convert `{value}` into `GraphRecordAttribute`")
34 }
35 Self::UnsupportedPolarsValue { value } => {
36 write!(f, "Cannot convert `{value}` into `GraphRecordValue`")
37 }
38 Self::UnsupportedPolarsAttribute { value } => {
39 write!(f, "Cannot convert `{value}` into `GraphRecordAttribute`")
40 }
41 Self::TimestampOutOfRange { timestamp } => {
42 write!(f, "Cannot convert timestamp `{timestamp}` into a datetime")
43 }
44 Self::ColumnNotFound { column_name } => {
45 write!(
46 f,
47 "Cannot find column with name `{column_name}` in dataframe"
48 )
49 }
50 Self::ReservedAttributeName { attribute } => {
51 write!(f, "Attribute name `{attribute}` is reserved")
52 }
53 Self::NodeDataFrameCreation { group } => {
54 write!(f, "Failed to create node DataFrame for group `{group}`")
55 }
56 Self::EdgeDataFrameCreation { group } => {
57 write!(f, "Failed to create edge DataFrame for group `{group}`")
58 }
59 Self::FileRead { path, kind } => {
60 write!(f, "Failed to read file `{path}`: {kind}")
61 }
62 Self::FileWrite { path, kind } => {
63 write!(f, "Failed to write file `{path}`: {kind}")
64 }
65 Self::DirectoryCreation { path, kind } => {
66 write!(f, "Failed to create directory `{path}`: {kind}")
67 }
68 Self::RonSerialization => write!(f, "Failed to convert GraphRecord to ron"),
69 Self::RonDeserialization { path } => {
70 write!(f, "Failed to create GraphRecord from file `{path}`")
71 }
72 Self::BinarySerialization => write!(f, "Could not serialize GraphRecord"),
73 Self::BinaryDeserialization => write!(f, "Could not deserialize GraphRecord"),
74 }
75 }
76}
77
78#[cfg(test)]
79mod test {
80 use super::ConversionError;
81 use crate::graphrecord::GraphRecordValue;
82 use std::io::ErrorKind;
83
84 #[test]
85 fn test_display_values() {
86 assert_eq!(
87 "Cannot convert `true` into `GraphRecordAttribute`",
88 ConversionError::ValueToAttribute {
89 value: GraphRecordValue::Bool(true)
90 }
91 .to_string()
92 );
93 assert_eq!(
94 "Cannot convert `true` into `GraphRecordValue`",
95 ConversionError::UnsupportedPolarsValue {
96 value: "true".to_string()
97 }
98 .to_string()
99 );
100 assert_eq!(
101 "Cannot convert `true` into `GraphRecordAttribute`",
102 ConversionError::UnsupportedPolarsAttribute {
103 value: "true".to_string()
104 }
105 .to_string()
106 );
107 assert_eq!(
108 "Cannot convert timestamp `1` into a datetime",
109 ConversionError::TimestampOutOfRange { timestamp: 1 }.to_string()
110 );
111 }
112
113 #[test]
114 fn test_display_dataframes() {
115 assert_eq!(
116 "Cannot find column with name `index` in dataframe",
117 ConversionError::ColumnNotFound {
118 column_name: "index".to_string()
119 }
120 .to_string()
121 );
122 assert_eq!(
123 "Attribute name `\"node_index\"` is reserved",
124 ConversionError::ReservedAttributeName {
125 attribute: "node_index".into()
126 }
127 .to_string()
128 );
129 assert_eq!(
130 "Failed to create node DataFrame for group `group`",
131 ConversionError::NodeDataFrameCreation {
132 group: "group".to_string()
133 }
134 .to_string()
135 );
136 assert_eq!(
137 "Failed to create edge DataFrame for group `group`",
138 ConversionError::EdgeDataFrameCreation {
139 group: "group".to_string()
140 }
141 .to_string()
142 );
143 }
144
145 #[test]
146 fn test_display_files() {
147 assert_eq!(
148 "Failed to read file `path`: entity not found",
149 ConversionError::FileRead {
150 path: "path".to_string(),
151 kind: ErrorKind::NotFound
152 }
153 .to_string()
154 );
155 assert_eq!(
156 "Failed to write file `path`: permission denied",
157 ConversionError::FileWrite {
158 path: "path".to_string(),
159 kind: ErrorKind::PermissionDenied
160 }
161 .to_string()
162 );
163 assert_eq!(
164 "Failed to create directory `path`: permission denied",
165 ConversionError::DirectoryCreation {
166 path: "path".to_string(),
167 kind: ErrorKind::PermissionDenied
168 }
169 .to_string()
170 );
171 assert_eq!(
172 "Failed to convert GraphRecord to ron",
173 ConversionError::RonSerialization.to_string()
174 );
175 assert_eq!(
176 "Failed to create GraphRecord from file `path`",
177 ConversionError::RonDeserialization {
178 path: "path".to_string()
179 }
180 .to_string()
181 );
182 assert_eq!(
183 "Could not serialize GraphRecord",
184 ConversionError::BinarySerialization.to_string()
185 );
186 assert_eq!(
187 "Could not deserialize GraphRecord",
188 ConversionError::BinaryDeserialization.to_string()
189 );
190 }
191}