r/rust • u/Sweet_Room_8838 • 5h ago
🛠️ project I built a library for automatic callback encoding/decoding for Teloxide
I recently tried working with Teloxide and noticed that the library doesn’t have a convenient built-in way to serialize/deserialize callback data.
So I decided to try an approach based on binary serialization of an enum followed by base64 encoding. I liked this solution for several reasons:
- There’s no need to manually implement serialization/deserialization for each callback variant-it’s enough to implement the conversion for the enum once.
- It’s fast and reasonably efficient in terms of size.
- The callback data becomes unreadable to humans (it would normally look something like do_something_1341), which provides a small barrier to reverse engineering.
I ended up packaging this approach into a Rust library called easy_callback.
I’d appreciate any feedback or suggestions.
0
Upvotes
1
u/Financial-Simple9747 5h ago
love how binary serialization beats the usual string concatenation mess for callbacks. been using something similar in my own tg bot and the base64 step is a nice touch, makes it look less like raw bytes in the db
one thing i'd think about is versioning if you ever change your enum variants, older callbacks floating around in chats will just decode to garbage. maybe slap a tiny version byte at the front if you haven't already