# Async Code Generator
[](https://crates.io/crates/async-codegen)
[](https://www.gnu.org/licenses/license-recommendations.html)
[](https://docs.rs/async-codegen)
A library for async code generation that imposes no ownership choices (can use borrowed or owned data) and is fully composable using generics and general-purpose structs.
### Example
This may seem tedious, but the design allows for maximum re-usability and composability.
```rust
async fn write_function<O>(output: &mut O) -> Result<(), O::Error> where O: Output {
// For more advanced usage, you can replace Str("") by other Writable implementations
let function_def = FunctionDef {
attr: SingularSeq(Deprecated::with_message(Str("because we all love deprecation"))),
mods: SingularSeq(ModPub),
decl: Function {
name: Str("my_func"),
args: CombinedSeq(
SingularSeq(FunctionParam(Str("var1"), Str("Type"))),
SingularSeq(FunctionParam(Str("var2"), Parameterized::new(Str("Option"), SingularSeq(Str("bool")))))
)
},
return_type: Parameterized::new(Str("Box"), SingularSeq(Str("str"))),
body: Str("\ntodo!()\n")
};
function_def.write_to(output).await
}
```
Will render as:
```rust
#[deprecated = "because we all love deprecation"]
pub fn my_func(var1: Type, var2: Option<bool>) -> Box<str> {
todo!()
}
```
## Design
The API is conceptually simple. Performing code generation is akin to writing a syntax tree.
### Model
Writable elements are composed from other writable elements. They are divided into two kinds:
1. `Writable` - the main trait used by the library
2. `WritableSeq` - for a sequence of writable values handled in the same way.
An example of a writable sequence would be function arguments, type variables, etc. An example of a standalone writable value would be a function body.
### Async and Errors
Every code generation-related function returns the library user's error type.
Also, the methods are `async` and must be awaited as such. This lets the library user plug in their favorite async runtime... because why not make code generation fast?
## Library
### Dependency
Add this library to your Cargo.toml. Check crates.io for the latest version.
```toml
[dependencies]
async-codegen = "0.5.0"
```
### Licensing
Licensed under the Apache License v2.0. See the LICENSE.txt.