use std::fmt::{self, Display};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Combination {
sets: Vec<String>,
}
impl Combination {
pub fn new(sets: &[&str]) -> Self {
let mut set_vec: Vec<String> = sets.iter().map(|s| s.to_string()).collect();
set_vec.sort();
Combination { sets: set_vec }
}
pub fn sets(&self) -> &[String] {
&self.sets
}
pub fn len(&self) -> usize {
self.sets.len()
}
pub fn is_empty(&self) -> bool {
self.sets.is_empty()
}
pub fn contains_all(&self, other: &Combination) -> bool {
other.sets.iter().all(|s| self.sets.contains(s))
}
}
impl Display for Combination {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.sets.join("&"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_combination_new() {
let combo = Combination::new(&["A", "B"]);
assert_eq!(combo.sets(), &["A", "B"]);
assert_eq!(combo.len(), 2);
}
#[test]
fn test_combination_sorted() {
let combo1 = Combination::new(&["B", "A"]);
let combo2 = Combination::new(&["A", "B"]);
assert_eq!(combo1, combo2);
}
#[test]
fn test_combination_to_string() {
let combo = Combination::new(&["A", "B", "C"]);
assert_eq!(combo.to_string(), "A&B&C");
}
}