pondrs 0.3.0

A pipeline execution library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Application framework for building pipeline executables.
//!
//! The [`App`] struct bundles catalog, params, hooks, and runners together
//! and provides methods for pipeline execution and CLI dispatch.

#[cfg(feature = "std")]
pub mod cli;
#[cfg(feature = "std")]
pub mod config;

#[cfg(feature = "std")]
use std::prelude::v1::*;

use serde::Serialize;

use crate::error::PondError;
use crate::hooks::Hooks;
use crate::pipeline::{PipelineFn, PipelineInfo};
use crate::runners::{Runners, SequentialRunner};

// --- Command enum (no_std core) ---

/// Subcommand for pipeline dispatch.
pub enum Command {
    /// Execute the pipeline.
    Run,
    /// Validate pipeline structure.
    Check,
    /// Build and serve pipeline visualization.
    #[cfg(feature = "std")]
    Viz {
        port: u16,
        output: Option<std::string::String>,
        export: Option<std::string::String>,
    },
}

// --- Default runners (conditional on std for ParallelRunner) ---

#[cfg(feature = "std")]
type DefaultRunners = (SequentialRunner, crate::runners::ParallelRunner);
#[cfg(not(feature = "std"))]
type DefaultRunners = (SequentialRunner,);

// --- App struct ---

/// Pipeline application with catalog, params, hooks, and runners.
///
/// # Construction
///
/// - [`App::new`] — provide catalog and params directly (no_std + std)
/// - [`App::from_cli`] — load from YAML via parsed [`CliArgs`](cli::CliArgs) (std only)
/// - [`App::from_args`] — parse CLI args and load from YAML (std only)
///
/// # Execution
///
/// - [`App::execute`] — run the pipeline directly
/// - [`App::dispatch`] — dispatch based on stored [`Command`]
pub struct App<C, P, H = (), R = DefaultRunners> {
    catalog: C,
    params: P,
    hooks: H,
    runners: R,
    command: Command,
    #[cfg(feature = "std")]
    runner_name: Option<std::string::String>,
    #[cfg(feature = "std")]
    node_filter: Option<crate::pipeline::NodeFilter>,
    #[cfg(feature = "std")]
    program_name: std::string::String,
}

// --- Core constructors (no_std) ---

impl<C, P> App<C, P, (), DefaultRunners> {
    /// Create an App with provided catalog and params.
    ///
    /// Uses default hooks (none) and default runners. Command defaults to `Run`.
    pub fn new(catalog: C, params: P) -> Self {
        App {
            catalog,
            params,
            hooks: (),
            runners: DefaultRunners::default(),
            command: Command::Run,
            #[cfg(feature = "std")]
            runner_name: None,
            #[cfg(feature = "std")]
            node_filter: None,
            #[cfg(feature = "std")]
            program_name: std::string::String::new(),
        }
    }
}

// --- Builder methods (no_std) ---

impl<C, P, H, R> App<C, P, H, R> {
    /// Replace hooks, returning a new App with different hook type.
    pub fn with_hooks<H2: Hooks>(self, hooks: H2) -> App<C, P, H2, R> {
        App {
            catalog: self.catalog,
            params: self.params,
            hooks,
            runners: self.runners,
            command: self.command,
            #[cfg(feature = "std")]
            runner_name: self.runner_name,
            #[cfg(feature = "std")]
            node_filter: self.node_filter,
            #[cfg(feature = "std")]
            program_name: self.program_name,
        }
    }

    /// Replace runners, returning a new App with different runner type.
    pub fn with_runners<R2: Runners>(self, runners: R2) -> App<C, P, H, R2> {
        App {
            catalog: self.catalog,
            params: self.params,
            hooks: self.hooks,
            runners,
            command: self.command,
            #[cfg(feature = "std")]
            runner_name: self.runner_name,
            #[cfg(feature = "std")]
            node_filter: self.node_filter,
            #[cfg(feature = "std")]
            program_name: self.program_name,
        }
    }

