All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rust-server.lib.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]

use async_trait::async_trait;
use futures::Stream;
use std::error::Error;
use std::task::{Poll, Context};
use swagger::{ApiError, ContextWrapper};
use serde::{Serialize, Deserialize};

type ServiceError = Box;

pub const BASE_PATH: &'static str = "{{{basePathWithoutHost}}}";
{{#appVersion}}
pub const API_VERSION: &'static str = "{{{.}}}";
{{/appVersion}}

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
{{>response}}
      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
/// API
#[async_trait]
pub trait Api {
    fn poll_ready(&self, _cx: &mut Context) -> Poll>> {
        Poll::Ready(Ok(()))
    }

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
{{#summary}}
    /// {{{.}}}
{{/summary}}
    async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
        &self,
{{#allParams}}
        {{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
        context: &C) -> Result<{{{operationId}}}Response, ApiError>;

      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
}

/// API where `Context` isn't passed on every API call
#[async_trait]
pub trait ApiNoContext {

    fn poll_ready(&self, _cx: &mut Context) -> Poll>>;

    fn context(&self) -> &C;

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
{{#summary}}
    /// {{{.}}}
{{/summary}}
    async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
        &self,
{{#allParams}}
        {{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
        ) -> Result<{{{operationId}}}Response, ApiError>;

      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
}

/// Trait to extend an API to make it easy to bind it to a context.
pub trait ContextWrapperExt where Self: Sized
{
    /// Binds this API to a context.
    fn with_context(self: Self, context: C) -> ContextWrapper;
}

impl + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt for T {
    fn with_context(self: T, context: C) -> ContextWrapper {
         ContextWrapper::::new(self, context)
    }
}

#[async_trait]
impl + Send + Sync, C: Clone + Send + Sync> ApiNoContext for ContextWrapper {
    fn poll_ready(&self, cx: &mut Context) -> Poll> {
        self.api().poll_ready(cx)
    }

    fn context(&self) -> &C {
        ContextWrapper::context(self)
    }

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
{{#summary}}
    /// {{{.}}}
{{/summary}}
    async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
        &self,
{{#allParams}}
        {{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
        ) -> Result<{{{operationId}}}Response, ApiError>
    {
        let context = self.context().clone();
        self.api().{{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}({{#allParams}}{{{paramName}}}, {{/allParams}}&context).await
    }

      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
}

{{#hasCallbacks}}

{{#apiInfo}}
    {{#apis}}
      {{#operations}}
        {{#operation}}
          {{#callbacks}}
            {{#urls}}
              {{#requests}}
{{>response}}
            {{/requests}}
          {{/urls}}
        {{/callbacks}}
      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}

/// Callback API
#[async_trait]
pub trait CallbackApi {
    fn poll_ready(&self, _cx: &mut Context) -> Poll>> {
        Poll::Ready(Ok(()))
    }

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
        {{#callbacks}}
          {{#urls}}
            {{#requests}}
{{#summary}}
    /// {{{.}}}
{{/summary}}
    async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
        &self,
{{#vendorExtensions}}
  {{#x-callback-params}}
        callback_{{.}}: String,
  {{/x-callback-params}}
{{/vendorExtensions}}
{{#allParams}}
        {{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
        context: &C) -> Result<{{{operationId}}}Response, ApiError>;

            {{/requests}}
          {{/urls}}
        {{/callbacks}}
      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
}

/// Callback API without a `Context`
#[async_trait]
pub trait CallbackApiNoContext {
    fn poll_ready(&self, _cx: &mut Context) -> Poll>>;

    fn context(&self) -> &C;

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
        {{#callbacks}}
          {{#urls}}
            {{#requests}}
{{#summary}}
    /// {{{.}}}
{{/summary}}
    async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
        &self,
{{#vendorExtensions}}
  {{#x-callback-params}}
        callback_{{.}}: String,
  {{/x-callback-params}}
{{/vendorExtensions}}
{{#allParams}}
        {{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
        ) -> Result<{{{operationId}}}Response, ApiError>;

            {{/requests}}
          {{/urls}}
        {{/callbacks}}
      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
}

pub trait CallbackContextWrapperExt where Self: Sized
{
    /// Binds this API to a context.
    fn with_context(self: Self, context: C) -> ContextWrapper;
}

impl + Send + Sync, C: Clone + Send + Sync> CallbackContextWrapperExt for T {
    fn with_context(self: T, context: C) -> ContextWrapper {
         ContextWrapper::::new(self, context)
    }
}

#[async_trait]
impl + Send + Sync, C: Clone + Send + Sync> CallbackApiNoContext for ContextWrapper {
    fn poll_ready(&self, cx: &mut Context) -> Poll> {
        self.api().poll_ready(cx)
    }

    fn context(&self) -> &C {
        ContextWrapper::context(self)
    }

{{#apiInfo}}
  {{#apis}}
    {{#operations}}
      {{#operation}}
        {{#callbacks}}
          {{#urls}}
            {{#requests}}
              {{#summary}}
    /// {{{.}}}
              {{/summary}}
    async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
        &self,
{{#vendorExtensions}}
  {{#x-callback-params}}
        callback_{{.}}: String,
  {{/x-callback-params}}
{{/vendorExtensions}}
{{#allParams}}
        {{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
        ) -> Result<{{{operationId}}}Response, ApiError>
    {
        let context = self.context().clone();
        self.api().{{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
{{#vendorExtensions}}
  {{#x-callback-params}}
            callback_{{.}},
  {{/x-callback-params}}
{{/vendorExtensions}}
{{#allParams}}
            {{{paramName}}},
{{/allParams}}
            &context).await
    }

            {{/requests}}
          {{/urls}}
        {{/callbacks}}
      {{/operation}}
    {{/operations}}
  {{/apis}}
{{/apiInfo}}
}

{{/hasCallbacks}}

#[cfg(feature = "client")]
pub mod client;

// Re-export Client as a top-level name
#[cfg(feature = "client")]
pub use client::Client;

#[cfg(feature = "server")]
pub mod server;

// Re-export router() as a top-level name
#[cfg(feature = "server")]
pub use self::server::Service;

{{#hasCallbacks}}
#[cfg(any(feature = "client", feature = "server"))]
{{/hasCallbacks}}
{{^hasCallbacks}}
#[cfg(feature = "server")]
{{/hasCallbacks}}
pub mod context;

pub mod models;

#[cfg(any(feature = "client", feature = "server"))]
pub(crate) mod header;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy