1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Hl7Error {
9 pub message: String,
11 pub code: Option<String>,
13}
14
15impl Hl7Error {
16 pub const REQUIRED_FIELD_MISSING: &'static str =
18 "Validation Error - Required field missing in message";
19 pub const UNSUPPORTED_MESSAGE_TYPE: &'static str =
21 "Validation Error - Message Type not supported by this implementation";
22 pub const BAD_MESSAGE: &'static str = "Validation Error - Bad Message";
24 pub const PARSING_ERROR: &'static str = "Parsing Error";
26 pub const SERIALIZATION_ERROR: &'static str = "Serialization Error";
28
29 pub fn new(message: impl Into<String>) -> Self {
31 Self { message: message.into(), code: None }
32 }
33
34 pub fn with_code(message: impl Into<String>, code: impl Into<String>) -> Self {
36 Self { message: message.into(), code: Some(code.into()) }
37 }
38}
39
40impl fmt::Display for Hl7Error {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match &self.code {
43 Some(code) => write!(f, "{code} : {}", self.message),
44 None => write!(f, "{}", self.message),
45 }
46 }
47}
48
49impl std::error::Error for Hl7Error {}