Skip to content

Schema documentation library for Rust structs

License

Notifications You must be signed in to change notification settings

snorrwe/doodle-rs

Repository files navigation

Doodle

Build Status doodle doodle_derive Documentation Licence

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

Usage example

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"
  }
}

About

Schema documentation library for Rust structs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages