#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Suggestion {
pub path: String,
pub kind: String,
}
impl Suggestion {
pub fn new(path: impl Into<String>, kind: impl Into<String>) -> Self {
Self {
path: path.into(),
kind: kind.into(),
}
}
}
pub(super) fn join_suggestions(items: &[Suggestion]) -> String {
if items.is_empty() {
return "(none)".to_string();
}
items
.iter()
.map(|s| format!("{} ({})", s.path, s.kind))
.collect::<Vec<_>>()
.join(", ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_empty_list_reads_as_none_in_the_sentence() {
assert_eq!(join_suggestions(&[]), "(none)");
}
#[test]
fn entries_keep_rank_order_and_the_kind_label() {
let out = join_suggestions(&[
Suggestion::new("option::Option::unwrap", "method"),
Suggestion::new("option::Option::unzip", "method"),
]);
assert_eq!(
out,
"option::Option::unwrap (method), option::Option::unzip (method)"
);
}
}