reifydb_type/error/
mod.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the MIT, see license.md file
3
4use std::{
5	fmt::{Display, Formatter},
6	ops::{Deref, DerefMut},
7};
8
9use serde::{de, ser};
10
11pub mod diagnostic;
12mod r#macro;
13
14use diagnostic::{Diagnostic, conversion, render::DefaultRenderer};
15
16#[derive(Debug, PartialEq)]
17pub struct Error(pub Diagnostic);
18
19impl Deref for Error {
20	type Target = Diagnostic;
21
22	fn deref(&self) -> &Self::Target {
23		&self.0
24	}
25}
26
27impl DerefMut for Error {
28	fn deref_mut(&mut self) -> &mut Self::Target {
29		&mut self.0
30	}
31}
32
33impl Display for Error {
34	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35		let out = DefaultRenderer::render_string(&self.0);
36		f.write_str(out.as_str())
37	}
38}
39
40impl Error {
41	pub fn diagnostic(self) -> Diagnostic {
42		self.0
43	}
44}
45
46impl std::error::Error for Error {}
47
48impl de::Error for Error {
49	fn custom<T: Display>(msg: T) -> Self {
50		crate::error!(diagnostic::serde::serde_deserialize_error(msg.to_string()))
51	}
52}
53
54impl ser::Error for Error {
55	fn custom<T: Display>(msg: T) -> Self {
56		crate::error!(diagnostic::serde::serde_serialize_error(msg.to_string()))
57	}
58}
59
60impl From<std::num::TryFromIntError> for Error {
61	fn from(err: std::num::TryFromIntError) -> Self {
62		crate::error!(conversion::integer_conversion_error(err))
63	}
64}
65
66impl From<std::array::TryFromSliceError> for Error {
67	fn from(err: std::array::TryFromSliceError) -> Self {
68		crate::error!(conversion::array_conversion_error(err))
69	}
70}
71
72impl From<std::string::FromUtf8Error> for Error {
73	fn from(err: std::string::FromUtf8Error) -> Self {
74		crate::error!(conversion::utf8_conversion_error(err))
75	}
76}