diff --git a/README.md b/README.md index 604503a..6aa8d7c 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Rust types will be generated in the models folder according to the given payload - Only json payloads are currently supported for automatic deserialization - Only one server is currently supported and only nats protocol is supported +- Generated microservice doesn't support authentication with NATS-broker out of the box - Only one message is currently supported per channel, payloads can be choosen freely including anyOf/oneOf/allOf - The generated rust types are required by default, if you want to use optional types, please modify the generated types after generation or use oneOf/anyOf/allOf to represent optional types - references in the specification are only suppported inside the same file, external references are not supported diff --git a/doc/templates.org b/doc/templates.org index 4185b11..fca9d56 100644 --- a/doc/templates.org +++ b/doc/templates.org @@ -5,6 +5,7 @@ - Any templates that in the templates folder at compilation time will be embedded in the compiled binary. - If you only have the binary you can put templates in the folder ~user-templates~. If a file from the ~user-templates~ folder has the same path as an embedded template, only the template from ~user-template~ will be rendered. + - set the command line argument ~--user-templates~ or ~-u~ to set a custom folder - the last file extension will be removed e.g: ~file.rs.go~ will be rendered to ~file.rs~. ** What fields are available inside templates? - Any of these fields will be accessible: @@ -50,6 +51,7 @@ pub struct SimplifiedMessage { - ~$$handler$$~ - ~$$producer$$~ - ~$$model$$~ + - ~$$schemas$$~ ** Functions available inside the templates - ~to_lower(input: String) -> String~ converts String to lowercase - ~key_exists(input: String) -> String~ checks if key exists diff --git a/src/cli/mod.rs b/src/cli/mod.rs index c4b82f3..b3232a3 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,7 +13,7 @@ pub struct Args { /// The output directory /// If not specified, the output will be written to ./output/ /// If the directory does not exist, it will be created - #[arg(short, long, default_value = "./output/")] + #[arg(short, long, default_value = "output")] pub output: String, /// The name of the project @@ -28,4 +28,8 @@ pub struct Args { ///Optionally enable logging in generated Microservice #[arg(short, long, default_value = "false")] pub log: bool, + + ///Optionally provide directory for additional templates + #[arg(short, long, default_value = "user_templates")] + pub user_templates: String, } diff --git a/src/generator/common.rs b/src/generator/common.rs index c884c42..be2a915 100644 --- a/src/generator/common.rs +++ b/src/generator/common.rs @@ -89,22 +89,25 @@ pub fn render_write_all_fs_templates( template_dir: &Path, context: &TemplateContext, output_path: &Path, -) { +) -> Result<(), Error> { for template_dir_entry in WalkDir::new(template_dir) .into_iter() .filter_map(|x| x.ok()) { let template_path = &template_dir_entry.path(); if template_path.is_file() { - let template = read_to_string(template_path).unwrap(); + let template = read_to_string(template_path)?; render_write_dependant( - template_path.strip_prefix(template_dir).unwrap(), + template_path + .strip_prefix(template_dir) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?, template, context, output_path, ); } } + Ok(()) } /// renders and writes all templates in `template_file_paths` to `output_path`/`template_file_path` diff --git a/src/main.rs b/src/main.rs index 793e5dd..bf4feb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() { let specfile_path = Path::new(&args.specification); println!("📄 Using specification file {:?}", specfile_path); - let template_dir = Path::new("./user_templates/"); + let user_template_dir = Path::new(&args.user_templates); let spec: AsyncAPI = match parser::asyncapi_model_parser::parse_spec_to_model(specfile_path) { Ok(spec) => spec, @@ -55,11 +55,11 @@ fn main() { cargo_command!("init", "--bin", output_path); render_write_all_embedded_templates(&async_config, output_path); - render_write_all_fs_templates(template_dir, &async_config, output_path); + render_write_all_fs_templates(user_template_dir, &async_config, output_path).unwrap(); println!("🚀 File generation finished, formatting generated files..."); // runs cargo format on path - cargo_command!("fmt", "--", output_path.join("src/main.rs")); + cargo_command!("fmt", "--", output_path.join("src").join("main.rs")); // cargo fix, mostly for cleaning unused imports cargo_command!( "fix", diff --git a/templates/src/handler/$$producer$$.rs.go b/templates/src/handler/$$producer$$.rs.go index a99ad2f..1aaa476 100644 --- a/templates/src/handler/$$producer$$.rs.go +++ b/templates/src/handler/$$producer$$.rs.go @@ -25,7 +25,7 @@ use log::{debug, warn, error}; let payload = match serde_json::from_slice::(&message.message.payload) { Ok(payload) => payload, Err(_) => { - error!("Failed to deserialize message payload, make sure payload is a valid json: user_signed_up_message\nOriginal message: {:#?}", message); + error!("Failed to deserialize message payload, make sure payload is a valid json: {{ .unique_id }}\nOriginal message: {:#?}", message); return; } }; @@ -34,7 +34,7 @@ use log::{debug, warn, error}; Path::new("./src/schemas/{{ .unique_id }}_payload_schema.json"), &payload, ) { - error!("Failed to validate message schema: user_signed_up_message\nOriginal message: {:#?}\nError: {}", message, e); + error!("Failed to validate message schema: {{ .unique_id }}\nOriginal message: {:#?}\nError: {}", message, e); return; } {{ end }} @@ -72,7 +72,7 @@ use log::{debug, warn, error}; let payload = match serde_json::from_slice::(&message.payload) { Ok(payload) => payload, Err(_) => { - error!("Failed to deserialize message payload, make sure payload is a valid json: user_signed_up_message\nOriginal message: {:#?}", message); + error!("Failed to deserialize message payload, make sure payload is a valid json: {{ .unique_id }}\nOriginal message: {:#?}", message); return; } }; @@ -81,7 +81,7 @@ use log::{debug, warn, error}; Path::new("./src/schemas/{{ .unique_id }}_payload_schema.json"), &payload, ) { - error!("Failed to validate message schema: user_signed_up_message\nOriginal message: {:#?}\nError: {}", message, e); + error!("Failed to validate message schema: {{ .unique_id }}\nOriginal message: {:#?}\nError: {}", message, e); return; } {{ end }}