coap_message_implementations/
error.rs

1use coap_message::error::RenderableOnMinimal;
2
3/// Error type for manipulation of [crate::inmemory_write] messages.
4///
5/// This is presented externally as an internal server error, but has enough structure to be useful
6/// for debugging through `dbg!()`.
7#[derive(Debug, thiserror::Error)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9#[non_exhaustive]
10pub enum WriteError {
11    #[error("operations did not adhere to required sequence")]
12    OutOfSequence,
13    #[error("no space in buffer")]
14    OutOfSpace,
15}
16
17impl RenderableOnMinimal for WriteError {
18    type Error<IE: RenderableOnMinimal + core::fmt::Debug> = IE;
19    fn render<M: coap_message::MinimalWritableMessage>(
20        self,
21        msg: &mut M,
22    ) -> Result<(), Self::Error<M::UnionError>> {
23        coap_message_utils::Error::internal_server_error().render::<M>(msg)
24    }
25}
26
27impl From<core::convert::Infallible> for WriteError {
28    fn from(never: core::convert::Infallible) -> Self {
29        match never {}
30    }
31}
32
33#[test]
34fn test_error_format() {
35    extern crate std;
36    use std::{format, string::String};
37
38    fn render(e: &dyn core::error::Error) -> String {
39        format!("{}", e)
40    }
41    // Not necessarily *exactly* like that text, but something like it, and implementing Error
42    assert_eq!(render(&WriteError::OutOfSpace), "no space in buffer");
43}