use crate::models::CustomSet;
use std::hash::Hash;
pub trait PrettyPrint {
fn pretty(&self) -> String;
fn compact(&self) -> String;
}
impl<T: Eq + Hash + Clone + std::fmt::Display> PrettyPrint for CustomSet<T> {
fn pretty(&self) -> String {
if self.is_empty() {
"∅".to_string()
} else {
let elements: Vec<_> = self.iter().collect();
format!(
"{{ {} }}",
elements
.iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(", ")
)
}
}
fn compact(&self) -> String {
if self.is_empty() {
"∅".to_string()
} else {
let elements: Vec<_> = self.iter().collect();
format!(
"{{{}}}",
elements
.iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(",")
)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pretty_print() {
let set = CustomSet::from(vec![1, 2, 3]);
let pretty = set.pretty();
assert!(pretty.contains("1"));
assert!(pretty.contains("2"));
assert!(pretty.contains("3"));
}
#[test]
fn test_compact_print() {
let set = CustomSet::from(vec![1, 2, 3]);
let compact = set.compact();
assert!(compact.starts_with("{"));
assert!(compact.ends_with("}"));
}
#[test]
fn test_empty_pretty() {
let empty = CustomSet::<i32>::empty();
assert_eq!(empty.pretty(), "∅");
assert_eq!(empty.compact(), "∅");
}
}