From 2c83d55cb0082ff8fe40ae6fc08666279175cb83 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 14 Apr 2020 13:25:52 +0200 Subject: [PATCH] api-macro: util: add join and join_debug helper Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/src/api/structs.rs | 9 +-------- proxmox-api-macro/src/util.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/proxmox-api-macro/src/api/structs.rs b/proxmox-api-macro/src/api/structs.rs index da7fab14..3ccccb1e 100644 --- a/proxmox-api-macro/src/api/structs.rs +++ b/proxmox-api-macro/src/api/structs.rs @@ -188,14 +188,7 @@ fn handle_regular_struct( // now error out about all the fields not found in the struct: if !schema_fields.is_empty() { - let bad_fields = schema_fields.keys().fold(String::new(), |mut acc, key| { - if !acc.is_empty() { - acc.push_str(", "); - acc - } else { - key.to_owned() - } - }); + let bad_fields = util::join(", ", schema_fields.keys()); bail!( schema.span, "struct does not contain the following fields: {}", diff --git a/proxmox-api-macro/src/util.rs b/proxmox-api-macro/src/util.rs index d5e3d8eb..c169c1b8 100644 --- a/proxmox-api-macro/src/util.rs +++ b/proxmox-api-macro/src/util.rs @@ -542,3 +542,31 @@ impl Parse for AttrArgs { }) } } + +/// Join an iterator over `Display` values. +pub fn join(separator: &str, iter: impl Iterator) -> String +where + T: std::fmt::Display, +{ + let mut text = String::new(); + let mut sep = ""; + for i in iter { + text = format!("{}{}{}", text, sep, i); + sep = separator; + } + text +} + +/// Join an iterator over `Debug` values. +pub fn join_debug(separator: &str, iter: impl Iterator) -> String +where + T: std::fmt::Debug, +{ + let mut text = String::new(); + let mut sep = ""; + for i in iter { + text = format!("{}{}{:?}", text, sep, i); + sep = separator; + } + text +}