diff --git a/src/client/lazy.rs b/src/client/lazy.rs index 384675a..9e08608 100644 --- a/src/client/lazy.rs +++ b/src/client/lazy.rs @@ -10,21 +10,6 @@ use std::{fmt, io}; use super::{HttpRequest, HttpStream}; -macro_rules! try_lazy ( - ($field:expr, $try:expr) => ( - match $try { - Ok(ok) => ok, - Err(e) => return Err(LazyError::with_field($field.into(), e)), - } - ); - ($try:expr) => ( - match $try { - Ok(ok) => ok, - Err(e) => return Err(LazyError::without_field(e)), - } - ) -); - /// A `LazyError` wrapping `std::io::Error`. #[allow(clippy::module_name_repetitions)] pub type LazyIoError<'a> = LazyError<'a, io::Error>; @@ -199,9 +184,15 @@ impl<'n, 'd> Multipart<'n, 'd> { req.apply_headers(prepared.boundary(), prepared.content_len()); - let mut stream = try_lazy!(req.open_stream()); + let mut stream = match req.open_stream() { + Ok(s) => s, + Err(e) => return Err(LazyError::without_field(e)), + }; - try_lazy!(io::copy(&mut prepared, &mut stream)); + match io::copy(&mut prepared, &mut stream) { + Ok(ok) => ok, + Err(e) => return Err(LazyError::without_field(e)), + }; stream.finish().map_err(LazyError::without_field) } @@ -379,8 +370,15 @@ impl<'d> PreparedField<'d> { ) -> Result<(Self, u64), LazyIoError<'n>> { let (content_type, filename) = super::mime_filename(path); - let file = try_lazy!(name, File::open(path)); - let content_len = try_lazy!(name, file.metadata()).len(); + let file = match File::open(path) { + Ok(f) => f, + Err(e) => return Err(LazyError::with_field(name, e)), + }; + + let content_len = match file.metadata() { + Ok(m) => m.len(), + Err(e) => return Err(LazyError::with_field(name, e)), + }; let stream = Self::from_stream(&name, boundary, &content_type, filename, Box::new(file)); diff --git a/src/client/mod.rs b/src/client/mod.rs index 405541d..c1a42be 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -21,15 +21,6 @@ pub use self::sized::SizedRequest; const BOUNDARY_LEN: usize = 16; -macro_rules! map_self { - ($selff:expr, $try:expr) => { - match $try { - Ok(()) => Ok($selff), - Err(err) => Err(err.into()), - } - }; -} - /// The entry point of the client-side multipart API. /// /// Though they perform I/O, the `.write_*()` methods do not return `io::Result<_>` in order to @@ -65,7 +56,10 @@ impl Multipart { name: N, val: V, ) -> Result<&mut Self, S::Error> { - map_self!(self, self.writer.write_text(name.as_ref(), val.as_ref())) + match self.writer.write_text(name.as_ref(), val.as_ref()) { + Err(err) => Err(err.into()), + Ok(()) => Ok(self), + } } /// Open a file pointed to by `path` and write its contents to the multipart request, @@ -84,10 +78,10 @@ impl Multipart { name: N, path: P, ) -> Result<&mut Self, S::Error> { - let name = name.as_ref(); - let path = path.as_ref(); - - map_self!(self, self.writer.write_file(name, path)) + match self.writer.write_file(name.as_ref(), path.as_ref()) { + Err(err) => Err(err.into()), + Ok(()) => Ok(self), + } } /// Write a byte stream to the multipart request as a file field, supplying `filename` if given, @@ -117,13 +111,13 @@ impl Multipart { filename: Option<&str>, content_type: Option, ) -> Result<&mut Self, S::Error> { - let name = name.as_ref(); - - map_self!( - self, - self.writer - .write_stream(stream, name, filename, content_type) - ) + match self + .writer + .write_stream(stream, name.as_ref(), filename, content_type) + { + Err(err) => Err(err.into()), + Ok(()) => Ok(self), + } } /// Finalize the request and return the response from the server, or the last error if set. diff --git a/src/server/field.rs b/src/server/field.rs index 985df4f..2c3f349 100644 --- a/src/server/field.rs +++ b/src/server/field.rs @@ -12,15 +12,6 @@ use thiserror::Error; const EMPTY_STR_HEADER: StrHeader<'static> = StrHeader { name: "", val: "" }; -macro_rules! invalid_cont_disp { - ($reason: expr, $cause: expr) => { - return Err(ParseHeaderError::InvalidContDisp( - $reason, - $cause.to_string(), - )) - }; -} - /// Not exposed #[derive(Copy, Clone, Debug)] pub struct StrHeader<'a> { @@ -156,22 +147,29 @@ impl ContentDisp { // assert Content-Disposition: form-data // but needs to be parsed out to trim the spaces (allowed by spec IIRC) if disp_type.trim() != "form-data" { - invalid_cont_disp!("unexpected Content-Disposition value", disp_type); + return Err(ParseHeaderError::InvalidContDisp( + "unexpected Content-Disposition value", + disp_type.to_string(), + )); } after_disp_type } - None => invalid_cont_disp!( - "expected additional data after Content-Disposition type", - header.val - ), + None => { + return Err(ParseHeaderError::InvalidContDisp( + "expected additional data after Content-Disposition type", + header.val.to_string(), + )) + } }; // Content-Disposition: form-data; name=? let (field_name, filename) = match get_str_after("name=", ';', after_disp_type) { - None => invalid_cont_disp!( - "expected field name and maybe filename, got", - after_disp_type - ), + None => { + return Err(ParseHeaderError::InvalidContDisp( + "expected field name and maybe filename, got", + after_disp_type.to_string(), + )) + } // Content-Disposition: form-data; name={field_name}; filename=? Some((field_name, after_field_name)) => { let field_name = trim_quotes(field_name); diff --git a/src/server/save.rs b/src/server/save.rs index b607557..22b2ce8 100644 --- a/src/server/save.rs +++ b/src/server/save.rs @@ -1082,38 +1082,37 @@ fn try_read_buf SaveResult>( ) -> SaveResult { let mut total_copied = 0u64; - macro_rules! try_here ( - ($try:expr) => ( - match $try { - Ok(val) => val, - Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, - Err(e) => return if total_copied == 0 { Error(e) } - else { Partial(total_copied, e.into()) }, - } - ) - ); - loop { - let res = { - let buf = try_here!(src.fill_buf()); - if buf.is_empty() { - break; + let buf = match src.fill_buf() { + Ok(val) => val, + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, + Err(e) => { + return if total_copied == 0 { + SaveResult::Error(e) + } else { + SaveResult::Partial(total_copied, e.into()) + } } - with_buf(buf) }; + if buf.is_empty() { + break; + } + + let res = with_buf(buf); + match res { - Full(copied) => { + SaveResult::Full(copied) => { src.consume(copied); total_copied += copied as u64; } - Partial(copied, reason) => { + SaveResult::Partial(copied, reason) => { src.consume(copied); total_copied += copied as u64; - return Partial(total_copied, reason); + return SaveResult::Partial(total_copied, reason); } - Error(err) => { - return Partial(total_copied, err.into()); + SaveResult::Error(err) => { + return SaveResult::Partial(total_copied, err.into()); } } } @@ -1124,27 +1123,26 @@ fn try_read_buf SaveResult>( fn try_write_all(mut buf: &[u8], mut dest: W) -> SaveResult { let mut total_copied = 0; - macro_rules! try_here ( - ($try:expr) => ( - match $try { - Ok(val) => val, - Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, - Err(e) => return if total_copied == 0 { Error(e) } - else { Partial(total_copied, e.into()) }, - } - ) - ); - while !buf.is_empty() { - match try_here!(dest.write(buf)) { - 0 => try_here!(Err(io::Error::new( - io::ErrorKind::WriteZero, - "failed to write whole buffer" - ))), - copied => { + match dest.write(buf) { + Ok(0) => { + return SaveResult::Error(io::Error::new( + io::ErrorKind::WriteZero, + "failed to write whole buffer", + )) + } + Ok(copied) => { buf = &buf[copied..]; total_copied += copied; } + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, + Err(e) => { + return if total_copied == 0 { + SaveResult::Error(e) + } else { + SaveResult::Partial(total_copied, e.into()) + } + } } }