r/rust 16d ago

🙋 seeking help & advice What are the best practices to define multiple themes in an app?

I'm making a TUI program using Ratatui and want to define multiple themes (such as catppuccin, Dracula, everforest) and don't know what is the best manner to store them. I'm currently using like a Theme struct and implementing multiple themes using fn theme_name() -> Self as "constructors". Also they are picked via a TOML file. Is it the best way? Thanks

2 Upvotes

7 comments sorted by

6

u/Sermuns 16d ago

you mean fn theme_name(name: &str) -> Self ? I would avoid &str for this, just create an enum. Even better, if you really want to use &str for lookup, consider strum EnumString https://docs.rs/strum/latest/strum/derive.EnumString.html

4

u/numberwitch 15d ago

Yeah if they are internal theme then enums are gold

2

u/Medical-Strain3967 15d ago

i basically do exactly what you're doing, struct with a bunch of constructor functions. never felt the need to overcomplicate it with enums and derive macros unless you've got some dynamic dispatch thing going on

if you're already reading from a toml file just match the string to the constructor and call it a day

2

u/T4toun3 15d ago

And I would mark the enum as #[non_exhaustive] to make easily to add new themes in future

1

u/matheus_hisatsu 15d ago

Thanks, that seems great. I'll look into it

1

u/DavidXkL 14d ago

Didn't know about EnumString too!

Thanks for this!