Skip to content

Commit 8ed6c9a

Browse files
doepnernstano45
andauthored
5 generate rustdoc (#40)
* fixed compiled code not to be runnable * added rust documentation generation to main function * added cargo doc command and a basic readme to the template * try to make clippy understand * fix clippy --------- Co-authored-by: Stanislav Kosorin <stanislav.kosorin@cresta.ai>
1 parent 16143a0 commit 8ed6c9a

8 files changed

Lines changed: 42 additions & 4 deletions

File tree

example/specs/basic.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ asyncapi: 2.1.0
22
info:
33
title: My_API
44
version: 1.0.0
5+
description: a basic test api
56
servers:
67
production:
78
url: demo.nats.io

src/generator/common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ pub fn template_render_write<T: Into<gtmpl::Value>>(
4444
let render = gtmpl::template(&template, context_ref).expect("Could not inject template");
4545
utils::write_to_path_create_dir(&render, output_path).unwrap();
4646
}
47+
48+
pub fn cargo_generate_rustdoc(path: &Path) {
49+
Command::new("cargo")
50+
.current_dir(path)
51+
.arg("doc")
52+
.arg("--no-deps")
53+
.output()
54+
.expect("failed to generate rustdoc");
55+
}

src/generator/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod common;
22
pub use common::cargo_add;
33
pub use common::cargo_fmt;
4+
pub use common::cargo_generate_rustdoc;
45
pub use common::cargo_init_project;
56
pub use common::template_render_write;

src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod parser;
55
mod template_model;
66
mod utils;
77

8-
use crate::generator::template_render_write;
8+
use crate::generator::{cargo_generate_rustdoc, template_render_write};
99
use clap::Parser;
1010
use generator::{cargo_add, cargo_fmt, cargo_init_project};
1111
use std::path::Path;
@@ -22,16 +22,16 @@ fn main() {
2222
let spec = parser::parse_spec_to_model(specfile_path, validator_schema_path).unwrap();
2323
println!("{:?}", spec);
2424

25-
let title = match args.project_title {
25+
let title: &str = match &args.project_title {
2626
Some(t) => t,
27-
None => spec.info.title.clone(),
27+
None => &spec.info.title,
2828
};
29-
// output_path
3029
let output = args.output_directory;
3130
let output_path = &Path::new(&output).join(title.replace(' ', "_").to_lowercase());
3231
println!("output_path: {:?}", output_path);
3332

3433
let async_config = parser::spec_to_pubsub_template_type(&spec).unwrap();
34+
3535
// render template and write
3636
template_render_write(
3737
&template_path.join("main.rs"),
@@ -43,11 +43,21 @@ fn main() {
4343
&async_config,
4444
&output_path.join("src/handler.rs"),
4545
);
46+
template_render_write(
47+
&template_path.join("Readme.md"),
48+
&async_config,
49+
&output_path.join("Readme.md"),
50+
);
4651
// make output a compilable project
4752
cargo_init_project(output_path);
4853
cargo_fmt(&output_path.join("src/main.rs"));
4954
cargo_add(output_path, "tokio", Some("rt-multi-thread")); // when there are more crates move to generator.rs
5055
cargo_add(output_path, "async_nats", None);
5156
cargo_add(output_path, "futures", None);
5257
cargo_add(output_path, "serde", None);
58+
println!("generating docs...");
59+
println!(
60+
"this may take a while, you can already use the generated project in the output directory"
61+
);
62+
cargo_generate_rustdoc(output_path);
5363
}

src/parser/pubsub.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ pub fn spec_to_pubsub_template_type<'a>(
108108
subscribe_channels,
109109
publish_channels,
110110
schema: joined_schemas,
111+
title: &spec.info.title,
112+
description: &spec.info.description,
111113
};
112114
Ok(pubsub_template)
113115
}

src/template_model/pubsub_template.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::string::*;
66

77
#[derive(Serialize, Debug)]
88
pub struct PubsubTemplate<'a> {
9+
pub title: &'a String,
10+
pub description: &'a Option<String>,
911
pub server: &'a Server,
1012
pub subscribe_channels: Vec<(&'a String, &'a Operation)>,
1113
pub publish_channels: Vec<(&'a String, &'a Operation)>,

templates/Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# {{.title}}
2+
{{if .description}}
3+
{{.description}}
4+
{{end}}
5+
6+
## Documenation
7+
Open the documentation with the following command:
8+
```
9+
cargo doc --no-deps --open
10+
```

templates/handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
use std::time;
22
use async_nats::{Client, Message};
33
use crate::publish_message;
4+
use serde::{Deserialize, Serialize};
45

56
{{ .schema }}
67

78
{{ range .subscribe_channels }}
9+
/// this handler is called when a message is received on {{ (index . 1).operationId }}
810
pub fn handler_{{ (index . 1).operationId }}(message: Message) {
911
println!("Received message {:#?}", message)
1012
}
1113
{{ end }}
1214

1315
{{ range .publish_channels }}
16+
/// publish message to {{ (index . 1).operationId }}
1417
pub async fn producer_{{ (index . 1).operationId }}(client: &Client, channel: &str) {
1518
loop {
1619
tokio::time::sleep(time::Duration::from_secs(2)).await;

0 commit comments

Comments
 (0)