Add schema_for
macro
This commit is contained in:
parent
463ba518b6
commit
ef5c584118
4 changed files with 14 additions and 6 deletions
|
@ -2,6 +2,9 @@ pub mod gen;
|
||||||
pub mod make_schema;
|
pub mod make_schema;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
pub use make_schema::MakeSchema;
|
pub use make_schema::MakeSchema;
|
||||||
|
|
||||||
pub use schemars_derive::*;
|
pub use schemars_derive::*;
|
||||||
|
|
6
schemars/src/macros.rs
Normal file
6
schemars/src/macros.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#[macro_export()]
|
||||||
|
macro_rules! schema_for {
|
||||||
|
($($type:tt)+) => {
|
||||||
|
$crate::gen::SchemaGenerator::new().into_root_schema_for::<$($type)+>()
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
use schemars::*;
|
use schemars::schema_for;
|
||||||
|
use schemars::schema::Schema;
|
||||||
use serde_json::Result;
|
use serde_json::Result;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let gen = gen::SchemaGenerator::new();
|
let schema = schema_for!(Schema);
|
||||||
let schema = gen.into_root_schema_for::<schema::Schema>();
|
|
||||||
let json = serde_json::to_string_pretty(&schema)?;
|
let json = serde_json::to_string_pretty(&schema)?;
|
||||||
println!("{}", json);
|
println!("{}", json);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use schemars::gen::SchemaGenerator;
|
|
||||||
use schemars::schema::*;
|
use schemars::schema::*;
|
||||||
|
use schemars::schema_for;
|
||||||
use serde_json::{from_str, to_string_pretty};
|
use serde_json::{from_str, to_string_pretty};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
@ -12,8 +12,7 @@ mod tests {
|
||||||
let expected_json = fs::read_to_string("tests/schema.json")?;
|
let expected_json = fs::read_to_string("tests/schema.json")?;
|
||||||
let expected: Schema = from_str(&expected_json)?;
|
let expected: Schema = from_str(&expected_json)?;
|
||||||
|
|
||||||
let gen = SchemaGenerator::new();
|
let actual = schema_for!(Schema);
|
||||||
let actual = gen.into_root_schema_for::<Schema>();
|
|
||||||
fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?;
|
fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?;
|
||||||
|
|
||||||
assert_eq!(actual, expected, "\n\nGenerated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\".");
|
assert_eq!(actual, expected, "\n\nGenerated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\".");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue