Skip to content

Commit 35d5b7f

Browse files
add Rust IPC example
1 parent c9705f3 commit 35d5b7f

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "helloworldipc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
neutralipcrs = "0.0.0-dev1"
8+
serde_json = "1.0"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Neutral TS Hello World IPC example
2+
==================================
3+
4+
Command line Hello World rust example.
5+
6+
- Requires the IPC server: [Neutral TS IPC Server](https://github.com/FranBarInstance/neutral-ipc/releases)
7+
- Requires the Rust IPC client: [Neutral TS Rust IPC Client](https://crates.io/crates/neutralipcrs)
8+
9+
Navigate to the examples/rust/helloworldipc directory and then to helloworld:
10+
11+
```
12+
cargo run
13+
```
14+
15+
Links
16+
-----
17+
18+
Neutral TS template engine.
19+
20+
- [Template docs](https://franbarinstance.github.io/neutralts-docs/docs/neutralts/doc/)
21+
- [Repository](https://github.com/FranBarInstance/neutralts)
22+
- [Crate](https://crates.io/crates/neutralts)
23+
- [Examples](https://github.com/FranBarInstance/neutralts-docs/tree/master/examples)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Neutral TS Hello World Rust IPC example
3+
* https://github.com/FranBarInstance/neutralts-docs/
4+
*/
5+
6+
use neutralipcrs::NeutralIpcTemplate;
7+
use serde_json::json;
8+
9+
fn main() {
10+
// The schema contains among other things the data and variables for the template
11+
let schema = json!({
12+
"data": {
13+
"hello": "Hello World"
14+
}
15+
});
16+
17+
// This will also work by doing schema_str.into() later
18+
let _schema_str = r#"{
19+
"data": {
20+
"hello": "Hello World"
21+
}
22+
}"#;
23+
24+
// Determine the template full path.
25+
// Since the IPC server runs in a separate process,
26+
// paths relative to this process will not work.
27+
let manifest_dir = env!("CARGO_MANIFEST_DIR");
28+
let template_path = format!("{}/template.ntpl", manifest_dir);
29+
30+
// Create an instance of Template
31+
let mut template = NeutralIpcTemplate::from_file_value(&template_path, schema).unwrap();
32+
33+
// Render the template
34+
let contents = template.render().unwrap();
35+
36+
// Get the status code, text and parameter
37+
let status_code: &str = template.get_status_code();
38+
let status_text: &str = template.get_status_text();
39+
let status_param: &str = template.get_status_param();
40+
41+
// Print the rendered content, in other cases contents will be sent to output according to framework.
42+
println!("{}", contents);
43+
println!("Status: {} {} {}", status_code, status_text, status_param);
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
From rust example: {:;hello:}

0 commit comments

Comments
 (0)