use crate::option_extension::ExtensionError;
use coap_message::error::RenderableOnMinimal;
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum WriteError {
#[error("operations did not adhere to required sequence")]
OutOfSequence,
#[error("no space in buffer")]
OutOfSpace,
#[error("options inside the message were encoded badly")]
BadEncoding,
}
impl RenderableOnMinimal for WriteError {
type Error<IE: RenderableOnMinimal + core::fmt::Debug> = IE;
fn render<M: coap_message::MinimalWritableMessage>(
self,
msg: &mut M,
) -> Result<(), Self::Error<M::UnionError>> {
coap_message_utils::Error::internal_server_error().render::<M>(msg)
}
}
impl From<core::convert::Infallible> for WriteError {
fn from(never: core::convert::Infallible) -> Self {
match never {}
}
}
impl From<ExtensionError> for WriteError {
fn from(_value: ExtensionError) -> Self {
Self::BadEncoding
}
}
#[test]
fn test_error_format() {
extern crate std;
use std::{format, string::String};
fn render(e: &dyn core::error::Error) -> String {
format!("{}", e)
}
assert_eq!(render(&WriteError::OutOfSpace), "no space in buffer");
}