Skip to content

Commit

Permalink
small fixes (#108)
Browse files Browse the repository at this point in the history
* no more Unix specific file_paths
* removed spec specific strings from templates
* user_templates can now be provided in cli
* render_write_fs_templates now returns `Result`
* updated doc/templates accordingly
* added limitation regarding nats broker authentication
  • Loading branch information
bird-dancer committed Jul 14, 2023
1 parent 0498f98 commit d05d047
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions doc/templates.org
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
9 changes: 6 additions & 3 deletions src/generator/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions templates/src/handler/$$producer$$.rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use log::{debug, warn, error};
let payload = match serde_json::from_slice::<serde_json::Value>(&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;
}
};
Expand All @@ -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 }}
Expand Down Expand Up @@ -72,7 +72,7 @@ use log::{debug, warn, error};
let payload = match serde_json::from_slice::<serde_json::Value>(&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;
}
};
Expand All @@ -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 }}
Expand Down

0 comments on commit d05d047

Please sign in to comment.