    /// Borrow the catalog.
    pub fn catalog(&self) -> &C {
        &self.catalog
    }

    /// Borrow the params.
    pub fn params(&self) -> &P {
        &self.params
    }

    /// Set the command for dispatch.
    pub fn with_command(mut self, command: Command) -> Self {
        self.command = command;
        self
    }

    /// Borrow the current command.
    pub fn command(&self) -> &Command {
        &self.command
    }
}

// --- Execution methods (no_std) ---

impl<C: Serialize, P: Serialize, H: Hooks, R: Runners> App<C, P, H, R> {
    /// Execute the pipeline directly.
    ///
    /// Uses the runner selected by CLI (`--runner`), defaulting to `"sequential"`.
    pub fn execute<'a, E, F>(&'a self, f: F) -> Result<(), E>
    where
        E: From<PondError> + core::fmt::Display + core::fmt::Debug + Send + Sync + 'static,
        F: PipelineFn<'a, C, P, E>,
    {
        let pipeline = f.call(&self.catalog, &self.params);

        #[cfg(feature = "std")]
        let name = self.runner_name.as_deref().unwrap_or(self.runners.first_name());
        #[cfg(not(feature = "std"))]
        let name = self.runners.first_name();

        #[cfg(feature = "std")]
        if let Some(ref filter) = self.node_filter {
            let filtered = crate::pipeline::filter_steps(&pipeline, &self.catalog, &self.params, filter)
                .map_err(E::from)?;
            return match self.runners.run_by_name(name, &filtered, &self.catalog, &self.params, &self.hooks) {
                Some(result) => result,
                None => Err(PondError::RunnerNotFound.into()),
            };
        }

        match self.runners.run_by_name(name, &pipeline, &self.catalog, &self.params, &self.hooks) {
            Some(result) => result,
            None => Err(PondError::RunnerNotFound.into()),
        }
    }

    /// Dispatch based on the stored [`Command`].
    ///
    /// - `Command::Run` → [`execute`](App::execute)
    /// - `Command::Check` → validate pipeline structure
    /// - `Command::Viz` → build graph and serve/export (std + viz feature only)
    pub fn dispatch<'a, E, F>(&'a self, f: F) -> Result<(), E>
    where
        E: From<PondError> + core::fmt::Display + core::fmt::Debug + Send + Sync + 'static,
        F: PipelineFn<'a, C, P, E>,
    {
        match &self.command {
            Command::Run => self.execute(f),
            Command::Check => {
                let pipeline = f.call(&self.catalog, &self.params);
                match pipeline.check() {
                    Ok(()) => {
                        #[cfg(feature = "std")]
                        println!("Pipeline is valid.");
                        Ok(())
                    }
                    Err(e) => {
                        #[cfg(feature = "std")]
                        {
                            Err(PondError::Custom(std::format!(
                                "Pipeline validation failed:\n  - {e}"
                            ))
                            .into())
                        }
                        #[cfg(not(feature = "std"))]
                        {
                            let _ = e;
                            Err(PondError::CheckFailed.into())
                        }
                    }
                }
            }
            #[cfg(feature = "std")]
            Command::Viz { port, output, export } => {
                self.dispatch_viz(f, *port, output.as_deref(), export.as_deref())
            }
        }
    }
}

// --- std-only constructors and methods ---

#[cfg(feature = "std")]
mod std_app {
    use super::*;
    use crate::graph::build_pipeline_graph;
    use clap::Parser;
    use cli::{CliArgs, Command as CliCommand};
    use config::{apply_overrides, deserialize_config, load_yaml};
    use serde::de::DeserializeOwned;

    /// Load a YAML config file, apply overrides, and deserialize.
    fn load_config<T: DeserializeOwned>(
        path: &str,
        overrides: &[String],
    ) -> Result<T, PondError> {
        let mut value = load_yaml(path)?;
        if !overrides.is_empty() {
            apply_overrides(&mut value, overrides);
        }
        Ok(deserialize_config(value)?)
    }

