use crate::error::DecoderError;
pub mod connack;
pub mod connect;
pub mod packet;
pub mod publish;
pub mod subscribe;
pub mod unsubscribe;
include!("_ack.rs");
include!("_signal.rs");
pub trait TryFromIterator
where
Self: Sized,
{
fn try_from_iter<T>(iter: T) -> Result<Self, DecoderError>
where
T: IntoIterator<Item = u8>;
}
#[cfg(feature = "std")]
pub trait TryFromReader
where
Self: Sized,
{
fn try_read<T>(reader: T) -> Result<Self, std::io::Error>
where
T: std::io::Read;
}
#[cfg(feature = "std")]
impl<T> TryFromReader for T
where
T: TryFromIterator,
{
#[allow(clippy::unbuffered_bytes, reason = "implementors may pass a buffered reader if appropriate")]
fn try_read<R>(reader: R) -> Result<Self, std::io::Error>
where
R: std::io::Read,
{
use crate::error::Decoding;
use std::io::{Error, ErrorKind};
let mut last_error = None;
let iter = reader.bytes()
.map(|result| result.map_err(|e| last_error = Some(e)))
.map_while(|result| result.ok());
match (Self::try_from_iter(iter), last_error) {
(Ok(value), _) => Ok(value),
(Err(_), Some(e)) => Err(e),
(Err(e), _) => match e.variant {
Decoding::Truncated => Err(Error::new(ErrorKind::UnexpectedEof, e)),
Decoding::SpecViolation => Err(Error::new(ErrorKind::InvalidData, e)),
Decoding::Memory => Err(Error::new(ErrorKind::OutOfMemory, e)),
},
}
}
}
#[cfg(feature = "std")]
pub trait ToWriter {
fn write<T>(self, writer: T) -> Result<(), std::io::Error>
where
T: std::io::Write;
}
#[cfg(feature = "std")]
impl<T> ToWriter for T
where
T: IntoIterator<Item = u8>,
{
fn write<W>(self, writer: W) -> Result<(), std::io::Error>
where
W: std::io::Write,
{
use std::io::{BufWriter, Write};
let mut writer = BufWriter::new(writer);
for byte in self {
writer.write_all(&[byte])?;
}
writer.flush()
}
}