mpd_protocol/
lib.rs

1#![warn(
2    missing_debug_implementations,
3    missing_docs,
4    rust_2018_idioms,
5    unreachable_pub
6)]
7#![deny(rustdoc::broken_intra_doc_links)]
8#![forbid(unsafe_code)]
9#![cfg_attr(docsrs, feature(doc_cfg))]
10
11//! Implementation of the client protocol for [MPD]. Supports binary responses and command lists.
12//!
13//! # Crate Features
14//!
15//! | Feature | Description                     |
16//! |---------|---------------------------------|
17//! | `async` | Async support, based on [Tokio] |
18//!
19//! [MPD]: https://musicpd.org
20//! [Tokio]: https://tokio.rs
21
22pub mod command;
23pub mod response;
24
25mod connection;
26mod parser;
27
28use std::{error::Error, fmt, io};
29
30#[cfg(feature = "async")]
31pub use self::connection::AsyncConnection;
32pub use self::{
33    command::{Command, CommandList},
34    connection::Connection,
35};
36
37/// Unrecoverable errors.
38#[derive(Debug)]
39pub enum MpdProtocolError {
40    /// IO error occurred
41    Io(io::Error),
42    /// A message could not be parsed successfully.
43    InvalidMessage,
44}
45
46impl fmt::Display for MpdProtocolError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            MpdProtocolError::Io(_) => write!(f, "IO error"),
50            MpdProtocolError::InvalidMessage => write!(f, "invalid message"),
51        }
52    }
53}
54
55#[doc(hidden)]
56impl From<io::Error> for MpdProtocolError {
57    fn from(e: io::Error) -> Self {
58        MpdProtocolError::Io(e)
59    }
60}
61
62impl Error for MpdProtocolError {
63    fn source(&self) -> Option<&(dyn Error + 'static)> {
64        match self {
65            MpdProtocolError::Io(e) => Some(e),
66            MpdProtocolError::InvalidMessage => None,
67        }
68    }
69}