chiark / gitweb /
sort out error handling for server rx slip frames
[hippotat.git] / src / slip.rs
index 955c36f624dddd027e0798fe2e7c04947af1d480..6bb508bf2cfe25894aa00dcc3f27c5dfcaa4472d 100644 (file)
@@ -24,13 +24,14 @@ impl SlipMime for Slip2Mime { const CONV_TO: Option<bool> = Some(true); }
 impl SlipMime for Mime2Slip { const CONV_TO: Option<bool> = Some(false); }
 impl SlipMime for SlipNoConv { const CONV_TO: Option<bool> = None; }
 
-#[derive(Debug)]
-#[derive(Error)]
-#[error("only bad IP datagrams")]
-pub struct ErrorOnlyBad;
-
-#[throws(ErrorOnlyBad)]
-pub fn checkn<AC, EH, OUT, ACR, M: SlipMime+Copy>(
+#[derive(Debug,Error)]
+pub enum SlipFramesError<E> where E: std::error::Error + 'static {
+  #[error("only bad IP datagrams")] ErrorOnlyBad,
+  #[error("{0}")] Other(#[from] E),
+}
+  
+#[throws(SlipFramesError<EHE>)]
+pub fn checkn<AC, EH, EHE, OUT, ACR, M: SlipMime+Copy>(
   mime: M,
   mtu: u32,
   data: &[u8],
@@ -39,7 +40,8 @@ pub fn checkn<AC, EH, OUT, ACR, M: SlipMime+Copy>(
   mut error_handler: EH
 ) where AC: Fn(&[u8]) -> Result<ACR, PacketError> + Copy,
         OUT: FnMut((Box<[u8]>, ACR)) -> Result<(), PacketError>,
-        EH: FnMut(PacketError),
+        EH: FnMut(PacketError) -> Result<(), SlipFramesError<EHE>>,
+        EHE: std::error::Error + 'static,
 {
   //  eprintln!("before: {:?}", DumpHex(data));
   if data.is_empty() { return }
@@ -54,11 +56,11 @@ pub fn checkn<AC, EH, OUT, ACR, M: SlipMime+Copy>(
       Ok::<_,PE>(())
     })() {
       Ok(()) => { },
-      Err(e) => { err=true; error_handler(e); },
+      Err(e) => { err=true; error_handler(e)?; },
     }
   }
 //  eprintln!(" after: {:?}", DumpHex(data));
-  if err && !ok { throw!(ErrorOnlyBad) }
+  if err && !ok { throw!(SlipFramesError::ErrorOnlyBad) }
 }
 
 #[throws(PacketError)]