11 releases

new 0.1.10 May 9, 2025
0.1.9 Mar 29, 2025
0.1.8 Feb 5, 2025
0.1.7 Jan 31, 2025
0.1.0 Dec 11, 2024

#158 in Template engine

MIT/Apache

17KB
279 lines

mandolin

crates.io

Generate server code in Rust from openapi specification and jinja2 templates.

Online demo with wasm: https://lzpel.github.io/mandolin/

Using mandolin

Render axum server code using builtin template

use mandolin;
use serde_yaml;
use std::fs;
fn main() {
	let input_api = serde_yaml::from_str(
		fs::read_to_string("./openapi/openapi.yaml")
			.unwrap()
			.as_str(),
	)
	.unwrap();
	let env = mandolin::environment(input_api).unwrap();
	let template = env.get_template("RUST_SERVER_AXUM").unwrap();
	let output = template.render(0).unwrap();
	// write the rendered output
	fs::write("./output/server_builtin.rs", output).unwrap();
}

Render axum server source code using your custom jinja2 template.

use mandolin;
use serde_yaml;
use std::fs;

fn main() {
	let input_api = serde_yaml::from_str(
		fs::read_to_string("./openapi/openapi.yaml")
			.unwrap()
			.as_str(),
	)
	.unwrap();
	let mut env = mandolin::environment(input_api).unwrap();
	// add your templates
	let content = fs::read_to_string("./templates/rust_server_axum.template").unwrap();
	env.add_template("RUST_SERVER_AXUM", content.as_str()).unwrap();

	let content = fs::read_to_string("./templates/schema.template").unwrap();
	env.add_template("SCHEMA", content.as_str()).unwrap();

	let content = fs::read_to_string("./templates/server_axum.template").unwrap();
	env.add_template("SERVER_AXUM", content.as_str()).unwrap();

	let content = fs::read_to_string("./templates/trait.template").unwrap();
	env.add_template("TRAIT", content.as_str()).unwrap();
	// render
	let template = env.get_template("RUST_SERVER_AXUM").unwrap();
	let output = template.render(0).unwrap();
	fs::write("./output/server_custom.out.rs", output).unwrap();
}

version

  • 0.1.7 hotfix
  • 0.1.6 independent from regex, tera
  • 0.1.5 fix ref filter
  • 0.1.4 replace minijinja from tera
  • 0.1.3
    • simplify mandolin::Mandolin::new pub fn new(api: OpenAPI) -> Result<Self, serde_yaml::Error> into pub fn new(api: OpenAPI) -> Self
    • remove mandolin::Mandolin::template_from_path
    • move serde_yaml(deprecated) in dependency into in dev-dependency
    • update README.md
    • add examples
    • rename mandolin::builtin into mandolin::templates
    • exclude frontend from crate
  • 0.1.0 publish

my favorite mandolin music

Dependencies

~2.4–3.5MB
~64K SLoC