Skip to content

Commit 239336a

Browse files
committed
refactor body formatting
1 parent 9e73df5 commit 239336a

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/main.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,32 @@ fn main() {
176176
.map(|os_str| os_str.to_string_lossy().to_string())
177177
.unwrap_or("???".to_string());
178178

179-
let invocation_ctx = {
180-
let mut out = String::new();
179+
let subject = format!("{sender}@{hostname}: {summary}");
180+
181+
let body = (|| {
182+
let mut body = String::new();
183+
writeln!(
184+
&mut body,
185+
"A process on host {hostname:?} invoked the sendmail binary."
186+
)?;
187+
writeln!(
188+
&mut body,
189+
"The original message is attached inline to this wrapper message."
190+
)?;
191+
writeln!(&mut body)?;
192+
writeln!(&mut body, "Invocation args: {args}")?;
193+
writeln!(&mut body)?;
181194
writeln!(
182-
&mut out,
195+
&mut body,
183196
"uid:{} gid:{} euid:{} egid:{}",
184197
users::get_current_uid(),
185198
users::get_current_gid(),
186199
users::get_effective_uid(),
187200
users::get_effective_gid()
188-
)
189-
.unwrap();
201+
)?;
190202
let mut display_or_none = |what, value: Option<OsString>| {
191203
writeln!(
192-
&mut out,
204+
&mut body,
193205
"{what}: {}",
194206
value
195207
.as_ref()
@@ -202,28 +214,19 @@ fn main() {
202214
display_or_none("groupname", users::get_current_groupname());
203215
display_or_none("effective username", users::get_effective_username());
204216
display_or_none("effective groupname", users::get_effective_groupname());
205-
writeln!(&mut out).unwrap();
206-
writeln!(&mut out, "hostname: {}", whoami::hostname()).unwrap();
207-
writeln!(&mut out, "device name: {}", whoami::devicename()).unwrap();
208-
writeln!(&mut out, "distro: {}", whoami::distro()).unwrap();
209-
writeln!(&mut out, "platform: {}", whoami::platform()).unwrap();
210-
out
211-
};
212-
213-
let subject = format!("{sender}@{hostname}: {summary}");
214-
let body = format!(
215-
r#"A process on host {hostname:?} invoked the sendmail binary.
216-
The original message is attached inline to this wrapper message.
217-
218-
Invocation args: {args}
219-
220-
{invocation_ctx}
221-
222-
(This message was generated by forwad-as-attachment-mta)
223-
224-
225-
"#
226-
);
217+
writeln!(&mut body)?;
218+
writeln!(&mut body, "hostname: {}", whoami::hostname())?;
219+
writeln!(&mut body, "device name: {}", whoami::devicename())?;
220+
writeln!(&mut body, "distro: {}", whoami::distro())?;
221+
writeln!(&mut body, "platform: {}", whoami::platform())?;
222+
writeln!(&mut body)?;
223+
writeln!(
224+
&mut body,
225+
"This message was generated by forwad-as-attachment-mta"
226+
)?;
227+
std::result::Result::<_, std::fmt::Error>::Ok(body)
228+
})()
229+
.expect("this is all in-memory and we don't expect formatting to fail");
227230

228231
let envelope = Envelope::new(
229232
Some(config.sender_email.clone()),

0 commit comments

Comments
 (0)