use std::fmt::Display;
use crate::rollresult::DiceResult;
#[derive(Debug, Clone)]
pub enum Value {
Int(i64),
Float(f64),
}
impl Value {
pub fn get_value(&self) -> i64 {
match *self {
Value::Int(i) => i,
Value::Float(f) => f as i64,
}
}
}
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match *self {
Value::Int(i) => i.to_string(),
Value::Float(f) => f.to_string(),
};
write!(f, "{}", s)
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum RollHistory {
ReRolls(Vec<Vec<DiceResult>>),
Roll(Vec<DiceResult>),
Fudge(Vec<u64>),
Value(Value),
Separator(&'static str),
OpenParenthesis,
CloseParenthesis,
}
impl Display for RollHistory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
RollHistory::ReRolls(v) => {
let s2 = v
.iter()
.map(|r| {
r.iter()
.map(|r| r.res.to_string())
.collect::<Vec<_>>()
.join(" -> ")
})
.collect::<Vec<_>>()
.join(", ");
format!("[{}] -> ", s2)
}
RollHistory::Roll(v) => {
let s2 = v
.iter()
.map(|r| r.res.to_string())
.collect::<Vec<_>>()
.join(", ");
format!("[{}]", s2)
}
RollHistory::Fudge(v) => {
let mut s = String::new();
s.push('[');
let len = v.len();
v.iter().enumerate().for_each(|(i, r)| {
let r = if *r <= 2 {
"-"
} else if *r <= 4 {
"▢"
} else {
"+"
};
s.push_str(r);
if i < len - 1 {
s.push_str(", ");
}
});
s.push(']');
s
}
RollHistory::Value(v) => {
let mut s = String::new();
s.push_str(&v.to_string());
s
}
RollHistory::Separator(sep) => {
let mut s = String::new();
s.push_str(sep);
s
}
RollHistory::OpenParenthesis => "(".to_string(),
RollHistory::CloseParenthesis => ")".to_string(),
};
write!(f, "{}", s)
}
}