Skip to main content

veloq_core/
diagnostic.rs

1use serde::Serialize;
2use std::borrow::Cow;
3use std::error::Error;
4use std::fmt;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub struct ErrorCode(&'static str);
8
9impl ErrorCode {
10    pub const IO_READ: Self = Self::new("io.read");
11    pub const IO_WRITE: Self = Self::new("io.write");
12    pub const IO_STAT: Self = Self::new("io.stat");
13    pub const IO_CREATE_DIR: Self = Self::new("io.create-dir");
14    pub const IO_REMOVE: Self = Self::new("io.remove");
15    pub const IO_PUBLISH: Self = Self::new("io.publish");
16    pub const ENCODING_UTF8: Self = Self::new("encoding.utf8");
17    pub const COMPRESSION_GZIP: Self = Self::new("compression.gzip");
18    pub const ARROW_RECORD_BATCH: Self = Self::new("arrow.record-batch");
19    pub const PARQUET_OPEN_WRITER: Self = Self::new("parquet.open-writer");
20    pub const PARQUET_WRITE: Self = Self::new("parquet.write");
21    pub const PARQUET_CLOSE: Self = Self::new("parquet.close");
22    pub const OUTPUT_CSV_HEADER: Self = Self::new("output.csv-header");
23    pub const OUTPUT_CSV_ROW: Self = Self::new("output.csv-row");
24    pub const OUTPUT_CSV_FLUSH: Self = Self::new("output.csv-flush");
25    pub const SIDECAR_ENCODE: Self = Self::new("sidecar.encode");
26    pub const SIDECAR_DECODE: Self = Self::new("sidecar.decode");
27
28    pub const fn new(code: &'static str) -> Self {
29        Self(code)
30    }
31
32    pub const fn as_str(self) -> &'static str {
33        self.0
34    }
35}
36
37impl fmt::Display for ErrorCode {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(self.0)
40    }
41}
42
43impl Serialize for ErrorCode {
44    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45    where
46        S: serde::Serializer,
47    {
48        serializer.serialize_str(self.0)
49    }
50}
51
52pub trait VeloqDiagnostic: Error + Send + Sync + 'static {
53    fn code(&self) -> ErrorCode;
54
55    fn message(&self) -> Cow<'_, str> {
56        Cow::Owned(self.to_string())
57    }
58
59    fn hint(&self) -> Option<Cow<'_, str>> {
60        None
61    }
62}