helix_db/helix_engine/
types.rs

1use crate::helixc::parser::parser_methods::ParserError;
2use core::fmt;
3use heed3::Error as HeedError;
4use sonic_rs::Error as SonicError;
5use std::{net::AddrParseError, str::Utf8Error, string::FromUtf8Error};
6
7#[derive(Debug)]
8pub enum GraphError {
9    Io(std::io::Error),
10    GraphConnectionError(String, std::io::Error),
11    StorageConnectionError(String, std::io::Error),
12    StorageError(String),
13    TraversalError(String),
14    ConversionError(String),
15    DecodeError(String),
16    EdgeNotFound,
17    NodeNotFound,
18    LabelNotFound,
19    VectorError(String),
20    Default,
21    New(String),
22    Empty,
23    MultipleNodesWithSameId,
24    MultipleEdgesWithSameId,
25    InvalidNode,
26    ConfigFileNotFound,
27    SliceLengthError,
28    ShortestPathNotFound,
29    EmbeddingError(String),
30}
31
32impl fmt::Display for GraphError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            GraphError::Io(e) => write!(f, "IO error: {}", e),
36            GraphError::StorageConnectionError(msg, e) => {
37                write!(f, "Error: {}", format!("{} {}", msg, e))
38            }
39            GraphError::GraphConnectionError(msg, e) => {
40                write!(f, "Error: {}", format!("{} {}", msg, e))
41            }
42            GraphError::TraversalError(msg) => write!(f, "Traversal error: {}", msg),
43            GraphError::StorageError(msg) => write!(f, "Storage error: {}", msg),
44            GraphError::ConversionError(msg) => write!(f, "Conversion error: {}", msg),
45            GraphError::DecodeError(msg) => write!(f, "Decode error: {}", msg),
46            GraphError::EdgeNotFound => write!(f, "Edge not found"),
47            GraphError::NodeNotFound => write!(f, "Node not found"),
48            GraphError::LabelNotFound => write!(f, "Label not found"),
49            GraphError::New(msg) => write!(f, "Graph error: {}", msg),
50            GraphError::Default => write!(f, "Graph error"),
51            GraphError::Empty => write!(f, "No Error"),
52            GraphError::MultipleNodesWithSameId => write!(f, "Multiple nodes with same id"),
53            GraphError::MultipleEdgesWithSameId => write!(f, "Multiple edges with same id"),
54            GraphError::InvalidNode => write!(f, "Invalid node"),
55            GraphError::ConfigFileNotFound => write!(f, "Config file not found"),
56            GraphError::SliceLengthError => write!(f, "Slice length error"),
57            GraphError::VectorError(msg) => write!(f, "Vector error: {}", msg),
58            GraphError::ShortestPathNotFound => write!(f, "Shortest path not found"),
59            GraphError::EmbeddingError(msg) => write!(f, "Error while embedding text: {}", msg),
60        }
61    }
62}
63
64impl From<HeedError> for GraphError {
65    fn from(error: HeedError) -> Self {
66        GraphError::StorageError(error.to_string())
67    }
68}
69
70impl From<std::io::Error> for GraphError {
71    fn from(error: std::io::Error) -> Self {
72        GraphError::Io(error)
73    }
74}
75
76impl From<AddrParseError> for GraphError {
77    fn from(error: AddrParseError) -> Self {
78        GraphError::ConversionError(format!("AddrParseError: {}", error.to_string()))
79    }
80}
81
82impl From<SonicError> for GraphError {
83    fn from(error: SonicError) -> Self {
84        GraphError::ConversionError(format!("sonic error: {}", error.to_string()))
85    }
86}
87
88impl From<FromUtf8Error> for GraphError {
89    fn from(error: FromUtf8Error) -> Self {
90        GraphError::ConversionError(format!("FromUtf8Error: {}", error.to_string()))
91    }
92}
93
94impl From<&'static str> for GraphError {
95    fn from(error: &'static str) -> Self {
96        GraphError::New(error.to_string())
97    }
98}
99
100impl From<String> for GraphError {
101    fn from(error: String) -> Self {
102        GraphError::New(error.to_string())
103    }
104}
105
106impl From<bincode::Error> for GraphError {
107    fn from(error: bincode::Error) -> Self {
108        GraphError::ConversionError(format!("bincode error: {}", error.to_string()))
109    }
110}
111
112impl From<ParserError> for GraphError {
113    fn from(error: ParserError) -> Self {
114        GraphError::ConversionError(format!("ParserError: {}", error.to_string()))
115    }
116}
117
118impl From<Utf8Error> for GraphError {
119    fn from(error: Utf8Error) -> Self {
120        GraphError::ConversionError(format!("Utf8Error: {}", error.to_string()))
121    }
122}
123
124impl From<uuid::Error> for GraphError {
125    fn from(error: uuid::Error) -> Self {
126        GraphError::ConversionError(format!("uuid error: {}", error.to_string()))
127    }
128}
129
130impl From<VectorError> for GraphError {
131    fn from(error: VectorError) -> Self {
132        GraphError::VectorError(format!("VectorError: {}", error.to_string()))
133    }
134}
135
136#[derive(Debug)]
137pub enum VectorError {
138    VectorNotFound(String),
139    InvalidVectorLength,
140    InvalidVectorData,
141    EntryPointNotFound,
142    ConversionError(String),
143    VectorCoreError(String),
144}
145
146impl fmt::Display for VectorError {
147    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148        match self {
149            VectorError::VectorNotFound(id) => write!(f, "Vector not found: {}", id),
150            VectorError::InvalidVectorLength => write!(f, "Invalid vector length"),
151            VectorError::InvalidVectorData => write!(f, "Invalid vector data"),
152            VectorError::EntryPointNotFound => write!(f, "Entry point not found"),
153            VectorError::ConversionError(msg) => write!(f, "Conversion error: {}", msg),
154            VectorError::VectorCoreError(msg) => write!(f, "Vector core error: {}", msg),
155        }
156    }
157}
158
159impl From<HeedError> for VectorError {
160    fn from(error: HeedError) -> Self {
161        VectorError::VectorCoreError(format!("heed error: {}", error.to_string()))
162    }
163}
164
165impl From<FromUtf8Error> for VectorError {
166    fn from(error: FromUtf8Error) -> Self {
167        VectorError::ConversionError(format!("FromUtf8Error: {}", error.to_string()))
168    }
169}
170
171impl From<Utf8Error> for VectorError {
172    fn from(error: Utf8Error) -> Self {
173        VectorError::ConversionError(format!("Utf8Error: {}", error.to_string()))
174    }
175}
176
177impl From<SonicError> for VectorError {
178    fn from(error: SonicError) -> Self {
179        VectorError::ConversionError(format!("SonicError: {}", error.to_string()))
180    }
181}
182
183impl From<bincode::Error> for VectorError {
184    fn from(error: bincode::Error) -> Self {
185        VectorError::ConversionError(format!("bincode error: {}", error.to_string()))
186    }
187}