chiark / gitweb /
Move code for into_crlfs macro to client.rs
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 9 May 2026 11:38:44 +0000 (12:38 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 10 May 2026 11:05:40 +0000 (12:05 +0100)
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 <ijackson@chiark.greenend.org.uk>
client/client.rs
client/into_crlfs.rs [new file with mode: 0644]
macros/macros.rs

index c002a43e8f622206fad7b67c387c0901c7e264e4..e45d1650e3f2b42c1b5241e0e1b10d8a00184e1b 100644 (file)
@@ -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::<String>();
+
+    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 (file)
index 0000000..e69de29
index 4cc50f4505effe03c1adbb0ebebac92cafcb9024..be148cf849607378789a60d5f1ccffe310562213 100644 (file)
 #![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::<String>();
-  //dbg!(&output);
-  let output = LitStr::new(&output, token.span());
-  let output = quote!(#output);
-  output.into()
-}