Skip to content

Commit

Permalink
utoipa openapi base features
Browse files Browse the repository at this point in the history
  • Loading branch information
juhaku committed Oct 29, 2021
0 parents commit b5a538e
Show file tree
Hide file tree
Showing 26 changed files with 1,694 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
Cargo.lock
*.iml
.idea
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "utoipa"
version = "0.1.0"
edition = "2021"

[workspace]
members = [
"utoipa-gen"
]

[features]
swagger_ui = ["rust-embed", "mime_guess"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.130", features = ["derive"] }
log = "0.4.14"
serde_json = "1.0.68"
rust-embed = { version = "6.2.0", optional = true }
actix-web = { version = "3.3.2", optional = true }
mime_guess = { version = "2.0.3", optional = true }
utoipa-gen = { path = "./utoipa-gen" }
14 changes: 14 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::process::Command;

const SWAGGER_UI_DIST_ZIP: &str = "swagger-ui-3.52.5";

fn main() {
println!("cargo:rerun-if-changed={}.zip", SWAGGER_UI_DIST_ZIP);

Command::new("unzip")
.arg(&format!("{}.zip", SWAGGER_UI_DIST_ZIP))
.arg(&format!("{}/dist/**", SWAGGER_UI_DIST_ZIP))
.args(&["-d", "target"])
.status()
.unwrap();
}
34 changes: 34 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::fmt::Display;

#[derive(Debug)]
pub enum Error {
Serde(serde_json::Error),
Io(std::io::Error),
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Serde(error) => write!(f, "serde: {}", error),
Self::Io(error) => write!(f, "io: {}", error),
}
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

impl From<std::io::Error> for Error {
fn from(io: std::io::Error) -> Self {
Self::Io(io)
}
}

impl From<serde_json::Error> for Error {
fn from(serde: serde_json::Error) -> Self {
Self::Serde(serde)
}
}
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod error;
pub mod openapi;
#[cfg(feature = "swagger_ui")]
pub mod swagger_ui;
pub mod types;

pub use utoipa_gen::*;

pub trait OpenApi {
fn openapi() -> openapi::OpenApi;
}
39 changes: 39 additions & 0 deletions src/openapi/contact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use serde::{Deserialize, Serialize};

#[non_exhaustive]
#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Contact {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
}

impl Contact {
pub fn new() -> Self {
Default::default()
}

pub fn with_name<S: AsRef<str>>(mut self, name: S) -> Self {
self.name = Some(name.as_ref().to_string());

self
}

pub fn with_url<S: AsRef<str>>(mut self, url: S) -> Self {
self.url = Some(url.as_ref().to_string());

self
}

pub fn with_email<S: AsRef<str>>(mut self, email: S) -> Self {
self.email = Some(email.as_ref().to_string());

self
}
}
24 changes: 24 additions & 0 deletions src/openapi/external_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};

#[non_exhaustive]
#[derive(Serialize, Deserialize, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExternalDocs {
pub url: String,
pub description: Option<String>,
}

impl ExternalDocs {
pub fn new<S: AsRef<str>>(url: S) -> Self {
Self {
url: url.as_ref().to_string(),
..Default::default()
}
}

pub fn with_description<S: AsRef<str>>(mut self, description: S) -> Self {
self.description = Some(description.as_ref().to_string());

self
}
}
58 changes: 58 additions & 0 deletions src/openapi/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::{Deserialize, Serialize};

use super::{contact::Contact, licence::Licence};

#[non_exhaustive]
#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Info {
pub title: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub terms_of_service: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub contact: Option<Contact>,

#[serde(skip_serializing_if = "Option::is_none")]
pub licence: Option<Licence>,

pub version: String,
}

impl Info {
pub fn new<S: AsRef<str>>(title: S, version: S) -> Self {
Self {
title: title.as_ref().to_string(),
version: version.as_ref().to_string(),
..Default::default()
}
}

pub fn with_description<S: AsRef<str>>(mut self, description: S) -> Self {
self.description = Some(description.as_ref().to_string());

self
}

pub fn with_terms_of_service<S: AsRef<str>>(mut self, terms_of_service: S) -> Self {
self.terms_of_service = Some(terms_of_service.as_ref().to_string());

self
}

pub fn with_contact(mut self, contanct: Contact) -> Self {
self.contact = Some(contanct);

self
}

pub fn with_licence(mut self, licence: Licence) -> Self {
self.licence = Some(licence);

self
}
}
26 changes: 26 additions & 0 deletions src/openapi/licence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::{Deserialize, Serialize};

#[non_exhaustive]
#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Licence {
pub name: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}

impl Licence {
pub fn new<S: AsRef<str>>(name: S) -> Self {
Self {
name: name.as_ref().to_string(),
..Default::default()
}
}

pub fn with_url<S: AsRef<str>>(mut self, url: S) -> Self {
self.url = Some(url.as_ref().to_string());

self
}
}
Loading

0 comments on commit b5a538e

Please sign in to comment.