Skip to main content

live_data/
error.rs

1//! Crate-wide error type.
2//!
3//! `live-data` itself is I/O-free, so most variants are placeholders consumed
4//! by the higher-level crates. `Io` covers anything they need to surface up.
5
6use std::fmt;
7
8use crate::subscription::SubscribeErrorCode;
9use crate::transport::{FormatPreference, TransportTag};
10
11pub type Result<T> = std::result::Result<T, Error>;
12
13#[derive(Debug)]
14#[non_exhaustive]
15pub enum Error {
16    UnknownFeed(String),
17    UnsupportedTransport(TransportTag),
18    UnsupportedFormat(FormatPreference),
19    UnsupportedCapability(&'static str),
20    InvalidDescriptor(String),
21    Protocol(String),
22    Io(std::io::Error),
23    Refused { code: SubscribeErrorCode, message: String },
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::UnknownFeed(name) => write!(f, "unknown feed: {name}"),
30            Self::UnsupportedTransport(t) => write!(f, "unsupported transport: {t:?}"),
31            Self::UnsupportedFormat(fmt_) => write!(f, "unsupported format: {fmt_:?}"),
32            Self::UnsupportedCapability(c) => write!(f, "unsupported capability: {c}"),
33            Self::InvalidDescriptor(m) => write!(f, "invalid descriptor: {m}"),
34            Self::Protocol(m) => write!(f, "protocol error: {m}"),
35            Self::Io(e) => write!(f, "io error: {e}"),
36            Self::Refused { code, message } => write!(f, "subscription refused ({code:?}): {message}"),
37        }
38    }
39}
40
41impl std::error::Error for Error {
42    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43        match self {
44            Self::Io(e) => Some(e),
45            _ => None,
46        }
47    }
48}
49
50impl From<std::io::Error> for Error {
51    fn from(e: std::io::Error) -> Self {
52        Self::Io(e)
53    }
54}