1use std::fmt;
2use std::fmt::{Debug, Display};
3use std::string::FromUtf8Error;
4
5pub enum FhirError {
6 Message(String),
7 UnImplementation(String),
8 EndArrayWhileParsingList,
9 FromStrError(String),
10}
11
12impl FhirError {
13 pub fn error(msg: &str) -> Self {
14 FhirError::Message(String::from(msg))
15 }
16
17 pub fn error_string(msg: String) -> Self {
18 FhirError::Message(msg)
19 }
20
21 pub fn un_implementation(msg: &str) -> Self {
22 FhirError::UnImplementation(String::from(msg))
23 }
24}
25
26impl From<std::io::Error> for FhirError {
27 fn from(value: std::io::Error) -> Self {
28 FhirError::Message(value.to_string())
29 }
30}
31
32impl From<xml::reader::Error> for FhirError {
33 fn from(value: xml::reader::Error) -> Self {
34 FhirError::Message(value.to_string())
35 }
36}
37
38impl From<xml::writer::Error> for FhirError {
39 fn from(value: xml::writer::Error) -> Self {
40 FhirError::Message(value.to_string())
41 }
42}
43
44impl From<core::convert::Infallible> for FhirError {
45 fn from(value: core::convert::Infallible) -> Self {
46 FhirError::FromStrError(value.to_string())
47 }
48}
49
50impl From<std::num::ParseIntError> for FhirError {
51 fn from(value: std::num::ParseIntError) -> Self {
52 FhirError::FromStrError(value.to_string())
53 }
54}
55
56impl From<std::num::ParseFloatError> for FhirError {
57 fn from(value: std::num::ParseFloatError) -> Self {
58 FhirError::FromStrError(value.to_string())
59 }
60}
61
62impl From<std::str::ParseBoolError> for FhirError {
63 fn from(value: std::str::ParseBoolError) -> Self {
64 FhirError::FromStrError(value.to_string())
65 }
66}
67
68impl From<chrono::format::ParseError> for FhirError {
69 fn from(value: chrono::format::ParseError) -> Self {
70 FhirError::FromStrError(value.to_string())
71 }
72}
73
74impl From<FromUtf8Error> for FhirError {
75 fn from(value: FromUtf8Error) -> Self {
76 FhirError::FromStrError(value.to_string())
77 }
78}
79
80impl Debug for FhirError {
81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82 match self {
83 FhirError::Message(msg) => write!(f, "错误消息: {:?}", msg),
84 FhirError::UnImplementation(func) => write!(f, "函数未实现: {:?}", func),
85 FhirError::EndArrayWhileParsingList => write!(f, "解析数组时遇到数组结束符号"),
86 FhirError::FromStrError(msg) => write!(f, "字符串转换错误: {:?}", msg),
87 }
88 }
89}
90
91impl Display for FhirError {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93 Debug::fmt(self, f)
94 }
95}
96