live-data 0.1.0

Shared descriptor, manifest, and subscription types for the live-feed publisher SDK and the live-stream consumer SDK.
Documentation
//! Crate-wide error type.
//!
//! `live-data` itself is I/O-free, so most variants are placeholders consumed
//! by the higher-level crates. `Io` covers anything they need to surface up.

use std::fmt;

use crate::subscription::SubscribeErrorCode;
use crate::transport::{FormatPreference, TransportTag};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    UnknownFeed(String),
    UnsupportedTransport(TransportTag),
    UnsupportedFormat(FormatPreference),
    UnsupportedCapability(&'static str),
    InvalidDescriptor(String),
    Protocol(String),
    Io(std::io::Error),
    Refused { code: SubscribeErrorCode, message: String },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownFeed(name) => write!(f, "unknown feed: {name}"),
            Self::UnsupportedTransport(t) => write!(f, "unsupported transport: {t:?}"),
            Self::UnsupportedFormat(fmt_) => write!(f, "unsupported format: {fmt_:?}"),
            Self::UnsupportedCapability(c) => write!(f, "unsupported capability: {c}"),
            Self::InvalidDescriptor(m) => write!(f, "invalid descriptor: {m}"),
            Self::Protocol(m) => write!(f, "protocol error: {m}"),
            Self::Io(e) => write!(f, "io error: {e}"),
            Self::Refused { code, message } => write!(f, "subscription refused ({code:?}): {message}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}