1use std::io;
2use std::string::FromUtf8Error;
3
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error, PartialEq)]
9#[non_exhaustive]
10pub enum Error {
11 #[error(
12 "DataChannel message is not long enough to determine type: (expected: {expected}, actual: {actual})"
13 )]
14 UnexpectedEndOfBuffer { expected: usize, actual: usize },
15 #[error("Unknown MessageType {0}")]
16 InvalidMessageType(u8),
17 #[error("Unknown ChannelType {0}")]
18 InvalidChannelType(u8),
19 #[error("Unknown PayloadProtocolIdentifier {0}")]
20 InvalidPayloadProtocolIdentifier(u8),
21 #[error("Stream closed")]
22 ErrStreamClosed,
23
24 #[error("{0}")]
25 Util(#[from] util::Error),
26 #[error("{0}")]
27 Sctp(#[from] sctp::Error),
28 #[error("utf-8 error: {0}")]
29 Utf8(#[from] FromUtf8Error),
30
31 #[allow(non_camel_case_types)]
32 #[error("{0}")]
33 new(String),
34}
35
36impl From<Error> for util::Error {
37 fn from(e: Error) -> Self {
38 util::Error::from_std(e)
39 }
40}
41
42impl From<Error> for io::Error {
43 fn from(error: Error) -> Self {
44 match error {
45 e @ Error::Sctp(sctp::Error::ErrEof) => {
46 io::Error::new(io::ErrorKind::UnexpectedEof, e.to_string())
47 }
48 e @ Error::ErrStreamClosed => {
49 io::Error::new(io::ErrorKind::ConnectionAborted, e.to_string())
50 }
51 e => io::Error::new(io::ErrorKind::Other, e.to_string()),
52 }
53 }
54}
55
56impl PartialEq<util::Error> for Error {
57 fn eq(&self, other: &util::Error) -> bool {
58 if let Some(down) = other.downcast_ref::<Error>() {
59 return self == down;
60 }
61 false
62 }
63}
64
65impl PartialEq<Error> for util::Error {
66 fn eq(&self, other: &Error) -> bool {
67 if let Some(down) = self.downcast_ref::<Error>() {
68 return other == down;
69 }
70 false
71 }
72}