Struct CodeGenerator

Source
pub struct CodeGenerator { /* private fields */ }
Expand description

OpenAPI Lambda code generator.

This code generator is intended to be called from a build.rs Rust build script. It emits an out.rs file to the directory referenced by the OUT_DIR environment variable set by Cargo. This file defines a module named models containing Rust types for the input parameters and request/response bodies defined in the OpenAPI definition. It also defines one module for each call to add_api_lambda, which defines an Api trait with one method for each operation (path + HTTP method) defined in the OpenAPI definition.

In addition, the generator writes the following files to the out_dir directory specified in the call to new:

  • openapi-apigw.yaml - OpenAPI definition annotated with x-amazon-apigateway-integration extensions to be used by Amazon API Gateway. This file is also modified from the input OpenAPI definition to help adhere to the subset of OpenAPI features supported by Amazon API Gateway. In particular, all references are merged into a single file, and discriminator properties are removed.
  • One file for each call to add_api_lambda named <MODULE_NAME>_handler.rs, where <MODULE_NAME> is the mod_name in the ApiLambda passed to add_api_lambda. This file contains a placeholder implementation of the corresponding Api trait. To get started, copy this file into src/, define a corresponding module (<MODULE_NAME>_handler) in src/lib.rs, and replace each instance of todo!() in the trait implementation.

§Examples

§Mono-Lambda

The following invocation in build.rs uses a single Lambda function to handle all API endpoints:

CodeGenerator::new("openapi.yaml", ".openapi-lambda")
  .add_api_lambda(
    ApiLambda::new("backend", LambdaArn::cloud_formation("BackendApiFunction.Alias"))
  )
  .generate();

§Multiple Lambda functions

The following invocation in build.rs uses multiple Lambda functions, each handling a subset of API endpoints:

CodeGenerator::new("openapi.yaml", ".openapi-lambda")
  .add_api_lambda(
    ApiLambda::new("pet", LambdaArn::cloud_formation("PetApiFunction.Alias"))
    // Only include API endpoints with the `pet` tag.
    .with_op_filter(|op| op.tags.iter().any(|tag| tag == "pet"))
  )
  .add_api_lambda(
    ApiLambda::new("store", LambdaArn::cloud_formation("StoreApiFunction.Alias"))
    // Only include API endpoints with the `store` tag.
    .with_op_filter(|op| op.tags.iter().any(|tag| tag == "store"))
  )
  .generate();

Implementations§

Source§

impl CodeGenerator

Source

pub fn new<P, O>(openapi_path: P, out_dir: O) -> Self
where P: Into<PathBuf>, O: Into<PathBuf>,

Construct a new CodeGenerator.

§Arguments
  • openapi_path - Input path to OpenAPI definition in YAML format
  • out_dir - Output directory path in which openapi-apigw.yaml and one <MODULE_NAME>_handler.rs file for each call to add_api_lambda will be written
Source

pub fn add_api_lambda(self, builder: ApiLambda) -> Self

Register an API Lambda function for code generation.

Each call to this method will result in a module being generated that contains an Api trait with methods for the corresponding API endpoints. See ApiLambda for further details.

Source

pub fn generate(self)

Emit generated code.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.