gamedig / rust-gamedig

Game Server Query Library.
https://crates.io/crates/gamedig
MIT License
38 stars 11 forks source link

Implement generic response with dyn #56

Closed Douile closed 1 year ago

Douile commented 1 year ago

Sorry for the large amount of PRs, I believe it is useful to see a draft of what each variation of this could look like.

This would replace #53 and I believe this is the nicest way to do implement it after the discussion in discord.

This adds two traits: "CommonResponse", and "CommonPlayer", when the generic game query function returns a response it returns a pointer to its original response type that implements "CommonResponse".

Both common traits require that "as_original()" be implemented, this returns an enum containing a pointer to the original type.

Both traits have a concrete method "as_json()" that returns a struct containing data fetched from all of its methods as. This struct implements serde and can hence be serialized as required.

The traits require a few other methods be implemented, those being the fields that are common across all types. All other methods have a default None implementation so that each response type only needs to implement methods for fields that it has.

https://github.com/Douile/rust-gamedig/blob/b8b47314fe2a0b63871939fdbbb4109070924093/src/protocols/types.rs#L48-L90

After receiving a CommonResponse the user can then call each dynamically dispatched method to get the information they need or if they need all of it in a serializable form they can use the as_json() convenience method (see examples/generic.rs).

CosminPerRam commented 1 year ago

Looks really good, it's quite an elegant solution that ticks everything (?) we wanted. Don't forget to rebase and it's merge time.