Skip to content

Commit 23b3e8c

Browse files
committed
refactor: rename the closure from process to run in commands and handlers
1 parent 7d39156 commit 23b3e8c

File tree

8 files changed

+43
-44
lines changed

8 files changed

+43
-44
lines changed

docs/src/content/docs/reference/commands.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To create a command, append a definition string with the topic
1919

2020
```nushell
2121
r#'{
22-
process: {|frame|
22+
run: {|frame|
2323
# frame.topic - always <command>.call
2424
# frame.hash - contains input content if present
2525
# frame.meta.args - contains call arguments
@@ -32,7 +32,7 @@ r#'{
3232

3333
The command definition requires:
3434

35-
- `process`: A closure that receives the call frame and can return a pipeline of
35+
- `run`: A closure that receives the call frame and can return a pipeline of
3636
results
3737

3838
Each value in the closure's output pipeline becomes a `.recv` event

docs/src/content/docs/reference/handlers.mdx

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ to process and act on incoming frames as they are appended to the store.
1515

1616
```nushell
1717
{
18-
process: {|frame|
18+
run: {|frame|
1919
if $frame.topic == "ping" {
2020
"pong" # Will be appended to handler.out
2121
}
@@ -39,7 +39,7 @@ handler's behavior:
3939
```nushell
4040
r###'{
4141
# Required: Handler closure
42-
process: {|frame|
42+
run: {|frame|
4343
if $frame.topic == "ping" {
4444
"pong" # Will be appended to handler.out
4545
}
@@ -72,7 +72,7 @@ configuration.
7272

7373
| Field | Description |
7474
| ---------------- | -------------------------------------------------------------------------- |
75-
| `process` | Required handler closure that processes each frame |
75+
| `run` | Required handler closure that processes each frame |
7676
| `resume_from` | "tail" (default), "head", or scru128 ID to control where processing starts |
7777
| `pulse` | Interval in milliseconds to send synthetic xs.pulse events |
7878
| `return_options` | Controls output frames: see Return Options |
@@ -95,7 +95,7 @@ The `modules` option allows handlers to use custom Nushell modules:
9595

9696
```nushell
9797
r###'{
98-
process: {|frame|
98+
run: {|frame|
9999
my-math double 8 # Use module command
100100
}
101101
modules: {
@@ -111,7 +111,7 @@ calls:
111111

112112
```nushell
113113
{
114-
process: {|frame|
114+
run: {|frame|
115115
# Initialize or increment counter
116116
let env.count = ($env | get -i count | default 0) + 1
117117
$"Processed ($env.count) frames"

examples/discord-bot/handler-heartbeat.nu

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ $env.BOT_TOKEN = .head discord.ws.token | .cas $in.hash
7373
resume_from: (.head discord.ws.start | if ($in | is-not-empty) { get id })
7474
pulse: 1000
7575

76-
process: {|frame|
76+
run: {|frame|
7777
# https://discord.com/developers/docs/topics/gateway#list-of-intents
7878
# GUILDS, GUILD_MEMBERS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, MESSAGE_CONTENT
7979
let IDENTIFY_INTENTS = 34307

examples/discord-bot/handler-roller.nu

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ $env.BOT_TOKEN = .head discord.ws.token | .cas $in.hash
3434
{
3535
modules: {discord: (.head discord.nu | .cas $in.hash)}
3636

37-
process: {|frame|
37+
run: {|frame|
3838
if $frame.topic != "discord.ws.recv" { return }
3939

4040
# TODO: .cas should also be able to take a record, to match xs2.nu's usage

src/commands/serve.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ async fn register_command(
127127
)),
128128
])?;
129129

130-
// Parse the command configuration to extract return_options (ignore the process closure here)
130+
// Parse the command configuration to extract return_options (ignore the run closure here)
131131
let (_closure, return_options) = parse_command_definition(&mut engine, &definition)?;
132132

133133
Ok(Command {
@@ -284,10 +284,10 @@ fn parse_command_definition(
284284

285285
let config = result.into_value(nu_protocol::Span::unknown())?;
286286

287-
// Get the process closure (required)
288-
let process = config
289-
.get_data_by_key("process")
290-
.ok_or("No 'process' field found in command configuration")?
287+
// Get the run closure (required)
288+
let run = config
289+
.get_data_by_key("run")
290+
.ok_or("No 'run' field found in command configuration")?
291291
.into_closure()?;
292292

293293
// Optionally parse return_options (using the same approach as in handlers)
@@ -315,5 +315,5 @@ fn parse_command_definition(
315315

316316
engine.state.merge_env(&mut stack)?;
317317

318-
Ok((process, return_options))
318+
Ok((run, return_options))
319319
}

src/commands/tests.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn test_command_with_pipeline() -> Result<(), Error> {
2323
store
2424
.cas_insert(
2525
r#"{
26-
process: {|frame|
26+
run: {|frame|
2727
let input = if ($frame.hash != null) { .cas $frame.hash } else { null }
2828
let n = $frame.meta.args.n
2929
1..($n) | each {$"($in): ($input)"}
@@ -92,7 +92,7 @@ async fn test_command_error_handling() -> Result<(), Error> {
9292
store
9393
.cas_insert(
9494
r#"{
95-
process: {|frame|
95+
run: {|frame|
9696
$frame.meta.args.not_exists # This will error
9797
}
9898
}"#,
@@ -144,7 +144,7 @@ async fn test_command_single_value() -> Result<(), Error> {
144144
store
145145
.cas_insert(
146146
r#"{
147-
process: {|frame| "single value output"}
147+
run: {|frame| "single value output"}
148148
}"#,
149149
)
150150
.await?,
@@ -208,7 +208,7 @@ async fn test_command_empty_output() -> Result<(), Error> {
208208
store
209209
.cas_insert(
210210
r#"{
211-
process: {|frame|}
211+
run: {|frame|}
212212
}"#,
213213
)
214214
.await?,
@@ -250,7 +250,7 @@ async fn test_command_tee_and_append() -> Result<(), Error> {
250250
store
251251
.cas_insert(
252252
r#"{
253-
process: {|frame|
253+
run: {|frame|
254254
[1 2 3] | tee { collect { math sum } | to json -r | .append sum }
255255
}
256256
}"#,

src/handlers/handler.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ impl Handler {
8282
)),
8383
])?;
8484

85-
let (mut process, mut config) =
86-
parse_handler_configuration_script(&mut engine, &expression)?;
85+
let (mut run, mut config) = parse_handler_configuration_script(&mut engine, &expression)?;
8786

8887
// Load modules and reparse if needed
8988
if !config.modules.is_empty() {
@@ -96,10 +95,10 @@ impl Handler {
9695

9796
// we need to re-parse the expression after loading modules, so that the closure has access
9897
// to the additional modules: not the best, but I can't see a better way
99-
(process, config) = parse_handler_configuration_script(&mut engine, &expression)?;
98+
(run, config) = parse_handler_configuration_script(&mut engine, &expression)?;
10099
}
101100

102-
let block = engine.state.get_block(process.block_id);
101+
let block = engine.state.get_block(run.block_id);
103102
if block.signature.required_positional.len() != 1 {
104103
return Err(format!(
105104
"Closure must accept exactly one frame argument, found {}",
@@ -108,7 +107,7 @@ impl Handler {
108107
.into());
109108
}
110109

111-
let engine_worker = Arc::new(EngineWorker::new(engine, process));
110+
let engine_worker = Arc::new(EngineWorker::new(engine, run));
112111

113112
Ok(Self {
114113
id,
@@ -475,9 +474,9 @@ fn parse_handler_configuration_script(
475474

476475
let config = result.into_value(nu_protocol::Span::unknown())?;
477476

478-
let process = config
479-
.get_data_by_key("process")
480-
.ok_or("No 'process' field found in handler configuration")?
477+
let run = config
478+
.get_data_by_key("run")
479+
.ok_or("No 'run' field found in handler configuration")?
481480
.into_closure()?;
482481

483482
let resume_from = match config.get_data_by_key("resume_from") {
@@ -542,7 +541,7 @@ fn parse_handler_configuration_script(
542541
engine.state.merge_env(&mut stack)?;
543542

544543
Ok((
545-
process,
544+
run,
546545
HandlerConfig {
547546
resume_from,
548547
modules,

src/handlers/tests.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async fn test_register_invalid_closure() {
116116
.hash(
117117
store
118118
.cas_insert(
119-
r#"{process: {|| 42}}"#, // Invalid closure, expects at least one argument
119+
r#"{run: {|| 42}}"#, // Invalid closure, expects at least one argument
120120
)
121121
.await
122122
.unwrap(),
@@ -162,7 +162,7 @@ async fn test_register_parse_error() {
162162
.cas_insert(
163163
r#"
164164
{
165-
process: {|frame|
165+
run: {|frame|
166166
.head index.html | .cas
167167
}
168168
}
@@ -193,7 +193,7 @@ async fn test_register_parse_error() {
193193
}
194194

195195
#[tokio::test]
196-
// This test is to ensure that a handler does not process its own output
196+
// This test is to ensure that a handler does not run its own output
197197
async fn test_no_self_loop() {
198198
let (store, _temp_dir) = setup_test_environment().await;
199199
let options = ReadOptions::builder().follow(FollowOption::On).build();
@@ -204,13 +204,13 @@ async fn test_no_self_loop() {
204204
"xs.threshold".to_string()
205205
);
206206

207-
// Register handler that would process its own output if not prevented
207+
// Register handler that would run its own output if not prevented
208208
store
209209
.append(
210210
Frame::builder("echo.register", ZERO_CONTEXT)
211211
.hash(
212212
store
213-
.cas_insert(r#"{process: {|frame| $frame}}"#)
213+
.cas_insert(r#"{run: {|frame| $frame}}"#)
214214
.await
215215
.unwrap(),
216216
)
@@ -271,7 +271,7 @@ async fn test_essentials() {
271271
.cas_insert(
272272
r#"
273273
{
274-
process: {|frame|
274+
run: {|frame|
275275
if $frame.topic != "pew" { return }
276276
"processed"
277277
}
@@ -373,7 +373,7 @@ async fn test_unregister_on_error() {
373373
store
374374
.cas_insert(
375375
r#"{
376-
process: {|frame|
376+
run: {|frame|
377377
let x = {"foo": null}
378378
$x.foo.bar # Will error at runtime - null access
379379
}
@@ -421,7 +421,7 @@ async fn test_return_options() {
421421
ttl: "head:1"
422422
}
423423
424-
process: {|frame|
424+
run: {|frame|
425425
if $frame.topic != "ping" { return }
426426
"pong"
427427
}
@@ -491,7 +491,7 @@ async fn test_custom_append() {
491491
store
492492
.cas_insert(
493493
r#"{
494-
process: {|frame|
494+
run: {|frame|
495495
if $frame.topic != "trigger" { return }
496496
"1" | .append topic1 --meta {"t": "1"}
497497
"2" | .append topic2 --meta {"t": "2"}
@@ -541,7 +541,7 @@ async fn test_handler_replacement() {
541541
.hash(
542542
store
543543
.cas_insert(
544-
r#"{process: {|frame|
544+
r#"{run: {|frame|
545545
if $frame.topic != "trigger" { return }
546546
"handler1"
547547
}}"#,
@@ -563,7 +563,7 @@ async fn test_handler_replacement() {
563563
.hash(
564564
store
565565
.cas_insert(
566-
r#"{process: {|frame|
566+
r#"{run: {|frame|
567567
if $frame.topic != "trigger" { return }
568568
"handler2"
569569
}}"#,
@@ -639,7 +639,7 @@ async fn test_handler_with_module() -> Result<(), Error> {
639639
mymod: (.head mymod.nu | .cas $in.hash)
640640
}
641641
642-
process: {|frame|
642+
run: {|frame|
643643
if $frame.topic != "trigger" { return }
644644
mymod add_nums 40 2
645645
}
@@ -712,7 +712,7 @@ async fn test_handler_preserve_env() -> Result<(), Error> {
712712
}
713713
714714
{
715-
process: {|frame|
715+
run: {|frame|
716716
if $frame.topic != "trigger" { return }
717717
inc-abc
718718
}
@@ -798,7 +798,7 @@ async fn test_handler_context_isolation() -> Result<(), Error> {
798798
store
799799
.cas_insert(
800800
r#"{
801-
process: "not a closure"
801+
run: "not a closure"
802802
}"#,
803803
)
804804
.await?,
@@ -820,7 +820,7 @@ async fn test_handler_context_isolation() -> Result<(), Error> {
820820
let handler_hash = store
821821
.cas_insert(
822822
r#"{
823-
process: {|frame|
823+
run: {|frame|
824824
if $frame.topic != "trigger" { return }
825825
"explicit append" | .append echo.direct
826826
"handler return"

0 commit comments

Comments
 (0)