Skip to content

Commit a37837d

Browse files
committed
add scripting module with script! macro
1 parent 52e866f commit a37837d

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub mod timer;
4949
/// Interact with the virtual filesystem
5050
pub mod vfs;
5151

52+
/// A set of types and macros for writing "script" processes.
53+
pub mod scripting;
54+
5255
mod types;
5356
pub use types::{
5457
address::{Address, AddressParseError},
@@ -122,6 +125,14 @@ pub fn await_message() -> Result<Message, SendError> {
122125
}
123126
}
124127

128+
/// Get the next message body from the message queue, or propagate the error.
129+
pub fn await_next_message_body() -> Result<Vec<u8>, SendError> {
130+
match await_message() {
131+
Ok(msg) => Ok(msg.body().to_vec()),
132+
Err(e) => Err(e.into()),
133+
}
134+
}
135+
125136
/// Simple wrapper over spawn() in WIT to make use of our good types
126137
pub fn spawn(
127138
name: Option<&str>,
@@ -262,11 +273,3 @@ pub fn get_capability(our: &Address, params: &str) -> Option<Capability> {
262273
})
263274
.cloned()
264275
}
265-
266-
/// Get the next message body from the message queue, or propagate the error
267-
pub fn await_next_message_body() -> Result<Vec<u8>, SendError> {
268-
match await_message() {
269-
Ok(msg) => Ok(msg.body().to_vec()),
270-
Err(e) => Err(e.into()),
271-
}
272-
}

src/scripting/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[macro_export]
2+
/// A macro for writing a "script" process. Using this will create the initial
3+
/// entry point for your process, including the standard `init` function which
4+
/// is called by the system, and a set of calls that:
5+
/// 1. Parse the `our` string into an `Address` object.
6+
/// 2. Wait for the first message to be sent to the process.
7+
/// 3. Convert the message body into a string.
8+
/// 4. Call the `init` function you provide with the `Address` and the message body string.
9+
///
10+
/// This is best used by then using `clap` to create a `Command` and parsing the body string with it.
11+
macro_rules! script {
12+
($init_func:ident) => {
13+
struct Component;
14+
impl Guest for Component {
15+
fn init(our: String) {
16+
use kinode_process_lib::{await_message, println, Address, Message, Response};
17+
let our: Address = our.parse().unwrap();
18+
let Message::Request {
19+
body,
20+
expects_response,
21+
..
22+
} = await_message().unwrap()
23+
else {
24+
return;
25+
};
26+
let body_string =
27+
format!("{} {}", our.process(), std::str::from_utf8(&body).unwrap());
28+
let response_string: String = $init_func(our, body_string);
29+
if expects_response.is_some() {
30+
Response::new()
31+
.body(response_string.as_bytes())
32+
.send()
33+
.unwrap();
34+
} else {
35+
println!("{response_string}");
36+
}
37+
}
38+
}
39+
export!(Component);
40+
};
41+
}

0 commit comments

Comments
 (0)