nautilus_serialization/sbe/
error.rs1use std::{error::Error, fmt::Display};
19
20pub const MAX_GROUP_SIZE: u32 = 10_000;
22
23#[derive(Debug, Clone, PartialEq, Eq)]
25pub enum SbeEncodeError {
26 StringTooLong {
28 field: &'static str,
30 len: usize,
32 max: usize,
34 },
35 GroupSizeTooLarge {
37 group: &'static str,
39 count: usize,
41 max: u32,
43 },
44 NumericOverflow {
46 field: &'static str,
48 },
49}
50
51impl Display for SbeEncodeError {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match self {
54 Self::StringTooLong { field, len, max } => {
55 write!(
56 f,
57 "String field `{field}` length {len} exceeds maximum {max}"
58 )
59 }
60 Self::GroupSizeTooLarge { group, count, max } => {
61 write!(f, "Group `{group}` size {count} exceeds maximum {max}")
62 }
63 Self::NumericOverflow { field } => {
64 write!(f, "Numeric value overflows encoded field {field}")
65 }
66 }
67 }
68}
69
70impl Error for SbeEncodeError {}
71
72#[derive(Debug, Clone, PartialEq, Eq)]
74pub enum SbeDecodeError {
75 BufferTooShort {
77 expected: usize,
79 actual: usize,
81 },
82 SchemaMismatch {
84 expected: u16,
86 actual: u16,
88 },
89 VersionMismatch {
91 expected: u16,
93 actual: u16,
95 },
96 UnknownTemplateId(u16),
98 GroupSizeTooLarge {
100 count: u32,
102 max: u32,
104 },
105 InvalidBlockLength {
107 expected: u16,
109 actual: u16,
111 },
112 InvalidUtf8,
114 InvalidEnumValue {
116 type_name: &'static str,
118 value: u16,
120 },
121 NumericOverflow {
123 type_name: &'static str,
125 },
126 InvalidValue {
128 field: &'static str,
130 },
131}
132
133impl Display for SbeDecodeError {
134 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135 match self {
136 Self::BufferTooShort { expected, actual } => {
137 write!(
138 f,
139 "Buffer too short: expected {expected} bytes, was {actual}"
140 )
141 }
142 Self::SchemaMismatch { expected, actual } => {
143 write!(f, "Schema ID mismatch: expected {expected}, was {actual}")
144 }
145 Self::VersionMismatch { expected, actual } => {
146 write!(
147 f,
148 "Schema version mismatch: expected {expected}, was {actual}"
149 )
150 }
151 Self::UnknownTemplateId(id) => write!(f, "Unknown template ID: {id}"),
152 Self::GroupSizeTooLarge { count, max } => {
153 write!(f, "Group size {count} exceeds maximum {max}")
154 }
155 Self::InvalidBlockLength { expected, actual } => {
156 write!(f, "Invalid block length: expected {expected}, was {actual}")
157 }
158 Self::InvalidUtf8 => write!(f, "Invalid UTF-8 in string field"),
159 Self::InvalidEnumValue { type_name, value } => {
160 write!(f, "Invalid enum value {value} for {type_name}")
161 }
162 Self::NumericOverflow { type_name } => {
163 write!(f, "Numeric value overflows target type {type_name}")
164 }
165 Self::InvalidValue { field } => write!(f, "Invalid value for {field}"),
166 }
167 }
168}
169
170impl Error for SbeDecodeError {}
171
172#[cfg(test)]
173mod tests {
174 use rstest::rstest;
175
176 use super::*;
177
178 #[rstest]
179 fn test_string_too_long_display() {
180 let err = SbeEncodeError::StringTooLong {
181 field: "symbol",
182 len: 300,
183 max: 65535,
184 };
185 assert_eq!(
186 err.to_string(),
187 "String field `symbol` length 300 exceeds maximum 65535"
188 );
189 }
190
191 #[rstest]
192 fn test_numeric_overflow_display() {
193 let err = SbeEncodeError::NumericOverflow {
194 field: "BarSpecification.step",
195 };
196 assert_eq!(
197 err.to_string(),
198 "Numeric value overflows encoded field BarSpecification.step"
199 );
200 }
201
202 #[rstest]
203 fn test_buffer_too_short_display() {
204 let err = SbeDecodeError::BufferTooShort {
205 expected: 100,
206 actual: 50,
207 };
208 assert_eq!(
209 err.to_string(),
210 "Buffer too short: expected 100 bytes, was 50"
211 );
212 }
213
214 #[rstest]
215 fn test_schema_mismatch_display() {
216 let err = SbeDecodeError::SchemaMismatch {
217 expected: 3,
218 actual: 1,
219 };
220 assert_eq!(err.to_string(), "Schema ID mismatch: expected 3, was 1");
221 }
222
223 #[rstest]
224 fn test_group_size_too_large_display() {
225 let err = SbeDecodeError::GroupSizeTooLarge {
226 count: 50000,
227 max: 10000,
228 };
229 assert_eq!(err.to_string(), "Group size 50000 exceeds maximum 10000");
230 }
231
232 #[rstest]
233 fn test_error_equality() {
234 let err1 = SbeDecodeError::InvalidUtf8;
235 let err2 = SbeDecodeError::InvalidUtf8;
236 assert_eq!(err1, err2);
237 }
238
239 #[rstest]
240 fn test_invalid_enum_value_display() {
241 let err = SbeDecodeError::InvalidEnumValue {
242 type_name: "OrderSide",
243 value: 99,
244 };
245 assert_eq!(err.to_string(), "Invalid enum value 99 for OrderSide");
246 }
247}