extern crate failure;
#[macro_use]
extern crate failure_derive;
#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "failed to parse '{}' to {}", input, to)]
ParseStrError {
input: String,
to: String,
}
}
pub trait AsStr {
fn as_str(&self) -> &str;
}
#[macro_export]
macro_rules! enum_str {
($name:ident, $(($key:ident, $value:expr),)*) => {
#[derive(Debug, PartialEq)]
enum $name
{
$($key),*
}
impl AsStr for $name {
fn as_str(&self) -> &str {
match self {
$(
&$name::$key => $value
),*
}
}
}
impl FromStr for $name {
type Err = Error;
fn from_str(val: &str) -> Result<Self, Self::Err> {
match val
{
$(
$value => Ok($name::$key)
),*,
_ => Err(Error::ParseStrError{input: val.to_string(), to: stringify!($name).to_string()})
}
}
}
}
}