Make schema_name()
return Cow<'static, str>
instead of String
This commit is contained in:
parent
fe05631f21
commit
1aaa162e0b
25 changed files with 126 additions and 157 deletions
|
@ -12,13 +12,11 @@ permalink: /implementing/
|
|||
## schema_name
|
||||
|
||||
```rust
|
||||
fn schema_name() -> String;
|
||||
fn schema_name() -> Cow<'static, str>;
|
||||
```
|
||||
|
||||
This function returns the human-readable friendly name of the type's schema, which frequently is just the name of the type itself. The schema name is used as the title for root schemas, and the key within the root's `definitions` property for subschemas.
|
||||
|
||||
NB in a future version of schemars, it's likely that this function will be changed to return a `Cow<'static, str>`.
|
||||
|
||||
## schema_id
|
||||
|
||||
```rust
|
||||
|
@ -29,8 +27,7 @@ This function returns a unique identifier of the type's schema - if two types re
|
|||
|
||||
```rust
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(
|
||||
format!("[{}]", T::schema_id()))
|
||||
format!("[{}]", T::schema_id()).into()
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -40,14 +37,14 @@ For a type with no generic type arguments, a reasonable implementation of this f
|
|||
|
||||
```rust
|
||||
impl JsonSchema for NonGenericType {
|
||||
fn schema_name() -> String {
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
// Exclude the module path to make the name in generated schemas clearer.
|
||||
"NonGenericType".to_owned()
|
||||
"NonGenericType".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
// Include the module, in case a type with the same name is in another module/crate
|
||||
Cow::Borrowed(concat!(module_path!(), "::NonGenericType"))
|
||||
concat!(module_path!(), "::NonGenericType").into()
|
||||
}
|
||||
|
||||
fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -12,10 +12,11 @@ use crate::{visit::*, JsonSchema};
|
|||
use dyn_clone::DynClone;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::{any::Any, collections::HashSet, fmt::Debug};
|
||||
|
||||
type CowStr = std::borrow::Cow<'static, str>;
|
||||
|
||||
/// Settings to customize how Schemas are generated.
|
||||
///
|
||||
/// 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.
|
||||
|
@ -166,9 +167,9 @@ impl SchemaSettings {
|
|||
pub struct SchemaGenerator {
|
||||
settings: SchemaSettings,
|
||||
definitions: Map<String, Value>,
|
||||
pending_schema_ids: HashSet<Cow<'static, str>>,
|
||||
schema_id_to_name: HashMap<Cow<'static, str>, String>,
|
||||
used_schema_names: HashSet<String>,
|
||||
pending_schema_ids: HashSet<CowStr>,
|
||||
schema_id_to_name: HashMap<CowStr, CowStr>,
|
||||
used_schema_names: HashSet<CowStr>,
|
||||
}
|
||||
|
||||
impl Clone for SchemaGenerator {
|
||||
|
@ -230,11 +231,11 @@ impl SchemaGenerator {
|
|||
Some(n) => n,
|
||||
None => {
|
||||
let base_name = T::schema_name();
|
||||
let mut name = String::new();
|
||||
let mut name = CowStr::Borrowed("");
|
||||
|
||||
if self.used_schema_names.contains(&base_name) {
|
||||
if self.used_schema_names.contains(base_name.as_ref()) {
|
||||
for i in 2.. {
|
||||
name = format!("{}{}", base_name, i);
|
||||
name = format!("{}{}", base_name, i).into();
|
||||
if !self.used_schema_names.contains(&name) {
|
||||
break;
|
||||
}
|
||||
|
@ -250,7 +251,7 @@ impl SchemaGenerator {
|
|||
};
|
||||
|
||||
let reference = format!("#{}/{}", self.definitions_path_stripped(), name);
|
||||
if !self.definitions.contains_key(&name) {
|
||||
if !self.definitions.contains_key(name.as_ref()) {
|
||||
self.insert_new_subschema_for::<T>(name, id);
|
||||
}
|
||||
Schema::new_ref(reference)
|
||||
|
@ -259,18 +260,14 @@ impl SchemaGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
fn insert_new_subschema_for<T: ?Sized + JsonSchema>(
|
||||
&mut self,
|
||||
name: String,
|
||||
id: Cow<'static, str>,
|
||||
) {
|
||||
fn insert_new_subschema_for<T: ?Sized + JsonSchema>(&mut self, name: CowStr, id: CowStr) {
|
||||
let dummy = false.into();
|
||||
// insert into definitions BEFORE calling json_schema to avoid infinite recursion
|
||||
self.definitions.insert(name.clone(), dummy);
|
||||
self.definitions.insert(name.clone().into(), dummy);
|
||||
|
||||
let schema = self.json_schema_internal::<T>(id);
|
||||
|
||||
self.definitions.insert(name, schema.to_value());
|
||||
self.definitions.insert(name.into(), schema.to_value());
|
||||
}
|
||||
|
||||
/// Borrows the collection of all [referenceable](JsonSchema::is_referenceable) schemas that have been generated.
|
||||
|
@ -410,15 +407,15 @@ impl SchemaGenerator {
|
|||
Ok(schema)
|
||||
}
|
||||
|
||||
fn json_schema_internal<T: ?Sized + JsonSchema>(&mut self, id: Cow<'static, str>) -> Schema {
|
||||
fn json_schema_internal<T: ?Sized + JsonSchema>(&mut self, id: CowStr) -> Schema {
|
||||
struct PendingSchemaState<'a> {
|
||||
gen: &'a mut SchemaGenerator,
|
||||
id: Cow<'static, str>,
|
||||
id: CowStr,
|
||||
did_add: bool,
|
||||
}
|
||||
|
||||
impl<'a> PendingSchemaState<'a> {
|
||||
fn new(gen: &'a mut SchemaGenerator, id: Cow<'static, str>) -> Self {
|
||||
fn new(gen: &'a mut SchemaGenerator, id: CowStr) -> Self {
|
||||
let did_add = gen.pending_schema_ids.insert(id.clone());
|
||||
Self { gen, id, did_add }
|
||||
}
|
||||
|
@ -477,6 +474,8 @@ impl SchemaGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns `self.settings.definitions_path` as a plain JSON pointer to the definitions object,
|
||||
/// i.e. without a leading '#' or trailing '/'
|
||||
fn definitions_path_stripped(&self) -> &str {
|
||||
let path = &self.settings.definitions_path;
|
||||
let path = path.strip_prefix('#').unwrap_or(path);
|
||||
|
|
|
@ -6,12 +6,12 @@ use std::borrow::Cow;
|
|||
impl<T> JsonSchema for [T; 0] {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"EmptyArray".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"EmptyArray".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("[]")
|
||||
"[]".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -28,13 +28,12 @@ macro_rules! array_impls {
|
|||
impl<T: JsonSchema> JsonSchema for [T; $len] {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Array_size_{}_of_{}", $len, T::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Array_size_{}_of_{}", $len, T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(
|
||||
format!("[{}; {}]", $len, T::schema_id()))
|
||||
format!("[{}; {}]", $len, T::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -12,8 +12,8 @@ where
|
|||
{
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name())
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -6,12 +6,12 @@ use std::borrow::Cow;
|
|||
impl JsonSchema for Weekday {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Weekday".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Weekday".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("chrono::Weekday")
|
||||
"chrono::Weekday".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -38,12 +38,12 @@ macro_rules! formatted_string_impl {
|
|||
impl $($desc)+ {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
stringify!($ty).to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
stringify!($ty).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed(stringify!(chrono::$ty))
|
||||
stringify!(chrono::$ty).into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -7,12 +7,12 @@ use std::ops::{Bound, Range, RangeInclusive};
|
|||
impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Nullable_{}", T::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Nullable_{}", T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("Option<{}>", T::schema_id()))
|
||||
format!("Option<{}>", T::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -69,12 +69,12 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
|
|||
}
|
||||
|
||||
impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
|
||||
fn schema_name() -> String {
|
||||
format!("Result_of_{}_or_{}", T::schema_name(), E::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Result_of_{}_or_{}", T::schema_name(), E::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("Result<{}, {}>", T::schema_id(), E::schema_id()))
|
||||
format!("Result<{}, {}>", T::schema_id(), E::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -100,12 +100,12 @@ impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
|
|||
}
|
||||
|
||||
impl<T: JsonSchema> JsonSchema for Bound<T> {
|
||||
fn schema_name() -> String {
|
||||
format!("Bound_of_{}", T::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Bound_of_{}", T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("Bound<{}>", T::schema_id()))
|
||||
format!("Bound<{}>", T::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -135,12 +135,12 @@ impl<T: JsonSchema> JsonSchema for Bound<T> {
|
|||
}
|
||||
|
||||
impl<T: JsonSchema> JsonSchema for Range<T> {
|
||||
fn schema_name() -> String {
|
||||
format!("Range_of_{}", T::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Range_of_{}", T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("Range<{}>", T::schema_id()))
|
||||
format!("Range<{}>", T::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -7,12 +7,8 @@ macro_rules! decimal_impl {
|
|||
impl JsonSchema for $type {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Decimal".to_owned()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("Decimal")
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Decimal".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -6,16 +6,12 @@ use std::borrow::Cow;
|
|||
impl<L: JsonSchema, R: JsonSchema> JsonSchema for Either<L, R> {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Either_{}_or_{}", L::schema_name(), R::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Either_{}_or_{}", L::schema_name(), R::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(format!(
|
||||
"either::Either<{}, {}>",
|
||||
L::schema_id(),
|
||||
R::schema_id()
|
||||
))
|
||||
format!("either::Either<{}, {}>", L::schema_id(), R::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -4,12 +4,12 @@ use std::borrow::Cow;
|
|||
use std::ffi::{CStr, CString, OsStr, OsString};
|
||||
|
||||
impl JsonSchema for OsString {
|
||||
fn schema_name() -> String {
|
||||
"OsString".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"OsString".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("std::ffi::OsString")
|
||||
"std::ffi::OsString".into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -10,12 +10,12 @@ macro_rules! map_impl {
|
|||
{
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Map_of_{}", V::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Map_of_{}", V::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("Map<{}>", V::schema_id()))
|
||||
format!("Map<{}>", V::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -13,7 +13,7 @@ macro_rules! forward_impl {
|
|||
<$target>::is_referenceable()
|
||||
}
|
||||
|
||||
fn schema_name() -> String {
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
<$target>::schema_name()
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,12 @@ macro_rules! nonzero_unsigned_impl {
|
|||
impl JsonSchema for $type {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
stringify!($type).to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
stringify!($type).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed(stringify!(std::num::$type))
|
||||
stringify!(std::num::$type).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::gen::SchemaGenerator;
|
||||
use crate::Schema;
|
||||
use crate::JsonSchema;
|
||||
use crate::Schema;
|
||||
use std::borrow::Cow;
|
||||
use std::num::*;
|
||||
|
||||
|
@ -9,12 +9,12 @@ macro_rules! nonzero_unsigned_impl {
|
|||
impl JsonSchema for $type {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
stringify!($type).to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
stringify!($type).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed(stringify!(std::num::$type))
|
||||
stringify!(std::num::$type).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -9,12 +9,8 @@ macro_rules! simple_impl {
|
|||
impl JsonSchema for $type {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
$instance_type.to_owned()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed($instance_type)
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
$instance_type.into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -28,12 +24,8 @@ macro_rules! simple_impl {
|
|||
impl JsonSchema for $type {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
$format.to_owned()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed($format)
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
$format.into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -75,12 +67,8 @@ macro_rules! unsigned_impl {
|
|||
impl JsonSchema for $type {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
$format.to_owned()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed($format)
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
$format.into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -104,12 +92,12 @@ unsigned_impl!(usize => "integer", "uint");
|
|||
impl JsonSchema for char {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Character".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Character".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("char")
|
||||
"char".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -6,12 +6,12 @@ use std::borrow::Cow;
|
|||
impl JsonSchema for Version {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Version".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Version".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("semver::Version")
|
||||
"semver::Version".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -10,13 +10,12 @@ macro_rules! seq_impl {
|
|||
{
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Array_of_{}", T::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Array_of_{}", T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(
|
||||
format!("[{}]", T::schema_id()))
|
||||
format!("[{}]", T::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -37,13 +36,12 @@ macro_rules! set_impl {
|
|||
{
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
format!("Set_of_{}", T::schema_name())
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
format!("Set_of_{}", T::schema_name()).into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(
|
||||
format!("Set<{}>", T::schema_id()))
|
||||
format!("Set<{}>", T::schema_id()).into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -7,12 +7,8 @@ use std::collections::BTreeMap;
|
|||
impl JsonSchema for Value {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"AnyValue".to_owned()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("AnyValue")
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"AnyValue".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -25,12 +21,8 @@ forward_impl!(Map<String, Value> => BTreeMap<String, Value>);
|
|||
impl JsonSchema for Number {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Number".to_owned()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("Number")
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Number".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -4,12 +4,12 @@ use std::borrow::Cow;
|
|||
use std::time::{Duration, SystemTime};
|
||||
|
||||
impl JsonSchema for Duration {
|
||||
fn schema_name() -> String {
|
||||
"Duration".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Duration".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("std::time::Duration")
|
||||
"std::time::Duration".into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -25,12 +25,12 @@ impl JsonSchema for Duration {
|
|||
}
|
||||
|
||||
impl JsonSchema for SystemTime {
|
||||
fn schema_name() -> String {
|
||||
"SystemTime".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"SystemTime".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("std::time::SystemTime")
|
||||
"std::time::SystemTime".into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -8,10 +8,10 @@ macro_rules! tuple_impls {
|
|||
impl<$($name: JsonSchema),+> JsonSchema for ($($name,)+) {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
let mut name = "Tuple_of_".to_owned();
|
||||
name.push_str(&[$($name::schema_name()),+].join("_and_"));
|
||||
name
|
||||
name.into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
|
@ -19,7 +19,7 @@ macro_rules! tuple_impls {
|
|||
id.push_str(&[$($name::schema_id()),+].join(","));
|
||||
id.push(')');
|
||||
|
||||
Cow::Owned(id)
|
||||
id.into()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -6,12 +6,12 @@ use url2::Url;
|
|||
impl JsonSchema for Url {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Url".to_owned()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Url".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("url::Url")
|
||||
"url::Url".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -6,12 +6,12 @@ use uuid1::Uuid;
|
|||
impl JsonSchema for Uuid {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
"Uuid".to_string()
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"Uuid".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Borrowed("uuid::Uuid")
|
||||
"uuid::Uuid".into()
|
||||
}
|
||||
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
|
|
|
@ -56,14 +56,14 @@ pub use schema::Schema;
|
|||
/// struct NonGenericType;
|
||||
///
|
||||
/// impl JsonSchema for NonGenericType {
|
||||
/// fn schema_name() -> String {
|
||||
/// fn schema_name() -> Cow<'static, str> {
|
||||
/// // Exclude the module path to make the name in generated schemas clearer.
|
||||
/// "NonGenericType".to_owned()
|
||||
/// "NonGenericType".into()
|
||||
/// }
|
||||
///
|
||||
/// fn schema_id() -> Cow<'static, str> {
|
||||
/// // Include the module, in case a type with the same name is in another module/crate
|
||||
/// Cow::Borrowed(concat!(module_path!(), "::NonGenericType"))
|
||||
/// concat!(module_path!(), "::NonGenericType").into()
|
||||
/// }
|
||||
///
|
||||
/// fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -82,16 +82,16 @@ pub use schema::Schema;
|
|||
/// struct GenericType<T>(PhantomData<T>);
|
||||
///
|
||||
/// impl<T: JsonSchema> JsonSchema for GenericType<T> {
|
||||
/// fn schema_name() -> String {
|
||||
/// format!("GenericType_{}", T::schema_name())
|
||||
/// fn schema_name() -> Cow<'static, str> {
|
||||
/// format!("GenericType_{}", T::schema_name()).into()
|
||||
/// }
|
||||
///
|
||||
/// fn schema_id() -> Cow<'static, str> {
|
||||
/// Cow::Owned(format!(
|
||||
/// format!(
|
||||
/// "{}::GenericType<{}>",
|
||||
/// module_path!(),
|
||||
/// T::schema_id()
|
||||
/// ))
|
||||
/// ).into()
|
||||
/// }
|
||||
///
|
||||
/// fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
|
||||
|
@ -117,7 +117,7 @@ pub trait JsonSchema {
|
|||
/// The name of the generated JSON Schema.
|
||||
///
|
||||
/// This is used as the title for root schemas, and the key within the root's `definitions` property for subschemas.
|
||||
fn schema_name() -> String;
|
||||
fn schema_name() -> Cow<'static, str>;
|
||||
|
||||
/// Returns a string that uniquely identifies the schema produced by this type.
|
||||
///
|
||||
|
@ -127,7 +127,7 @@ pub trait JsonSchema {
|
|||
///
|
||||
/// The default implementation returns the same value as `schema_name()`.
|
||||
fn schema_id() -> Cow<'static, str> {
|
||||
Cow::Owned(Self::schema_name())
|
||||
Self::schema_name()
|
||||
}
|
||||
|
||||
/// Generates a JSON Schema for this type.
|
||||
|
|
|
@ -175,8 +175,8 @@ impl From<bool> for Schema {
|
|||
}
|
||||
|
||||
impl crate::JsonSchema for Schema {
|
||||
fn schema_name() -> String {
|
||||
"Schema".to_owned()
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
"Schema".into()
|
||||
}
|
||||
|
||||
fn schema_id() -> std::borrow::Cow<'static, str> {
|
||||
|
|
|
@ -60,7 +60,7 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
|
|||
<#ty as schemars::JsonSchema>::is_referenceable()
|
||||
}
|
||||
|
||||
fn schema_name() -> std::string::String {
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
<#ty as schemars::JsonSchema>::schema_name()
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
|
|||
{
|
||||
(
|
||||
quote! {
|
||||
#schema_base_name.to_owned()
|
||||
std::borrow::Cow::Borrowed(#schema_base_name)
|
||||
},
|
||||
quote! {
|
||||
std::borrow::Cow::Borrowed(std::concat!(
|
||||
|
@ -121,7 +121,9 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
|
|||
}
|
||||
(
|
||||
quote! {
|
||||
format!(#schema_name_fmt #(,#type_params=#type_params::schema_name())* #(,#const_params=#const_params)*)
|
||||
std::borrow::Cow::Owned(
|
||||
format!(#schema_name_fmt #(,#type_params=#type_params::schema_name())* #(,#const_params=#const_params)*)
|
||||
)
|
||||
},
|
||||
quote! {
|
||||
std::borrow::Cow::Owned(
|
||||
|
@ -143,7 +145,9 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
|
|||
schema_name_fmt.push_str(&"_and_{}".repeat(params.len() - 1));
|
||||
(
|
||||
quote! {
|
||||
format!(#schema_name_fmt #(,#type_params::schema_name())* #(,#const_params)*)
|
||||
std::borrow::Cow::Owned(
|
||||
format!(#schema_name_fmt #(,#type_params::schema_name())* #(,#const_params)*)
|
||||
)
|
||||
},
|
||||
quote! {
|
||||
std::borrow::Cow::Owned(
|
||||
|
@ -174,7 +178,7 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
|
|||
#[automatically_derived]
|
||||
#[allow(unused_braces)]
|
||||
impl #impl_generics schemars::JsonSchema for #type_name #ty_generics #where_clause {
|
||||
fn schema_name() -> std::string::String {
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
#schema_name
|
||||
}
|
||||
|
||||
|
|
|
@ -114,8 +114,8 @@ fn type_for_schema(with_attr: &WithAttr) -> (syn::Type, Option<TokenStream>) {
|
|||
false
|
||||
}
|
||||
|
||||
fn schema_name() -> std::string::String {
|
||||
#fn_name.to_string()
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
std::borrow::Cow::Borrowed(#fn_name)
|
||||
}
|
||||
|
||||
fn schema_id() -> std::borrow::Cow<'static, str> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue