From: Ian Jackson Date: Sat, 9 May 2026 11:38:44 +0000 (+0100) Subject: Move code for into_crlfs macro to client.rs X-Git-Tag: debian/1.3.3~5^2~7 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=a90d1c0ed2b18490d965fb7ee1118797a51dec8c;p=hippotat.git Move code for into_crlfs macro to client.rs This abolishes the macro. It will allow us to abolish our macro crate entirely. Right now the version in client.rs is a bit odd and flabby. Signed-off-by: Ian Jackson --- diff --git a/client/client.rs b/client/client.rs index c002a43..e45d165 100644 --- a/client/client.rs +++ b/client/client.rs @@ -9,7 +9,44 @@ #![allow(clippy::while_let_loop)] use hippotat::prelude::*; -use hippotat_macros::into_crlfs; + +/// Return a `String` made of each argument followed by CRLF. +macro_rules! lines_crlf { { $($content:expr),* $(,)? } => { + { + #[allow(unused_mut)] + let mut s = String::new(); + $( + write!(s, "{}\r\n", $content).expect("write failed"); + )* + s + } +} } + +/// Displays as the lines of `s`, deindented, with CRLFs +/// +/// TODO has anomalous handling of the last line. +pub struct LitLinesCrlf<'s>(pub &'s str); + +impl Display for LitLinesCrlf<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let input = &self.0; + + // TODO don't collect + // TODO more principled indentation handling + let output = input.split_inclusive('\n') + .map(|s| s.trim_start_matches(&[' ','\t'][..])) + .map(|s| match s.strip_suffix("\n") { + None => [s, ""], + Some(l) => [l, "\r\n"], + }) + .flatten() + .collect::(); + + let output = output.strip_suffix("\r\n").expect("XXXX bad input"); + write!(f, "{output}") + } +} + #[derive(clap::Parser,Debug)] pub struct Opts { @@ -71,18 +108,12 @@ fn submit_request<'r, 'c:'r>( let req_num = { *req_num += 1; *req_num }; - let prefix1 = format!(into_crlfs!( + let prefix1 = lines_crlf!(&LitLinesCrlf( r#"--b Content-Type: text/plain; charset="utf-8" Content-Disposition: form-data; name="m" - {} - {} - {} - {} - {} - {} - {}"#), + "#), &c.ic.link.client, token, c.ic.target_requests_outstanding, @@ -92,7 +123,7 @@ fn submit_request<'r, 'c:'r>( c.ic.max_batch_up, ); - let prefix2 = format!(into_crlfs!( + let prefix2 = lines_crlf!(&LitLinesCrlf( r#" --b Content-Type: application/octet-stream @@ -100,7 +131,7 @@ fn submit_request<'r, 'c:'r>( "#), ); - let suffix = format!(into_crlfs!( + let suffix = lines_crlf!(&LitLinesCrlf( r#" --b-- "#), diff --git a/client/into_crlfs.rs b/client/into_crlfs.rs new file mode 100644 index 0000000..e69de29 diff --git a/macros/macros.rs b/macros/macros.rs index 4cc50f4..be148cf 100644 --- a/macros/macros.rs +++ b/macros/macros.rs @@ -13,24 +13,3 @@ #![allow(clippy::map_flatten)] #![allow(clippy::single_char_pattern)] -use syn::LitStr; -use quote::quote; - -#[proc_macro] -pub fn into_crlfs(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let input: proc_macro2::TokenStream = input.into(); - let token: LitStr = syn::parse2(input).expect("expected literal"); - let input = token.value(); - let output = input.split_inclusive('\n') - .map(|s| s.trim_start_matches(&[' ','\t'][..])) - .map(|s| match s.strip_suffix("\n") { - None => [s, ""], - Some(l) => [l, "\r\n"], - }) - .flatten() - .collect::(); - //dbg!(&output); - let output = LitStr::new(&output, token.span()); - let output = quote!(#output); - output.into() -}