BeFunctional / haskell-foreign-rust

MIT License
9 stars 3 forks source link

Question: How to marshall a Rust `enum`? #12

Open khazaddum opened 1 year ago

khazaddum commented 1 year ago

What would be the right way of marshalling a Rust enum like this one?

pub enum Fruit {
   Apple,
   Orange,
   Banana
}

Does it depend on the enum having fmt::Display implemented?

edsko commented 1 year ago

I would use the borsh crate to derive BorshSerialize

#[derive(BorshSerialize)]
pub enum Fruit { .. }

at which point the ToHaskell<YourApp> instance is trivial:


impl ToHaskell<YourApp> for Fruit {
    fn to_haskell<W: Write>(&self, writer: &mut W, _tag: PhantomData<YourApp>) -> Result<()> {
        self.serialize(writer)?;
        Ok(())
    }
}