api-macro: util: add join and join_debug helper

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-04-14 13:25:52 +02:00
parent 0b2c9b95b4
commit 2c83d55cb0
2 changed files with 29 additions and 8 deletions

View File

@ -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: {}",

View File

@ -542,3 +542,31 @@ impl Parse for AttrArgs {
})
}
}
/// Join an iterator over `Display` values.
pub fn join<T>(separator: &str, iter: impl Iterator<Item = T>) -> 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<T>(separator: &str, iter: impl Iterator<Item = T>) -> 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
}