powerio_prob/scopf/
error.rs1use std::fmt;
2
3#[derive(Debug)]
5#[non_exhaustive]
6pub enum ScopfError {
7 Json(serde_json::Error),
8 Source(powerio::Error),
9 UnsupportedFormat(String),
10 InvalidDocument(String),
11}
12
13pub type ScopfResult<T> = std::result::Result<T, ScopfError>;
14
15impl ScopfError {
16 pub(super) fn invalid(message: impl Into<String>) -> Self {
17 Self::InvalidDocument(message.into())
18 }
19}
20
21impl fmt::Display for ScopfError {
22 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
23 match self {
24 Self::Json(error) => write!(formatter, "invalid SCOPF JSON: {error}"),
25 Self::Source(error) => write!(formatter, "invalid GOC3 source: {error}"),
26 Self::UnsupportedFormat(format) => {
27 write!(formatter, "unsupported SCOPF source format `{format}`")
28 }
29 Self::InvalidDocument(message) => formatter.write_str(message),
30 }
31 }
32}
33
34impl std::error::Error for ScopfError {
35 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36 match self {
37 Self::Json(error) => Some(error),
38 Self::Source(error) => Some(error),
39 Self::UnsupportedFormat(_) | Self::InvalidDocument(_) => None,
40 }
41 }
42}
43
44impl From<serde_json::Error> for ScopfError {
45 fn from(error: serde_json::Error) -> Self {
46 Self::Json(error)
47 }
48}
49
50impl From<powerio::Error> for ScopfError {
51 fn from(error: powerio::Error) -> Self {
52 Self::Source(error)
53 }
54}