together-rs 0.4.0

A simple tool for running multiple commands together
Documentation
use std::sync::mpsc;

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

#[derive(Debug)]
pub enum TogetherError {
    Io(std::io::Error),
    Yaml(serde_yml::Error),
    TomlSerialize(toml::ser::Error),
    TomlDeserialize(toml::de::Error),
    ChannelRecvError(mpsc::RecvError),
    PopenErrorError(subprocess::PopenError),
    InternalError(TogetherInternalError),
    DynError(Box<dyn std::error::Error>),
}

#[derive(Debug)]
pub enum TogetherInternalError {
    ProcessFailedToExit,
    UnexpectedResponse,
    InvalidConfigExtension,
}

impl std::fmt::Display for TogetherError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use TogetherInternalError as TIE;
        match self {
            TogetherError::Io(e) => write!(f, "IO error: {}", e),
            TogetherError::Yaml(e) => write!(f, "YAML error: {}", e),
            TogetherError::TomlSerialize(e) => write!(f, "TOML serialization error: {}", e),
            TogetherError::TomlDeserialize(e) => write!(f, "TOML deserialization error: {}", e),
            TogetherError::ChannelRecvError(e) => write!(f, "Channel receive error: {}", e),
            TogetherError::PopenErrorError(e) => write!(f, "Process error: {}", e),
            TogetherError::InternalError(TIE::ProcessFailedToExit) => {
                write!(f, "Process failed to exit")
            }
            TogetherError::InternalError(TIE::UnexpectedResponse) => {
                write!(f, "Unexpected response from process")
            }
            TogetherError::InternalError(TIE::InvalidConfigExtension) => {
                write!(f, "Invalid configuration file extension")
            }
            TogetherError::DynError(e) => write!(f, "Error: {}", e),
        }
    }
}

impl std::error::Error for TogetherError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            TogetherError::Io(e) => Some(e),
            TogetherError::Yaml(e) => Some(e),
            TogetherError::TomlSerialize(e) => Some(e),
            TogetherError::TomlDeserialize(e) => Some(e),
            TogetherError::ChannelRecvError(e) => Some(e),
            TogetherError::PopenErrorError(e) => Some(e),
            TogetherError::InternalError(_) => None,
            TogetherError::DynError(e) => Some(e.as_ref()),
        }
    }
}

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

impl From<serde_yml::Error> for TogetherError {
    fn from(e: serde_yml::Error) -> Self {
        TogetherError::Yaml(e)
    }
}

impl From<toml::ser::Error> for TogetherError {
    fn from(e: toml::ser::Error) -> Self {
        TogetherError::TomlSerialize(e)
    }
}

impl From<toml::de::Error> for TogetherError {
    fn from(e: toml::de::Error) -> Self {
        TogetherError::TomlDeserialize(e)
    }
}

impl From<mpsc::RecvError> for TogetherError {
    fn from(e: mpsc::RecvError) -> Self {
        TogetherError::ChannelRecvError(e)
    }
}

impl From<subprocess::PopenError> for TogetherError {
    fn from(e: subprocess::PopenError) -> Self {
        TogetherError::PopenErrorError(e)
    }
}

impl From<TogetherInternalError> for TogetherError {
    fn from(e: TogetherInternalError) -> Self {
        TogetherError::InternalError(e)
    }
}

impl From<Box<dyn std::error::Error>> for TogetherError {
    fn from(e: Box<dyn std::error::Error>) -> Self {
        TogetherError::DynError(e)
    }
}