In C++, one way to handle designing a program that needs to read multiple file formats is a Repository of Factories of Strategies. Generally the Repository part is a Singleton. The Factory takes care of building a Strategy which handles the file format. Furthermore, the Repository can be populated via static initialization, such that each Factory can register itself to the Repository before main()
is executed.
I'm struggling to see how to implement a similar system, without the Singleton component, in Rust. I'd like to be able to register Factories to some kind of file reader repository, ideally at static initialization time, with the added option for other modules or plug-ins to be able to add their own file readers at runtime.
What I'm looking for is a Rust version of something like this (which is simplified):
class Repo : boost::non_copyable {
public:
static Repo& instance();
void registerFactory(std::unique_ptr<FileFactory> factory, std::string extension);
};
template<class T>
class Factory {
public:
std::unique_ptr<T> build(std::string path) { return std::make_unique<T>(path); }
}
class AudioFile {
public:
AudioFile(std::string path);
}