## ----knitr-setup, include = FALSE-----------------------------------------------------------------
knitr::opts_chunk$set(
    comment = "#",
    prompt = FALSE,
    tidy = FALSE,
    cache = FALSE,
    collapse = TRUE
)

old <- options(width = 100L)

## ----stamped-class--------------------------------------------------------------------------------
library(pipeflow)

stamped <- function(x) structure(x, class = "stamped")

print.stamped <- function(x, ...) {
    cat("<stamped>", unclass(x), "\n")
    cat("  step:", attr(x, "step"), "\n")
    cat("  key :", attr(x, "key"), "\n")
    invisible(x)
}

## ----stamped-pipeline-----------------------------------------------------------------------------
p <- pip_new("report") |>
    pip_add("load", \(n = 3) stamped(seq_len(n))) |>
    pip_add("total", \(x = ~load) stamped(sum(x)))

pip_run(p, lgr = NULL)
p[["total", "out"]]

## ----stamped-method-------------------------------------------------------------------------------
pip_output_value.stamped <- function(x, ctx = list(), ...) {
    attr(x, "step") <- ctx[["step"]]
    attr(x, "key") <- ctx[["key"]]
    attr(x, "time") <- ctx[["time"]]
    x
}

## ----register-method------------------------------------------------------------------------------
registerS3method("pip_output_value", "stamped", pip_output_value.stamped)

## ----stamped-with-method--------------------------------------------------------------------------
pip_run(p, force = TRUE, lgr = NULL)
p[["load", "out"]]
p[["total", "out"]]

## ----stamped-time---------------------------------------------------------------------------------
identical(
    as.numeric(attr(p[["load", "out"]], "time")),
    as.numeric(p[["load", "time"]])
)

## ----downstream-----------------------------------------------------------------------------------
p2 <- pip_new("downstream") |>
    pip_add("make", \(v = 1) stamped(v)) |>
    pip_add("inspect", \(x = ~make) attr(x, "step"))

pip_run(p2, lgr = NULL)
p2[["inspect", "out"]]

## ----partitions-----------------------------------------------------------------------------------
p3 <- pip_new("partitions") |>
    pip_add(
        "split",
        \(x = 1) list(low = stamped(1), high = stamped(2)),
        exec = "split"
    ) |>
    pip_add("double", \(x = ~split) stamped(unclass(x) * 2))

pip_run(p3, lgr = NULL)
p3[["double", "out"]][["high"]]

## ----bundle-class---------------------------------------------------------------------------------
bundle <- function(...) structure(list(...), class = "bundle")

pip_output_value.bundle <- function(x, ctx = list(), ...) {
    x[] <- lapply(x, FUN = pip_output_value, ctx = ctx)
    x
}
registerS3method("pip_output_value", "bundle", pip_output_value.bundle)

p4 <- pip_new("bundle") |>
    pip_add("make", \(v = 1) bundle(a = stamped(v), b = stamped(v + 1)))

pip_run(p4, lgr = NULL)
p4[["make", "out"]][["b"]]

## ----checked-class--------------------------------------------------------------------------------
checked <- function(x) structure(x, class = "checked")

pip_output_value.checked <- function(x, ctx = list(), ...) {
    if (length(x) == 0L) {
        stop("empty output of step '", ctx[["step"]], "'")
    }
    x
}
registerS3method("pip_output_value", "checked", pip_output_value.checked)

p5 <- pip_new("errors") |>
    pip_add("empty", \(x = 1) checked(numeric(0))) |>
    pip_add("fine", \(x = 1) x + 1)

## ----method-error-stop----------------------------------------------------------------------------
tryCatch(
    pip_run(p5, lgr = NULL),
    error = \(e) conditionMessage(e)
)

## ----method-error-continue------------------------------------------------------------------------
suppressWarnings(
    pip_run(p5, lgr = NULL, on_error = "continue")
)

pip_data(p5)[, c("step", "state")]

## ----method-error-condition-----------------------------------------------------------------------
conditionMessage(p5[["empty", "out"]])

## ----include = FALSE----------------------------------------------------------
options(old)