    /// Convert a CLI Command to our core Command, extracting the runner name
    /// and optional node filter.
    fn extract_command(cli_cmd: &CliCommand) -> (Command, Option<String>, Option<crate::pipeline::NodeFilter>) {
        match cli_cmd {
            CliCommand::Run { runner, nodes, from_nodes, to_nodes, .. } => {
                let filter = if !nodes.is_empty() {
                    Some(crate::pipeline::NodeFilter::Nodes(
                        nodes.iter().cloned().collect(),
                    ))
                } else if !from_nodes.is_empty() || !to_nodes.is_empty() {
                    Some(crate::pipeline::NodeFilter::FromTo {
                        from: from_nodes.iter().cloned().collect(),
                        to: to_nodes.iter().cloned().collect(),
                    })
                } else {
                    None
                };
                (Command::Run, runner.clone(), filter)
            }
            CliCommand::Check => (Command::Check, None, None),
            CliCommand::Viz { port, output, export } => (
                Command::Viz {
                    port: *port,
                    output: output.clone(),
                    export: export.clone(),
                },
                None,
                None,
            ),
        }
    }

    impl<C: DeserializeOwned, P: DeserializeOwned>
        App<C, P, (), DefaultRunners>
    {
        /// Create an App from YAML catalog and params files.
        ///
        /// Loads and deserializes both files without any CLI parsing.
        /// Combine with [`with_args`](App::with_args) to add CLI subcommand
        /// dispatch and param overrides.
        ///
        /// # Example
        ///
        /// ```ignore
        /// let app = App::from_yaml("conf/catalog.yml", "conf/params.yml")?
        ///     .with_hooks(my_hooks)
        ///     .with_args(std::env::args_os())?;
        /// app.dispatch(my_pipeline)?;
        /// ```
        pub fn from_yaml(catalog_path: &str, params_path: &str) -> Result<Self, PondError> {
            let catalog: C = load_config(catalog_path, &[])?;
            let params: P = load_config(params_path, &[])?;
            Ok(App {
                catalog,
                params,
                hooks: (),
                runners: DefaultRunners::default(),
                command: Command::Run,
                runner_name: None,
                node_filter: None,
                program_name: String::new(),
            })
        }

        /// Create an App from pre-parsed [`CliArgs`].
        ///
        /// Loads catalog and params from YAML, applies CLI overrides,
        /// and stores the command for [`dispatch`](App::dispatch).
        pub fn from_cli(cli: CliArgs) -> Result<Self, PondError> {
            let catalog_path = cli
                .catalog_path
                .as_deref()
                .unwrap_or("conf/base/catalog.yml");
            let params_path = cli
                .params_path
                .as_deref()
                .unwrap_or("conf/base/parameters.yml");

            let (catalog_overrides, param_overrides) = match &cli.command {
                CliCommand::Run {
                    catalog_overrides,
                    param_overrides,
                    ..
                } => (catalog_overrides.as_slice(), param_overrides.as_slice()),
                _ => (&[][..], &[][..]),
            };

            let catalog: C = load_config(catalog_path, catalog_overrides)?;
            let params: P = load_config(params_path, param_overrides)?;

            let (command, runner_name, node_filter) = extract_command(&cli.command);

            Ok(App {
                catalog,
                params,
                hooks: (),
                runners: DefaultRunners::default(),
                command,
                runner_name,
                node_filter,
                program_name: String::new(),
            })
        }

        /// Create an App by parsing CLI args from an iterator.
        ///
        /// Extracts the program name from the first argument, then
        /// delegates to [`from_cli`](App::from_cli).
        pub fn from_args<I, T>(iter: I) -> Result<Self, PondError>
        where
            I: IntoIterator<Item = T>,
            T: Into<std::ffi::OsString> + Clone,
        {
            let raw_args: Vec<T> = iter.into_iter().collect();
            let program_name: String = raw_args
                .first()
                .map(|a| {
                    let os: std::ffi::OsString = a.clone().into();
                    std::path::Path::new(&os)
                        .file_stem()
                        .map(|s| s.to_string_lossy().into_owned())
                        .unwrap_or_else(|| os.to_string_lossy().into_owned())
                })
                .unwrap_or_default();
            let cli = CliArgs::parse_from(raw_args);
            let mut app = Self::from_cli(cli)?;
            app.program_name = program_name;
            Ok(app)
        }
    }

