-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe23eae
commit 29a4175
Showing
10 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
mod array_impls; | ||
mod box_impls; | ||
mod number_impls; | ||
mod string_impls; | ||
|
||
pub enum SchemaKind { | ||
Null, | ||
Map, | ||
List, | ||
String, | ||
Num, | ||
Int, | ||
Bool, | ||
} | ||
|
||
pub struct Schema<T> { | ||
pub min_value: Option<T>, | ||
pub max_value: Option<T>, | ||
pub description: Option<String>, | ||
pub example: Option<T>, | ||
pub name: String, | ||
pub kind: SchemaKind, | ||
|
||
} | ||
|
||
pub trait HasSchema: Sized { | ||
fn schema() -> Schema<Self>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
|
||
impl<T: super::HasSchema, const N: usize> super::HasSchema for [T; N] { | ||
fn schema() -> super::Schema<Self> { | ||
let base_schema = T::schema(); | ||
super::Schema { | ||
min_value: None, | ||
max_value: None, | ||
description: None, | ||
example: None, // making an array example requires that T be Copy... | ||
name: format!("Array of {} {}'s", N, base_schema.name), | ||
kind: super::SchemaKind::List, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
|
||
impl<T: super::HasSchema> super::HasSchema for Box<T> { | ||
fn schema() -> super::Schema<Self> { | ||
let base_schema = T::schema(); | ||
super::Schema { | ||
min_value: base_schema.min_value.map(Box::new), | ||
max_value: base_schema.max_value.map(Box::new), | ||
description: base_schema.description, | ||
example: base_schema.example.map(Box::new), | ||
name: base_schema.name, | ||
kind: base_schema.kind, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
impl super::HasSchema for i8 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(i8::MIN), | ||
max_value: Some(i8::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "signed 8-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for i16 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(i16::MIN), | ||
max_value: Some(i16::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "signed 16-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for i32 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(i32::MIN), | ||
max_value: Some(i32::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "signed 32-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for i64 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(i64::MIN), | ||
max_value: Some(i64::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "signed 64-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for i128 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(i128::MIN), | ||
max_value: Some(i128::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "signed 128-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for u8 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(u8::MIN), | ||
max_value: Some(u8::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "unsigned 8-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for u16 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(u16::MIN), | ||
max_value: Some(u16::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "unsigned 16-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for u32 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(u32::MIN), | ||
max_value: Some(u32::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "unsigned 32-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for u64 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(u64::MIN), | ||
max_value: Some(u64::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "unsigned 64-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} | ||
|
||
impl super::HasSchema for u128 { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: Some(u128::MIN), | ||
max_value: Some(u128::MAX), | ||
description: None, | ||
example: Some(1), | ||
name: "unsigned 128-bits integer".to_string(), | ||
kind: super::SchemaKind::Int, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
|
||
impl<'a> super::HasSchema for &'a str { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: None, | ||
max_value: None, | ||
description: None, | ||
example: Some("string"), | ||
name: "signed 8-bits integer".to_string(), | ||
kind: super::SchemaKind::String, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> super::HasSchema for String { | ||
fn schema() -> super::Schema<Self> { | ||
super::Schema { | ||
min_value: None, | ||
max_value: None, | ||
description: None, | ||
example: Some("string".to_string()), | ||
name: "signed 8-bits integer".to_string(), | ||
kind: super::SchemaKind::String, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Traits and structs related to automagically generating documentation for your Rocket routes | ||
|
||
use std::{collections::HashMap, marker::PhantomData}; | ||
|
||
use rocket_http::ContentType; | ||
|
||
mod has_schema; | ||
|
||
#[derive(Default)] | ||
pub struct Docs(HashMap<ContentType, DocContent>); | ||
|
||
#[derive(Default)] | ||
pub struct DocContent { | ||
title: Option<String>, | ||
description: Option<String>, | ||
content_type: Option<String>, | ||
} | ||
|
||
pub struct Resolve<T: ?Sized>(PhantomData<T>); | ||
|
||
pub trait Documented { | ||
fn docs() -> Docs; | ||
} | ||
|
||
trait Undocumented { | ||
fn docs() -> Docs { | ||
Docs::default() | ||
} | ||
} | ||
|
||
impl<T: ?Sized> Undocumented for T { } | ||
|
||
impl<T: Documented + ?Sized> Resolve<T> { | ||
pub const DOCUMENTED: bool = true; | ||
|
||
pub fn docs() -> Docs { | ||
T::docs() | ||
} | ||
} | ||
|
||
// impl<T: Documented + ?Sized> Documented for Json<T> { | ||
// fn docs() -> Docs { | ||
// Docs { | ||
// content_type: Some("application/json".to_string()), | ||
// ..Self::docs() | ||
// } | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters