Maybe this is silly but I've just took out std/fmt and used impl on the enum and did the same thing. binary size hasn't decreased yet but I haven't removed all std / fmt from all dependencies yet. Makes me feel silly for using std / fmt in the first place since it's the same amount of work either way. I think the only difference is if my MYType enum were used inside the &format! macro I'd just need to call .to_string()
pub enum MyType {A, B, /*etc...*/ }
impl MyEnumType
{
pub fn print(&self)
{
match &self
{
MyEnumType::A => web_sys::console::log_1("a"),
MyEnumType::B => web_sys::console::log_1("b"),
/*etc... */
}
}
pub fn from_str(input: &String) -> Result<MyEnumType, String>
{
match input.as_str()
{
"A" => Ok(MyEnumType::A),
"a" => Ok(MyEnumType::A),
"B" => Ok(MyEnumType::B),
"b" => Ok(MyEnumType::B),
/* etc*/
_ => Err("bad string".to_string()),
}
}
pub fn to_string(&self) -> String
{
match &self
{
MyEnumType::A => "a".to_string(),
MyEnumType::B => "b".to_string(),
/* etc ... */
}
}
}