Skip to main content

tightbeam/builder/
error.rs

1#[cfg(not(feature = "std"))]
2extern crate alloc;
3
4use crate::Version;
5
6#[cfg(feature = "derive")]
7use crate::Errorizable;
8
9/// Errors specific to metadata validation
10#[cfg_attr(feature = "derive", derive(Errorizable))]
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum MetadataError {
13	/// Missing required ID field
14	#[cfg_attr(feature = "derive", error("Missing required field: id"))]
15	MissingId,
16
17	/// Missing required order field
18	#[cfg_attr(feature = "derive", error("Missing required field: order"))]
19	MissingTimestamp,
20
21	/// Missing required hash field (V2+)
22	#[cfg_attr(feature = "derive", error("Missing required field: hash (required for V2)"))]
23	MissingHash,
24
25	/// Missing required encryption info (V1+)
26	#[cfg_attr(
27		feature = "derive",
28		error("Missing required field: encryption (required for V1+)")
29	)]
30	MissingEncryption,
31
32	/// Field not supported in this protocol version
33	#[cfg_attr(
34		feature = "derive",
35		error("Field '{field}' is not supported in protocol version {version:?}")
36	)]
37	UnsupportedField { field: &'static str, version: Version },
38}
39
40#[cfg(not(feature = "derive"))]
41impl core::fmt::Display for MetadataError {
42	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43		match self {
44			Self::MissingId => write!(f, "Missing required field: id"),
45			Self::MissingTimestamp => write!(f, "Missing required field: order"),
46			Self::MissingHash => write!(f, "Missing required field: hash (required for V2)"),
47			Self::MissingEncryption => write!(f, "Missing required field: encryption (required for V1+)"),
48			Self::UnsupportedField { field, version } => {
49				write!(f, "Field '{field}' is not supported in protocol version {version:?}")
50			}
51		}
52	}
53}
54
55#[cfg(not(feature = "derive"))]
56impl core::error::Error for MetadataError {}
57
58/// Errors that can occur during builder operations
59#[cfg_attr(feature = "derive", derive(Errorizable))]
60#[derive(Debug)]
61pub enum BuildError {
62	/// Invalid metadata configuration
63	#[cfg_attr(feature = "derive", error("Invalid metadata: {0}"))]
64	InvalidMetadata(MetadataError),
65
66	/// Glitch in the Matrix
67	#[cfg_attr(feature = "derive", error("Matrix error: {0}"))]
68	#[cfg_attr(feature = "derive", from)]
69	MatrixError(crate::matrix::MatrixError),
70
71	/// Error during serialization
72	#[cfg_attr(feature = "derive", error("Serialization error: {0}"))]
73	#[cfg_attr(feature = "derive", from)]
74	Serialization(der::Error),
75
76	/// Error during encryption
77	#[cfg(feature = "aead")]
78	#[cfg_attr(feature = "derive", error("Encryption error: {0}"))]
79	#[cfg_attr(feature = "derive", from)]
80	Encryption(aead::Error),
81
82	/// Error during signing
83	#[cfg(feature = "signature")]
84	#[cfg_attr(feature = "derive", error("Signature error: {0}"))]
85	#[cfg_attr(feature = "derive", from)]
86	Signature(signature::Error),
87
88	/// Error during compression
89	#[cfg(feature = "compress")]
90	#[cfg_attr(feature = "derive", error("Compression error: {0}"))]
91	Compression(crate::error::CompressionError),
92
93	/// Error obtaining random bytes
94	#[cfg(feature = "random")]
95	#[cfg_attr(feature = "derive", error("Random number generation error: {0}"))]
96	#[cfg_attr(feature = "derive", from)]
97	Random(rand_core::Error),
98
99	/// Missing message body
100	#[cfg_attr(feature = "derive", error("Missing message body"))]
101	MissingMessage,
102}
103
104#[cfg(not(feature = "derive"))]
105impl core::fmt::Display for BuildError {
106	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
107		match self {
108			Self::InvalidMetadata(err) => write!(f, "Invalid metadata: {err}"),
109			Self::Serialization(err) => write!(f, "Serialization error: {err}"),
110			Self::MissingMessage => write!(f, "Missing message body"),
111			#[cfg(feature = "aead")]
112			Self::Encryption(err) => write!(f, "Encryption error: {err}"),
113			#[cfg(feature = "signature")]
114			Self::Signature(err) => write!(f, "Signature error: {err}"),
115			#[cfg(feature = "compress")]
116			Self::Compression(err) => write!(f, "Compression error: {err}"),
117			#[cfg(feature = "random")]
118			Self::Random(err) => write!(f, "Random number generation error: {err}"),
119		}
120	}
121}
122
123#[cfg(not(feature = "derive"))]
124impl core::error::Error for BuildError {}
125
126#[cfg(not(feature = "derive"))]
127crate::impl_from!(MetadataError => BuildError::InvalidMetadata);
128#[cfg(not(feature = "derive"))]
129crate::impl_from!(der::Error => BuildError::Serialization);
130#[cfg(all(feature = "aead", not(feature = "derive")))]
131crate::impl_from!(aead::Error => BuildError::Encryption);
132#[cfg(all(feature = "signature", not(feature = "derive")))]
133crate::impl_from!(signature::Error => BuildError::Signature);
134#[cfg(all(feature = "random", not(feature = "derive")))]
135crate::impl_from!(rand_core::Error => BuildError::Random);
136#[cfg(all(feature = "compress", not(feature = "derive")))]
137crate::impl_from!(crate::error::CompressionError => BuildError::Compression);