Debian Codegraph @

src/macros.rs

raw symbols SCIP
1 /// Generates a [`Schema`](crate::Schema) for the given type using default settings.
2 /// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
3 ///
4 /// The type must implement [`JsonSchema`](crate::JsonSchema).
5 ///
6 /// # Example
7 /// ```
8 /// use schemars::{schema_for, JsonSchema};
9 ///
10 /// #[derive(JsonSchema)]
11 /// struct MyStruct {
12 /// foo: i32,
13 /// }
14 ///
15 /// let my_schema = schema_for!(MyStruct);
16 /// ```
17 #[cfg(doc)]
18 #[macro_export]
19 macro_rules! schema_for {
20 ($type:ty) => {
21 $crate::SchemaGenerator::default().into_root_schema_for::<$type>()
22 };
23 }
24
25 /// Generates a [`Schema`](crate::Schema) for the given type using default settings.
26 /// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
27 ///
28 /// The type must implement [`JsonSchema`](crate::JsonSchema).
29 ///
30 /// # Example
31 /// ```
32 /// use schemars::{schema_for, JsonSchema};
33 ///
34 /// #[derive(JsonSchema)]
35 /// struct MyStruct {
36 /// foo: i32,
37 /// }
38 ///
39 /// let my_schema = schema_for!(MyStruct);
40 /// ```
41 #[cfg(not(doc))]
42 #[macro_export]
43 macro_rules! schema_for {
44 ($type:ty) => {
45 $crate::SchemaGenerator::default().into_root_schema_for::<$type>()
46 };
47 ($_:expr) => {
48 compile_error!("This argument to `schema_for!` is not a type - did you mean to use `schema_for_value!` instead?")
49 };
50 }
51
52 /// Generates a [`Schema`](crate::Schema) for the given example value using default settings.
53 /// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
54 ///
55 /// The value must implement [`Serialize`](serde::Serialize). If the value also implements
56 /// [`JsonSchema`](crate::JsonSchema), then prefer using the [`schema_for!(Type)`](schema_for) macro
57 /// which will generally produce a more precise schema, particularly when the value contains any
58 /// enums.
59 ///
60 /// If the `Serialize` implementation of the value decides to fail, this macro will panic.
61 /// For a non-panicking alternative, create a [`SchemaGenerator`](crate::SchemaGenerator) and use
62 /// its [`into_root_schema_for_value`](crate::SchemaGenerator::into_root_schema_for_value) method.
63 ///
64 /// # Example
65 /// ```
66 /// use schemars::schema_for_value;
67 ///
68 /// #[derive(serde::Serialize)]
69 /// struct MyStruct {
70 /// foo: i32,
71 /// }
72 ///
73 /// let my_schema = schema_for_value!(MyStruct { foo: 123 });
74 /// ```
75 #[macro_export]
76 macro_rules! schema_for_value {
77 ($value:expr) => {
78 $crate::SchemaGenerator::default()
79 .into_root_schema_for_value(&$value)
80 .unwrap()
81 };
82 }
83
84 /// Construct a [`Schema`](crate::Schema) from a JSON literal. This can either be a JSON object, or
85 /// a boolean (`true` or `false`).
86 ///
87 /// You can interpolate variables or expressions into a JSON object using the same rules as the
88 /// [`serde_json::json`] macro.
89 ///
90 /// # Example
91 /// ```
92 /// use schemars::{Schema, json_schema};
93 ///
94 /// let desc = "A helpful description.";
95 /// let my_schema: Schema = json_schema!({
96 /// "description": desc,
97 /// "type": ["object", "null"]
98 /// });
99 /// ```
100 #[macro_export]
101 macro_rules! json_schema {
102 (
103 {$($json_object:tt)*}
104 ) => {
105 <$crate::Schema as ::core::convert::TryFrom<_>>::try_from($crate::_private::serde_json::json!({$($json_object)*})).unwrap()
106 };
107 (true) => {
108 $crate::Schema::from(true)
109 };
110 (false) => {
111 $crate::Schema::from(false)
112 };
113 }