    impl<C, P, H, R> App<C, P, H, R>
    where
        C: Serialize + DeserializeOwned,
        P: Serialize + DeserializeOwned,
    {
        /// Apply CLI overrides to already-provided catalog and params.
        ///
        /// Serializes the current data, patches with CLI overrides,
        /// deserializes back. Stores the command for [`dispatch`](App::dispatch).
        pub fn with_cli(self, cli: CliArgs) -> Result<App<C, P, H, R>, PondError> {
            let (catalog_overrides, param_overrides) = match &cli.command {
                CliCommand::Run {
                    catalog_overrides,
                    param_overrides,
                    ..
                } => (catalog_overrides.as_slice(), param_overrides.as_slice()),
                _ => (&[][..], &[][..]),
            };

            let catalog = if catalog_overrides.is_empty() {
                self.catalog
            } else {
                let mut value = serde_yaml::to_value(&self.catalog)
                    .map_err(PondError::SerdeYaml)?;
                apply_overrides(&mut value, catalog_overrides);
                serde_yaml::from_value(value).map_err(PondError::SerdeYaml)?
            };

            let params = if param_overrides.is_empty() {
                self.params
            } else {
                let mut value = serde_yaml::to_value(&self.params)
                    .map_err(PondError::SerdeYaml)?;
                apply_overrides(&mut value, param_overrides);
                serde_yaml::from_value(value).map_err(PondError::SerdeYaml)?
            };

            let (command, runner_name, node_filter) = extract_command(&cli.command);

            Ok(App {
                catalog,
                params,
                hooks: self.hooks,
                runners: self.runners,
                command,
                runner_name,
                node_filter,
                program_name: self.program_name,
            })
        }
    }

    impl<C: Serialize, P: Serialize + DeserializeOwned, H, R> App<C, P, H, R> {
        /// Parse CLI args and apply command + param overrides to an existing App.
        ///
        /// Unlike [`with_cli`](App::with_cli), this does not require the catalog
        /// to be deserializable — only the params are round-tripped through serde
        /// when overrides are present. Catalog path and overrides are ignored.
        ///
        /// Use this when the catalog is constructed programmatically (e.g.
        /// `RegisterDataset`, `GpioDataset`) but you still want CLI-driven
        /// command selection (`run`, `check`, `viz`) and param overrides.
        pub fn with_args<I, T>(self, iter: I) -> Result<Self, PondError>
        where
            I: IntoIterator<Item = T>,
            T: Into<std::ffi::OsString> + Clone,
        {
            let raw_args: Vec<T> = iter.into_iter().collect();
            let program_name: String = raw_args
                .first()
                .map(|a| {
                    let os: std::ffi::OsString = a.clone().into();
                    std::path::Path::new(&os)
                        .file_stem()
                        .map(|s| s.to_string_lossy().into_owned())
                        .unwrap_or_else(|| os.to_string_lossy().into_owned())
                })
                .unwrap_or_default();
            let cli = CliArgs::parse_from(raw_args);

            let param_overrides = match &cli.command {
                CliCommand::Run { param_overrides, .. } => param_overrides.as_slice(),
                _ => &[][..],
            };

            let params = if param_overrides.is_empty() {
                self.params
            } else {
                let mut value = serde_yaml::to_value(&self.params)
                    .map_err(PondError::SerdeYaml)?;
                apply_overrides(&mut value, param_overrides);
                serde_yaml::from_value(value).map_err(PondError::SerdeYaml)?
            };

            let (command, runner_name, node_filter) = extract_command(&cli.command);

            Ok(App {
                catalog: self.catalog,
                params,
                hooks: self.hooks,
                runners: self.runners,
                command,
                runner_name,
                node_filter,
                program_name,
            })
        }
    }

