From: Ian Jackson Date: Sat, 1 Feb 2025 11:19:55 +0000 (+0000) Subject: ReadLimitedError: Make it generic over the error type X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=e2aeccbe567acd42bdfc7360a6d061110f4fdb9d;p=hippotat.git ReadLimitedError: Make it generic over the error type We're going to have reqwest for the client and hyper for the server. Signed-off-by: Ian Jackson --- diff --git a/server/sweb.rs b/server/sweb.rs index dc962c1..69afc90 100644 --- a/server/sweb.rs +++ b/server/sweb.rs @@ -91,7 +91,7 @@ pub async fn handle( ).await { Ok(all) => all, Err(ReadLimitedError::Truncated { sofar,.. }) => sofar, - Err(ReadLimitedError::Hyper(e)) => throw!(e), + Err(ReadLimitedError::Http(e)) => throw!(e), }; let boundary_finder = memmem::Finder::new(&boundary); diff --git a/src/utils.rs b/src/utils.rs index 823e202..5dbb616 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -17,22 +17,22 @@ pub impl Result where AE: From { } #[derive(Error,Debug)] -pub enum ReadLimitedError { +pub enum ReadLimitedError { #[error("maximum size {limit} exceeded")] Truncated { sofar: Box<[u8]>, limit: usize }, #[error("HTTP error {0}")] - Hyper(#[from] hyper::Error), + Http(#[from] H), } -impl ReadLimitedError { +impl ReadLimitedError { pub fn discard_data(&mut self) { match self { ReadLimitedError::Truncated { sofar,.. } => { mem::take(sofar); }, _ => { }, } } } #[ext] -pub impl Result { +pub impl Result> { fn discard_data(self) -> Self { self.map_err(|mut e| { e.discard_data(); e }) } @@ -52,12 +52,13 @@ pub impl io::Error { } } -#[throws(ReadLimitedError)] -pub async fn read_limited_bytes(limit: usize, initial: Box<[u8]>, +#[throws(ReadLimitedError)] +pub async fn read_limited_bytes(limit: usize, initial: Box<[u8]>, capacity: usize, stream: &mut S) -> Box<[u8]> -where S: futures::Stream> +where S: futures::Stream> + Debug + Unpin, + H: std::error::Error + 'static, // we also require that the Stream is cancellation-safe { let mut accum = initial.into_vec();