pub mod standard;
#[derive(Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct DieSymbol {
name: String
}
impl DieSymbol {
pub fn new(val: &str) -> Result<DieSymbol, String> {
let trimmed = val.trim();
match trimmed.len() {
0 => Err("Value cannot be empty".to_string()),
_ => Ok(DieSymbol { name: trimmed.to_string() })
}
}
pub fn name(&self) -> &String {
&self.name
}
}
#[derive(Clone)]
pub struct DieSide {
symbols: Vec<DieSymbol>
}
impl DieSide {
pub fn new(symbols: Vec<DieSymbol>) -> DieSide {
DieSide { symbols }
}
pub fn symbols(&self) -> &[DieSymbol] {
&self.symbols.as_slice()
}
}
#[derive(Clone)]
pub struct Die {
sides: Vec<DieSide>
}
impl Die {
pub fn new(sides: Vec<DieSide>) -> Result<Die, String> {
match sides.len() {
0 => Err("Die must have at least 2 sides".to_string()),
1 => Err("Die must have at least 2 sides".to_string()),
_ => Ok(Die { sides })
}
}
pub fn sides(&self) -> &[DieSide] {
self.sides.as_slice()
}
pub fn unique_symbols(&self) -> Vec<DieSymbol> {
let mut unique = Vec::new();
for symbol in
self.sides.iter()
.map(|s| s.symbols())
.flatten()
.cloned()
.collect::<Vec<DieSymbol>>() {
if !unique.contains(&symbol) {
unique.push(symbol);
}
}
unique
}
}