I'm trying to make a website where you can convert between different file formats. I wanted to do it in Python, but decided I wanted to try to make it in Rust instead.
In Python, I wanted to represent each file as a class, make a list of functions representing the conversions, and iterate over the conversions to see which conversions are possible on a file (for example by using inspect.signature and isinstance).
In Rust, I have no idea how to do something like that. I wanted to use From and Into, but AFAIK there's not really a way to dynamically get the traits implemented for a type.
There might be a way to do it by representing each conversion as a separate trait with a method that returns an Option, and implement all of those traits for all the files. Apparently it's possible to use specialization to implement a default for it, but I can't get it to work, and that would be a suboptimal solution.
My first instinct would be to create an enum FileType with the file types your app wants to support and a struct File which represents an existing file. I'd then implement a method convert on File which will take the target FileType as argument and performs the conversion based on its own type and the given type. Here's a sketch:
Note that this scheme will require writing n² conversions. If there are a lot of file types that you want to support, it is often helpful to define some kind of intermediate in-memory representation. That way, you can define separate import (file → intermediate) and export (intermediate → file) operations for each filetype. convert_a_to_b(...) then becomes export_b(import_a(...)) and you only need to write 2n separate implementations.