fhir_model/
error.rs

1//! Error implementation.
2
3#[cfg(feature = "builders")]
4use derive_builder::UninitializedFieldError;
5
6/// Wrong resource type for conversion to the specified type.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct WrongResourceType(pub String, pub String);
9
10impl std::fmt::Display for WrongResourceType {
11	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12		write!(f, "The Resource is of a different type ({}) than requested ({})", self.0, self.1)
13	}
14}
15
16impl std::error::Error for WrongResourceType {}
17
18/// Error that may occur during the String to Date conversion
19#[derive(Debug)]
20pub enum DateFormatError {
21	/// Date parsing error
22	TimeParsing(time::error::Parse),
23	/// Integer to Month conversion error
24	TimeComponentRange(time::error::ComponentRange),
25	/// String to Integer conversion error
26	IntParsing(std::num::ParseIntError),
27	/// String splitting error
28	StringSplit,
29	/// Incorrectly formatted date error
30	InvalidDate,
31}
32
33impl std::fmt::Display for DateFormatError {
34	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35		match self {
36			Self::TimeParsing(err) => write!(f, "Couldn't parse date: {err}"),
37			Self::TimeComponentRange(err) => write!(f, "Invalid month: {err}"),
38			Self::IntParsing(err) => write!(f, "Couldn't parse string to integer: {err}"),
39			Self::StringSplit => f.write_str("Couldn't split string"),
40			Self::InvalidDate => f.write_str("Invalid date format"),
41		}
42	}
43}
44
45impl std::error::Error for DateFormatError {
46	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47		match self {
48			Self::TimeParsing(err) => Some(err),
49			Self::TimeComponentRange(err) => Some(err),
50			Self::IntParsing(err) => Some(err),
51			_ => None,
52		}
53	}
54}
55
56impl From<time::error::Parse> for DateFormatError {
57	fn from(value: time::error::Parse) -> Self {
58		Self::TimeParsing(value)
59	}
60}
61
62impl From<time::error::ComponentRange> for DateFormatError {
63	fn from(value: time::error::ComponentRange) -> Self {
64		Self::TimeComponentRange(value)
65	}
66}
67
68impl From<std::num::ParseIntError> for DateFormatError {
69	fn from(value: std::num::ParseIntError) -> Self {
70		Self::IntParsing(value)
71	}
72}
73
74#[cfg(feature = "builders")]
75/// Builder errors.
76#[derive(Debug)]
77pub struct BuilderError(pub UninitializedFieldError);
78
79#[cfg(feature = "builders")]
80impl std::fmt::Display for BuilderError {
81	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82		self.0.fmt(f)
83	}
84}
85
86#[cfg(feature = "builders")]
87impl std::error::Error for BuilderError {
88	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
89		Some(&self.0)
90	}
91}
92
93#[cfg(feature = "builders")]
94impl From<UninitializedFieldError> for BuilderError {
95	fn from(err: UninitializedFieldError) -> Self {
96		Self(err)
97	}
98}