Skip to content

Commit b566ac3

Browse files
feat: add schemars support
1 parent a8a0498 commit b566ac3

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benzina/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ features = [
1414
"postgres",
1515
"mysql",
1616
"serde",
17+
"schemars",
1718
"typed-uuid",
1819
"example-generated",
1920
"dangerous-construction",
@@ -26,6 +27,7 @@ rustdoc-args = ["--cfg", "docsrs"]
2627
diesel = { version = "2.3", default-features = false, optional = true }
2728
serde_core = { version = "1.0.221", optional = true }
2829
serde_json = { version = "1.0.144", optional = true }
30+
schemars = { version = "1", default-features = false, optional = true }
2931
utoipa = { version = "5", optional = true }
3032
benzina-derive = { path = "../benzina-derive", version = "=0.4.4", optional = true }
3133
uuid = { version = ">=0.7.0, <2.0.0", default-features = false, optional = true }
@@ -47,6 +49,7 @@ typed-uuid = ["postgres", "diesel/uuid", "dep:uuid"]
4749
mysql = ["benzina-derive?/mysql"]
4850

4951
serde = ["dep:serde_core", "uuid?/serde"]
52+
schemars = ["dep:schemars"]
5053
utoipa = ["dep:utoipa"]
5154

5255
example-generated = ["typed-uuid"]

benzina/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub mod example_generated;
3131
mod int;
3232
#[cfg(feature = "json")]
3333
mod json;
34+
#[cfg(all(feature = "schemars", feature = "postgres"))]
35+
mod schemars;
3436
#[cfg(all(feature = "serde", feature = "postgres"))]
3537
mod serde;
3638
#[cfg(feature = "postgres")]

benzina/src/schemars.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::borrow::Cow;
2+
3+
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
4+
5+
use crate::{U15, U31, U63};
6+
7+
macro_rules! impl_schemars_numbers {
8+
($($type:ident => $format:literal),*) => {
9+
$(
10+
impl JsonSchema for $type {
11+
fn schema_name() -> Cow<'static, str> {
12+
stringify!($type).into()
13+
}
14+
15+
fn json_schema(_: &mut SchemaGenerator) -> Schema {
16+
json_schema!({
17+
"type": "integer",
18+
"format": $format,
19+
"minimum": $type::MIN.get(),
20+
"maximum": $type::MAX.get()
21+
})
22+
}
23+
}
24+
)*
25+
}
26+
}
27+
28+
impl_schemars_numbers! {
29+
U15 => "int16",
30+
U31 => "int32",
31+
U63 => "int64"
32+
}

0 commit comments

Comments
 (0)