use std::str::FromStr;
use std::fmt;
#[derive(Clone, PartialEq)]
pub struct Variable {
pub symbol: char
}
impl Variable {
pub fn named(symbol: char) -> Self {
Self { symbol }
}
pub fn new(c: char) -> Self {
Self::named(c)
}
}
impl fmt::Debug for Variable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.symbol)
}
}
impl FromStr for Variable {
type Err = String;
fn from_str(s: &str) -> Result<Variable, Self::Err> {
let chars = s.chars().collect::<Vec<_>>();
match chars.len() {
0 => Err(format!("Variables must be one character long (none given).")),
1 => Ok(Variable { symbol: chars[0] }),
_ => Err(format!("Variables cannot be longer than one character ({} found).", chars.len()))
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}