4 releases

0.2.0 Feb 6, 2019
0.1.2 Jan 28, 2019
0.1.1 Jan 28, 2019
0.1.0 Jan 28, 2019

#1832 in Encoding

MIT license

8KB
65 lines

Doodle

TBA


lib.rs:

Doodle

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]
serde_json = "1.0.0"
doodle = { version = "0.1.1", features = ["derive"] }
use doodle::*;
use serde_json::json;

#[derive(Schema)]
struct Woods {
    pub id: i32,
    pub epic: Vec<Vec<Tale>>,
}

#[derive(Schema)]
struct Tale {
    pub value: Option<String>,
}

// 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);

// print the output
let output = serde_json::to_string_pretty(&schema).unwrap();
println!("Schema:\n\n{}", output);


////////////////////// Test the example //////////////////////


let expected = json! {{
"Tale": {                                         
  "properties": {                                 
    "value": {                                    
      "type": "string",                            
      "nullable": true
    }                                             
  },                                              
  "type": "object"                                
},                                                
"Woods": {                                        
  "properties": {                                 
    "epic": {                                     
      "items": {                                  
        "items": {                                
          "$ref": "#/components/schemas/Tale"     
        },                                        
        "type": "array"                           
      },                                          
      "type": "array"                             
    },                                            
    "id": {                                       
      "type": "number"                            
    }                                             
  },                                              
  "type": "object"                                
}                                                 
}};

assert_eq!(expected, schema);

Dependencies

~360–780KB
~17K SLoC