    // --- Viz dispatch (std only) ---

    impl<C: Serialize, P: Serialize, H: Hooks, R: Runners> App<C, P, H, R> {
        pub(super) fn dispatch_viz<'a, E, F>(
            &'a self,
            f: F,
            port: u16,
            output: Option<&str>,
            export: Option<&str>,
        ) -> Result<(), E>
        where
            E: From<PondError> + core::fmt::Display + core::fmt::Debug + Send + Sync + 'static,
            F: PipelineFn<'a, C, P, E>,
        {
            let pipeline = f.call(&self.catalog, &self.params);
            let graph = build_pipeline_graph(&pipeline, &self.catalog, &self.params);

            #[cfg(not(feature = "viz"))]
            {
                let _ = (port, output, export, graph);
                Err(PondError::Custom(
                    "viz subcommand requires the 'viz' feature (cargo build --features viz)"
                        .into(),
                )
                .into())
            }

            #[cfg(feature = "viz")]
            {
                use crate::viz::serialization::{collect_dataset_meta, viz_graph_from};
                use crate::viz::assets::FrontendAssets;
                use std::collections::HashMap;

                let mut viz_graph = viz_graph_from(&graph);
                viz_graph.name = self.program_name.clone();
                let dataset_meta = collect_dataset_meta(&graph);

                if let Some(path) = export {
                    // Collect dataset HTML and YAML snapshots
                    let dataset_html: HashMap<usize, String> = dataset_meta.iter()
                        .filter_map(|(&id, meta)| meta.html().map(|h| (id, h)))
                        .collect();
                    let dataset_yaml: HashMap<usize, String> = dataset_meta.iter()
                        .filter_map(|(&id, meta)| meta.yaml().map(|y| (id, y)))
                        .collect();

                    let static_data = serde_json::json!({
                        "graph": viz_graph,
                        "datasetHtml": dataset_html,
                        "datasetYaml": dataset_yaml,
                    });
                    let json = serde_json::to_string(&static_data)
                        .map_err(PondError::from)?;
                    // Escape </script> in JSON to prevent breaking the HTML
                    let json = json.replace("</script", "<\\/script");

                    let template = FrontendAssets::get("index.html")
                        .ok_or_else(|| PondError::Custom("embedded index.html not found".into()))?;
                    let html = String::from_utf8_lossy(&template.data);
                    let script = std::format!(
                        "<script>window.__STATIC_DATA__={};</script>",
                        json
                    );
                    let html_str = html.as_ref();
                    let output_html = match html_str.rfind("</head>") {
                        Some(pos) => std::format!("{}{script}{}", &html_str[..pos], &html_str[pos..]),
                        None => std::format!("{script}{html_str}"),
                    };
                    std::fs::write(path, output_html.as_bytes()).map_err(PondError::from)?;
                    println!("Static HTML exported to {path}");
                    Ok(())
                } else if let Some(path) = output {
                    let json =
                        serde_json::to_string_pretty(&viz_graph).map_err(PondError::from)?;
                    std::fs::write(path, &json).map_err(PondError::from)?;
                    println!("Graph written to {path}");
                    Ok(())
                } else {
                    use crate::viz::server::VizState;
                    use std::sync::Mutex;
                    use tokio::sync::broadcast;

                    let (tx, _rx) = broadcast::channel(64);
                    let state = VizState {
                        graph: viz_graph,
                        dataset_meta,
                        node_statuses: Mutex::new(std::collections::HashMap::new()),
                        dataset_activity: Mutex::new(std::collections::HashMap::new()),
                        tx,
                    };
                    crate::viz::server::start_server(state, port);
                    Ok(())
                }
            }
        }
    }
}