The Doodle library provides means for data structures to document themselves loosely following the OpenAPI specification specification
Types that implement the Schema
trait can document themselves using the methods available.
See the Schema
trait for more info
The crate also re-exports doodle_derive
which provides the derive Schema
which implements
The neccessary methods to serialize the data structure
Cargo.toml:
[dependencies]
doodle = { version = "0.1.1", features = ["derive"] }
serde_json = "1.0.0" # To pretty print our result
main.rs:
extern crate doodle;
#[macro_use]
extern crate serde_json;
use doodle::*;
#[derive(Schema)]
struct Woods {
pub id: i32,
pub epic: Vec<Tale>,
}
#[derive(Schema)]
struct Tale {
pub name: Option<String>,
}
fn main() {
// Initialize an empty dict
let mut schema = json! {{}};
let map = schema.as_object_mut().unwrap();
// Append our schemas
Woods::append_to_schema(map);
Tale::append_to_schema(map);
let output = serde_json::to_string_pretty(&schema).unwrap();
println!("Schema:\n\n{}", output);
}
Outputs
Schema:
{
"Tale": {
"properties": {
"name": {
"nullable": true,
"type": "string"
}
},
"type": "object"
},
"Woods": {
"properties": {
"epic": {
"items": {
"$ref": "#/components/schemas/Tale"
},
"type": "array"
},
"id": {
"type": "number"
}
},
"type": "object"
}
}