chiark / gitweb /
change mimeswap error handling
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 1 Aug 2021 19:51:51 +0000 (20:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 1 Aug 2021 19:51:51 +0000 (20:51 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock
Cargo.toml
src/prelude.rs
src/slip.rs

index 097897d9bc9bcb53daa5847436cb7ef5d9413aec..61b1daf6b03f348d65dc7f7a829be13be21ed304 100644 (file)
@@ -389,6 +389,7 @@ dependencies = [
  "regex",
  "sha2",
  "structopt",
+ "thiserror",
  "tokio",
  "void",
 ]
@@ -1017,6 +1018,26 @@ dependencies = [
  "unicode-width",
 ]
 
+[[package]]
+name = "thiserror"
+version = "1.0.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2"
+dependencies = [
+ "thiserror-impl",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "tokio"
 version = "1.8.2"
index 0b5abecd6b10e3f451e97d02c666b0e1eaff937a..f9e5ee6e76005ff521597b5679dc56d83d6419a1 100644 (file)
@@ -36,6 +36,7 @@ log = "0.4"
 sha2 = "0.9"
 structopt = "0.3"
 tokio = { version = "1", features = ["full"] }
+thiserror = "1"
 void = "1"
 
 # Not in sid:
index 8375744f84119b159fbe350b98c3e1a2f9be2a0b..d87d4fb5808f422ddcbbabd93adf7b7af171e1aa 100644 (file)
@@ -35,6 +35,7 @@ pub use itertools::{iproduct, Itertools};
 pub use lazy_regex::{regex_is_match, regex_replace_all};
 pub use log::{debug, info, error};
 pub use structopt::StructOpt;
+pub use thiserror::Error;
 pub use tokio::io::AsyncBufReadExt;
 pub use tokio::pin;
 pub use tokio::select;
index 763f24fd6e2162a00464b042e404200424fad4a3..357a8908a7e9572403146091301587cdae7403fb 100644 (file)
@@ -6,7 +6,13 @@ use crate::prelude::*;
 
 pub static SLIP_END_SLICE: &[u8] = &[SLIP_END];
 
-#[throws(AE)]
+#[derive(Error,Debug,Copy,Clone)]
+pub enum PacketError {
+  #[error("MTU exceeded ({len} > {mtu})")] MTU { len: usize, mtu: u32 },
+  #[error("Invalid SLIP escape sequence")] SLIP,
+}
+
+#[throws(PacketError)]
 pub fn check_checkmtu_mimeswap<const TO_MIME: bool>
   (mtu: u32, data: &mut [u8])
 {
@@ -14,7 +20,7 @@ pub fn check_checkmtu_mimeswap<const TO_MIME: bool>
 
   for mut packet in data.split_mut(|&c| c == SLIP_END) {
     if packet.len() > mtu.sat() {
-      throw!(anyhow!("MTU exceeded ({} > {})", packet.len(), mtu));
+      throw!(PacketError::MTU { len: packet.len(), mtu })
     }
 
     while let Some((i, was_mime)) = packet.iter().enumerate().find_map(
@@ -29,7 +35,7 @@ pub fn check_checkmtu_mimeswap<const TO_MIME: bool>
         match packet.get(i+1) {
           Some(&SLIP_ESC_END) |
           Some(&SLIP_ESC_ESC) => Ok(()),
-          _ => Err(anyhow!("SLIP escape not followed by ESC_END or ESC_ESC")),
+          _ => throw!(PacketError::SLIP),
         }?;
         packet = &mut packet[i+2 ..];
       } else {