Skip to content

Commit

Permalink
Implement schema_id() for more types
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 17, 2023
1 parent 15f0a3e commit 12ea862
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"rust-analyzer.check.command": "clippy"
"rust-analyzer.check.command": "clippy",
"rust-analyzer.showUnlinkedFileNotification": false
}
9 changes: 9 additions & 0 deletions schemars/src/json_schema_impls/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::schema::*;
use crate::JsonSchema;
use chrono::prelude::*;
use serde_json::json;
use std::borrow::Cow;

impl JsonSchema for Weekday {
no_ref_schema!();
Expand All @@ -11,6 +12,10 @@ impl JsonSchema for Weekday {
"Weekday".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("chrono::Weekday")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down Expand Up @@ -41,6 +46,10 @@ macro_rules! formatted_string_impl {
stringify!($ty).to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed(stringify!(chrono::$ty))
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/decimal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;

macro_rules! decimal_impl {
($type:ty) => {
Expand All @@ -14,6 +15,10 @@ macro_rules! decimal_impl {
$name.to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed($name)
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::$instance_type.into()),
Expand Down
6 changes: 5 additions & 1 deletion schemars/src/json_schema_impls/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ impl<L: JsonSchema, R: JsonSchema> JsonSchema for Either<L, R> {
}

fn schema_id() -> Cow<'static, str> {
Cow::Owned(format!("Either<{}, {}>", L::schema_id(), R::schema_id()))
Cow::Owned(format!(
"either::Either<{}, {}>",
L::schema_id(),
R::schema_id()
))
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use std::ffi::{CStr, CString, OsStr, OsString};

impl JsonSchema for OsString {
fn schema_name() -> String {
"OsString".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("std::ffi::OsString")
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut unix_schema = SchemaObject {
instance_type: Some(InstanceType::Object.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/nonzero_signed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use std::num::*;

macro_rules! nonzero_unsigned_impl {
Expand All @@ -12,6 +13,10 @@ macro_rules! nonzero_unsigned_impl {
stringify!($type).to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed(stringify!(std::num::$type))
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let zero_schema: Schema = SchemaObject {
const_value: Some(0.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/nonzero_unsigned.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use std::num::*;

macro_rules! nonzero_unsigned_impl {
Expand All @@ -12,6 +13,10 @@ macro_rules! nonzero_unsigned_impl {
stringify!($type).to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed(stringify!(std::num::$type))
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <$primitive>::json_schema(gen).into();
schema.number().minimum = Some(1.0);
Expand Down
13 changes: 13 additions & 0 deletions schemars/src/json_schema_impls/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::path::{Path, PathBuf};

Expand All @@ -19,6 +20,10 @@ macro_rules! simple_impl {
$name.to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed($name)
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::$instance_type.into()),
Expand Down Expand Up @@ -64,6 +69,10 @@ macro_rules! unsigned_impl {
$format.to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed($format)
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
let mut schema = SchemaObject {
instance_type: Some(InstanceType::$instance_type.into()),
Expand Down Expand Up @@ -91,6 +100,10 @@ impl JsonSchema for char {
"Character".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("char")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use semver::Version;
use std::borrow::Cow;

impl JsonSchema for Version {
no_ref_schema!();
Expand All @@ -10,6 +11,10 @@ impl JsonSchema for Version {
"Version".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("semver::Version")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down
9 changes: 9 additions & 0 deletions schemars/src/json_schema_impls/serdejson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use serde_json::{Map, Number, Value};
use std::borrow::Cow;
use std::collections::BTreeMap;

impl JsonSchema for Value {
Expand All @@ -11,6 +12,10 @@ impl JsonSchema for Value {
"AnyValue".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("AnyValue")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
Schema::Bool(true)
}
Expand All @@ -25,6 +30,10 @@ impl JsonSchema for Number {
"Number".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("Number")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Number.into()),
Expand Down
9 changes: 9 additions & 0 deletions schemars/src/json_schema_impls/time.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use std::time::{Duration, SystemTime};

impl JsonSchema for Duration {
fn schema_name() -> String {
"Duration".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("std::time::Duration")
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema = SchemaObject {
instance_type: Some(InstanceType::Object.into()),
Expand All @@ -29,6 +34,10 @@ impl JsonSchema for SystemTime {
"SystemTime".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("std::time::SystemTime")
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema = SchemaObject {
instance_type: Some(InstanceType::Object.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/url.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use url::Url;

impl JsonSchema for Url {
Expand All @@ -10,6 +11,10 @@ impl JsonSchema for Url {
"Url".to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("url::Url")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/uuid08.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use uuid08::Uuid;

impl JsonSchema for Uuid {
Expand All @@ -10,6 +11,10 @@ impl JsonSchema for Uuid {
"Uuid".to_string()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("uuid::Uuid")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down
5 changes: 5 additions & 0 deletions schemars/src/json_schema_impls/uuid1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use uuid1::Uuid;

impl JsonSchema for Uuid {
Expand All @@ -10,6 +11,10 @@ impl JsonSchema for Uuid {
"Uuid".to_string()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("uuid::Uuid")
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
Expand Down
9 changes: 9 additions & 0 deletions schemars_derive/src/schema_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ fn type_for_schema(with_attr: &WithAttr) -> (syn::Type, Option<TokenStream>) {
#fn_name.to_string()
}

fn schema_id() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(std::concat!(
"_SchemarsSchemaWithFunction/"
std::module_path!(),
"/",
#fn_name
))
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
#fun(gen)
}
Expand Down

0 comments on commit 12ea862

Please sign in to comment.