Renames
This commit is contained in:
		
							parent
							
								
									21a29dc6b5
								
							
						
					
					
						commit
						354c3aa98f
					
				
					 3 changed files with 35 additions and 19 deletions
				
			
		|  | @ -6,7 +6,6 @@ use make_schema::MakeSchema; | ||||||
| use schema::*; | use schema::*; | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| use serde_json::Result; | use serde_json::Result; | ||||||
| use std::collections::BTreeMap as Map; |  | ||||||
| 
 | 
 | ||||||
| #[derive(Serialize, Deserialize, Debug)] | #[derive(Serialize, Deserialize, Debug)] | ||||||
| #[serde(rename_all = "camelCase")] | #[serde(rename_all = "camelCase")] | ||||||
|  | @ -34,10 +33,27 @@ struct User { | ||||||
|     username: String, |     username: String, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | impl MakeSchema for User { | ||||||
|  |     fn generates_ref_schema() -> bool { | ||||||
|  |         true | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fn make_schema(gen: &mut generator::SchemaGenerator) -> Schema { | ||||||
|  |         let mut o = SchemaObject { | ||||||
|  |             ..Default::default() | ||||||
|  |         }; | ||||||
|  |         o.properties | ||||||
|  |             .insert("id".to_owned(), gen.subschema_for::<u64>()); | ||||||
|  |         o.properties | ||||||
|  |             .insert("username".to_owned(), gen.subschema_for::<String>()); | ||||||
|  |         o.into() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| fn main() -> Result<()> { | fn main() -> Result<()> { | ||||||
|     let gen = generator::SchemaGenerator::new(); |     let gen = generator::SchemaGenerator::new(); | ||||||
|     let schema = gen.into_root_schema_for::<str>(); |     let schema = gen.into_root_schema_for::<User>(); | ||||||
|     let json = serde_json::to_string(&schema)?; |     let json = serde_json::to_string_pretty(&schema)?; | ||||||
|     println!("{}", json); |     println!("{}", json); | ||||||
| 
 | 
 | ||||||
|     /*let todo = Todo {
 |     /*let todo = Todo {
 | ||||||
|  |  | ||||||
|  | @ -26,7 +26,7 @@ pub trait MakeSchema { | ||||||
|         false |         false | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn make_schema(generator: &mut SchemaGenerator) -> Schema; |     fn make_schema(gen: &mut SchemaGenerator) -> Schema; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // TODO structs, enums, tuples
 | // TODO structs, enums, tuples
 | ||||||
|  | @ -76,12 +76,12 @@ simple_impl!(() => Null); | ||||||
| 
 | 
 | ||||||
| impl MakeSchema for char { | impl MakeSchema for char { | ||||||
|     fn make_schema(_: &mut SchemaGenerator) -> Schema { |     fn make_schema(_: &mut SchemaGenerator) -> Schema { | ||||||
|         let mut extra_properties = Map::new(); |         let mut extensions = Map::new(); | ||||||
|         extra_properties.insert("minLength".to_owned(), json!(1)); |         extensions.insert("minLength".to_owned(), json!(1)); | ||||||
|         extra_properties.insert("maxLength".to_owned(), json!(1)); |         extensions.insert("maxLength".to_owned(), json!(1)); | ||||||
|         SchemaObject { |         SchemaObject { | ||||||
|             instance_type: Some(InstanceType::String.into()), |             instance_type: Some(InstanceType::String.into()), | ||||||
|             extra_properties, |             extensions, | ||||||
|             ..Default::default() |             ..Default::default() | ||||||
|         } |         } | ||||||
|         .into() |         .into() | ||||||
|  | @ -93,11 +93,11 @@ impl MakeSchema for char { | ||||||
| // Does not require T: MakeSchema.
 | // Does not require T: MakeSchema.
 | ||||||
| impl<T> MakeSchema for [T; 0] { | impl<T> MakeSchema for [T; 0] { | ||||||
|     fn make_schema(_: &mut SchemaGenerator) -> Schema { |     fn make_schema(_: &mut SchemaGenerator) -> Schema { | ||||||
|         let mut extra_properties = Map::new(); |         let mut extensions = Map::new(); | ||||||
|         extra_properties.insert("maxItems".to_owned(), json!(0)); |         extensions.insert("maxItems".to_owned(), json!(0)); | ||||||
|         SchemaObject { |         SchemaObject { | ||||||
|             instance_type: Some(InstanceType::Array.into()), |             instance_type: Some(InstanceType::Array.into()), | ||||||
|             extra_properties, |             extensions, | ||||||
|             ..Default::default() |             ..Default::default() | ||||||
|         } |         } | ||||||
|         .into() |         .into() | ||||||
|  | @ -110,13 +110,13 @@ macro_rules! array_impls { | ||||||
|             impl<T: MakeSchema> MakeSchema for [T; $len] |             impl<T: MakeSchema> MakeSchema for [T; $len] | ||||||
|             { |             { | ||||||
|                 fn make_schema(gen: &mut SchemaGenerator) -> Schema { |                 fn make_schema(gen: &mut SchemaGenerator) -> Schema { | ||||||
|                     let mut extra_properties = Map::new(); |                     let mut extensions = Map::new(); | ||||||
|                     extra_properties.insert("minItems".to_owned(), json!($len)); |                     extensions.insert("minItems".to_owned(), json!($len)); | ||||||
|                     extra_properties.insert("maxItems".to_owned(), json!($len)); |                     extensions.insert("maxItems".to_owned(), json!($len)); | ||||||
|                     SchemaObject { |                     SchemaObject { | ||||||
|                         instance_type: Some(InstanceType::Array.into()), |                         instance_type: Some(InstanceType::Array.into()), | ||||||
|                         items: Some(Box::from(gen.subschema_for::<T>())), |                         items: Some(Box::from(gen.subschema_for::<T>())), | ||||||
|                         extra_properties, |                         extensions, | ||||||
|                         ..Default::default() |                         ..Default::default() | ||||||
|                     }.into() |                     }.into() | ||||||
|                 } |                 } | ||||||
|  | @ -170,14 +170,14 @@ macro_rules! map_impl { | ||||||
|         { |         { | ||||||
|             fn make_schema(gen: &mut SchemaGenerator) -> Schema |             fn make_schema(gen: &mut SchemaGenerator) -> Schema | ||||||
|             { |             { | ||||||
|                 let mut extra_properties = Map::new(); |                 let mut extensions = Map::new(); | ||||||
|                 extra_properties.insert( |                 extensions.insert( | ||||||
|                     "additionalProperties".to_owned(), |                     "additionalProperties".to_owned(), | ||||||
|                     json!(gen.subschema_for::<T>()) |                     json!(gen.subschema_for::<T>()) | ||||||
|                 ); |                 ); | ||||||
|                 SchemaObject { |                 SchemaObject { | ||||||
|                     instance_type: Some(InstanceType::Object.into()), |                     instance_type: Some(InstanceType::Object.into()), | ||||||
|                     extra_properties, |                     extensions, | ||||||
|                     ..Default::default() |                     ..Default::default() | ||||||
|                 }.into() |                 }.into() | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  | @ -66,7 +66,7 @@ pub struct SchemaObject { | ||||||
|     #[serde(skip_serializing_if = "Map::is_empty")] |     #[serde(skip_serializing_if = "Map::is_empty")] | ||||||
|     pub definitions: Map<String, Schema>, |     pub definitions: Map<String, Schema>, | ||||||
|     #[serde(flatten)] |     #[serde(flatten)] | ||||||
|     pub extra_properties: Map<String, Value>, |     pub extensions: Map<String, Value>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Graham Esau
						Graham Esau