-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Structs or enums for API responses #19
Comments
Maybe a tool for scraping the Action API documentation and converting it into Rust structs and enums? I've always wanted to have type-checked parameters for various queries. |
@enterprisey, that would be great, but I don't know how to do it: the documentation of the schema of the JSON responses doesn't seem machine-readable enough. So far I've only tried making up the structs and enums manually. |
I played with this quite a bit over the long weekend and came up with a proc_macro that generates a response struct based on query parameters: #[query(
list = "logevents",
leprop = "user|type",
)]
struct Response; (You could also specify other parameters like Here's the proof-of-concept code: https://gitlab.com/legoktm/mwapi_responses/ (rather unpolished, but mostly functional) I think this provides a reasonable balance on allowing flexible permutations of As far as autogeneration, I think we can infer most of the metadata from results, but it might need some manual adjustments (e.g. If people are interested in this I can keep working on it. |
It seems a great advantage to be able to generate structs automatically. There are a lot of API responses and it takes a lot of effort to create a set of structs with useful types and more Rustic field names. See for instance the I'd consider this ideal, but not feasible for all API responses. Maybe the output of the proc macro could be used as a starting point for a more tailored set of structs. |
FWIW I have kept working on mwapi_responses and have published it as a full crate. Most of it is autogenerated, but some I ended up writing by hand just to keep things simpler (e.g. https://gitlab.com/mwbot-rs/mwbot/-/blob/master/mwapi_responses/src/protection.rs). https://gitlab.com/mwbot-rs/mwbot/-/blob/master/mwbot/src/generators.rs demonstrates some real-world uses of the macro. I think siteinfo is a good candidate for automatic/dynamic generation since the various siprop parameters affect which fields get output. I tried to outline my vision in https://blog.legoktm.com/2021/11/01/generating-rust-types-for-mediawiki-api-responses.html - basically stay as faithful to the API response as possible, but provide convenience functions to make it more Rust-friendly: https://gitlab.com/mwbot-rs/mwbot/-/issues/2. |
Very cool, thanks so much for doing this! |
Using
serde_json::Value
to represent API responses is pretty laborious. It requires lots of.as_object()
or.as_str()
and then checking that the result isSome(_)
, orOption::map
, or matching on variants ofserde_json::Value
, etc.I propose creating custom structs or enums to represent responses. This makes accessing fields in the JSON as simple as accessing fields in the struct or enum. For instance, this example shows a struct that could be used in the
Page::text
method (and returns aserde_json::Value
if the JSON fails to deserialize asRevisionsResponse
, though that may not be necessary):Dependencies in
Cargo.toml
:To make this possible, the
Api
methods that currently decode the response asserde_json::Value
(ultimately viaApi::query_api_json
) would need to be generic, so that they could instead deserialize into a more specific struct or enum (something like, in the example above,FallibleDeserialization<RevisionsResponse>
).And
Api::get_query_api_json_limit
would probably need some way to perform the function ofApi::json_merge
generically, for the various structs that it would return in place ofserde_json::Value
. For instance it could be generic over a trait that has amerge
method (maybe namedMergeableResponse
).Difficulties: Using the
Deserialize
derive macro will add to compile time. Also it may require trial-and-error to figure out what the schema for the API responses actually is.The text was updated successfully, but these errors were encountered: