I am using custom types for routing to different pages in my web app. I like using custom types for page routing because the compiler checks ensure that all routes to all pages work.
I am using Browser.application and I also handle onUrlChange requests. So, I need to translate url fragments (a string) to my custom type again. That is simple enough. Just create a bunch of if…then statements mapping strings to the custom type variants.
However, if I add a variant to my custom type and forget to add another if…then statement in url -> custom type translate function my web app contains a bug. The compiler does not catch this.
So, I thought of using a Codec using elm-serializer. And this works. I have compiler checks and encoding en decoding are nicely linked to each other.
However, my url now contains meaningless translations of variants. For example: the variant Apple is translated to AQAA and I don’t like AQAA in the url’s of my web app. I would like to have the variant Apple translated to ‘apple’.
I know elm-serialize says in the README: not good for “Encoding to a human readable format”.
Still, my question is: can I use a custom variant encoder/decoder in elm-serialize? Or is there another way to closely link the encoding and decoding of a custom type?
I have created a simple Ellie app that uses a custom Codec for a custom type. See: https://ellie-app.com/bT6w5QwwbP6a1