fplus_lib/
error.rs

1use std::{
2    fmt::Display,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use actix_web::{
8    body::{BodySize, MessageBody},
9    web::Bytes,
10};
11
12#[derive(Debug)]
13pub enum LDNError {
14    New(String),
15    Load(String),
16}
17
18impl Display for LDNError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            LDNError::Load(e) => {
22                write!(f, "Load: {}", e)
23            }
24            LDNError::New(e) => {
25                write!(f, "New: {}", e)
26            }
27        }
28    }
29}
30
31impl MessageBody for LDNError {
32    type Error = std::convert::Infallible;
33
34    fn size(&self) -> BodySize {
35        match self {
36            LDNError::Load(e) => BodySize::Sized(e.len() as u64),
37            LDNError::New(e) => BodySize::Sized(e.len() as u64),
38        }
39    }
40
41    fn poll_next(
42        self: Pin<&mut Self>,
43        _cx: &mut Context<'_>,
44    ) -> Poll<Option<Result<Bytes, Self::Error>>> {
45        match Pin::<&mut LDNError>::into_inner(self) {
46            LDNError::Load(e) => Poll::Ready(Some(Ok(Bytes::from(e.clone())))),
47            LDNError::New(e) => Poll::Ready(Some(Ok(Bytes::from(e.clone())))),
48        }
49    }
50}