mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Protocol] Implement generic response with dyn (#56)
* Implement generic response as enum * First draft of implementing into_common() * Make common response type generic * Use macros and generics to reduce repetition * [Games] Add dynamically dispatched CommonResponse trait 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. * [Game] Implement common traits for JCMP2 response * [Fmt] Run cargo fmt * Fix doctest failing * Run cargo fmt
This commit is contained in:
parent
bf14ecb4a4
commit
b368877031
17 changed files with 405 additions and 430 deletions
|
|
@ -1,15 +1,17 @@
|
|||
use gamedig::{protocols::GenericResponse, query, GDResult, GAMES};
|
||||
use gamedig::{protocols::types::CommonResponse, query, GDResult, GAMES};
|
||||
|
||||
use std::net::IpAddr;
|
||||
|
||||
fn generic_query(game_name: &str, addr: &IpAddr, port: Option<u16>) -> GDResult<GenericResponse> {
|
||||
fn generic_query(game_name: &str, addr: &IpAddr, port: Option<u16>) -> GDResult<Box<dyn CommonResponse>> {
|
||||
let game = GAMES.get(game_name).expect("Game doesn't exist");
|
||||
|
||||
println!("Querying {:#?} with game {:#?}.", addr, game.name);
|
||||
println!("Querying {:#?} with game {:#?}.", addr, game);
|
||||
|
||||
let response = query(game, addr, port)?;
|
||||
println!("Response: {:#?}", response.as_json());
|
||||
|
||||
println!("{:#?}", response);
|
||||
let common = response.as_original();
|
||||
println!("Common response: {:#?}", common);
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue