schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Add examples attribute

Open ede1998 opened this issue 4 years ago • 0 comments
trafficstars

I added a small feature: I tried to add multiple examples to a single struct field but found it quite cumbersome. I had to create a new function for every example values I wanted to add.

Therefore I implemented an attribute examples that works just like example. But instead of providing a single example value, the user can provide anything implementing IntoIterator to return an arbitrary number of examples from a single function.

Example:

#[derive(JsonSchema, Deserialize)]
struct Config {
    #[schemars(examples = "revisions")]
    git_revision: String,
}

fn revisions() -> [&'static str; 3] {
    ["master", "v0.3.0", "5dfcdf8eec66a051ecd85625518cfd13"]
}

This was my first time writing proc-macros so please look out for errors when reviewing (e.g. macro hygiene?).

ede1998 avatar Nov 13 '21 14:11 ede1998