semantic_operators/
error.rs1use std::fmt;
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum ErrorKind {
6 InvalidQuestion,
8 InvalidArgument,
10 InvalidState,
12 InvalidImage,
14 InvalidRequest,
16 InvalidResponse,
18 Provider,
20 Timeout,
22 Unsupported,
24 Runtime,
26}
27#[derive(Clone, Debug, PartialEq, Eq)]
29pub struct Error {
30 kind: ErrorKind,
31 message: String,
32 path: Option<String>,
33}
34impl Error {
35 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
37 Self {
38 kind,
39 message: message.into(),
40 path: None,
41 }
42 }
43 pub fn with_path(mut self, path: impl Into<String>) -> Self {
45 self.path = Some(path.into());
46 self
47 }
48 pub fn kind(&self) -> ErrorKind {
50 self.kind
51 }
52 pub fn message(&self) -> &str {
54 &self.message
55 }
56 pub fn path(&self) -> Option<&str> {
58 self.path.as_deref()
59 }
60}
61impl fmt::Display for Error {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 if let Some(p) = self.path.as_ref().filter(|p| !p.is_empty()) {
64 write!(f, "{p}: ")?;
65 }
66 f.write_str(&self.message)
67 }
68}
69impl std::error::Error for Error {}