flowey_core/
node.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Core types and traits used to create and work with flowey nodes.
5
6mod github_context;
7mod spec;
8
9pub use github_context::GhOutput;
10pub use github_context::GhToRust;
11pub use github_context::RustToGh;
12
13use self::steps::ado::AdoRuntimeVar;
14use self::steps::ado::AdoStepServices;
15use self::steps::github::GhStepBuilder;
16use self::steps::rust::RustRuntimeServices;
17use self::user_facing::ClaimedGhParam;
18use self::user_facing::GhPermission;
19use self::user_facing::GhPermissionValue;
20use crate::node::github_context::GhContextVarReader;
21use github_context::state::Root;
22use serde::Deserialize;
23use serde::Serialize;
24use serde::de::DeserializeOwned;
25use std::cell::RefCell;
26use std::collections::BTreeMap;
27use std::path::PathBuf;
28use std::rc::Rc;
29use user_facing::GhParam;
30
31/// Node types which are considered "user facing", and re-exported in the
32/// `flowey` crate.
33pub mod user_facing {
34    pub use super::ClaimVar;
35    pub use super::ClaimedReadVar;
36    pub use super::ClaimedWriteVar;
37    pub use super::FlowArch;
38    pub use super::FlowBackend;
39    pub use super::FlowNode;
40    pub use super::FlowPlatform;
41    pub use super::FlowPlatformKind;
42    pub use super::GhUserSecretVar;
43    pub use super::ImportCtx;
44    pub use super::IntoRequest;
45    pub use super::NodeCtx;
46    pub use super::ReadVar;
47    pub use super::SideEffect;
48    pub use super::SimpleFlowNode;
49    pub use super::StepCtx;
50    pub use super::VarClaimed;
51    pub use super::VarEqBacking;
52    pub use super::VarNotClaimed;
53    pub use super::WriteVar;
54    pub use super::steps::ado::AdoResourcesRepositoryId;
55    pub use super::steps::ado::AdoRuntimeVar;
56    pub use super::steps::ado::AdoStepServices;
57    pub use super::steps::github::ClaimedGhParam;
58    pub use super::steps::github::GhParam;
59    pub use super::steps::github::GhPermission;
60    pub use super::steps::github::GhPermissionValue;
61    pub use super::steps::rust::RustRuntimeServices;
62    pub use crate::flowey_request;
63    pub use crate::new_flow_node;
64    pub use crate::new_simple_flow_node;
65    pub use crate::node::FlowPlatformLinuxDistro;
66    pub use crate::pipeline::Artifact;
67
68    /// Helper method to streamline request validation in cases where a value is
69    /// expected to be identical across all incoming requests.
70    ///
71    /// # Example: Request Aggregation Pattern
72    ///
73    /// When a node receives multiple requests, it often needs to ensure certain
74    /// values are consistent across all requests. This helper simplifies that pattern:
75    ///
76    /// ```rust,ignore
77    /// fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
78    ///     let mut version = None;
79    ///     let mut ensure_installed = Vec::new();
80    ///     
81    ///     for req in requests {
82    ///         match req {
83    ///             Request::Version(v) => {
84    ///                 // Ensure all requests agree on the version
85    ///                 same_across_all_reqs("Version", &mut version, v)?;
86    ///             }
87    ///             Request::EnsureInstalled(v) => {
88    ///                 ensure_installed.push(v);
89    ///             }
90    ///         }
91    ///     }
92    ///     
93    ///     let version = version.ok_or(anyhow::anyhow!("Missing required request: Version"))?;
94    ///     
95    ///     // ... emit steps using aggregated requests
96    ///     Ok(())
97    /// }
98    /// ```
99    pub fn same_across_all_reqs<T: PartialEq>(
100        req_name: &str,
101        var: &mut Option<T>,
102        new: T,
103    ) -> anyhow::Result<()> {
104        match (var.as_ref(), new) {
105            (None, v) => *var = Some(v),
106            (Some(old), new) => {
107                if *old != new {
108                    anyhow::bail!("`{}` must be consistent across requests", req_name);
109                }
110            }
111        }
112
113        Ok(())
114    }
115
116    /// Helper method to streamline request validation in cases where a value is
117    /// expected to be identical across all incoming requests, using a custom
118    /// comparison function.
119    pub fn same_across_all_reqs_backing_var<V: VarEqBacking>(
120        req_name: &str,
121        var: &mut Option<V>,
122        new: V,
123    ) -> anyhow::Result<()> {
124        match (var.as_ref(), new) {
125            (None, v) => *var = Some(v),
126            (Some(old), new) => {
127                if !old.eq(&new) {
128                    anyhow::bail!("`{}` must be consistent across requests", req_name);
129                }
130            }
131        }
132
133        Ok(())
134    }
135
136    /// Helper method to handle Linux distros that are supported only on one
137    /// host architecture.
138    /// match_arch!(var, arch, result)
139    #[macro_export]
140    macro_rules! match_arch {
141        ($host_arch:expr, $match_arch:pat, $expr:expr) => {
142            if matches!($host_arch, $match_arch) {
143                $expr
144            } else {
145                anyhow::bail!("Linux distro not supported on host arch {}", $host_arch);
146            }
147        };
148    }
149}
150
151/// Check if `ReadVar` / `WriteVar` instances are backed by the same underlying
152/// flowey Var.
153///
154/// # Why not use `Eq`? Why have a whole separate trait?
155///
156/// `ReadVar` and `WriteVar` are, in some sense, flowey's analog to
157/// "pointers", insofar as these types primary purpose is to mediate access to
158/// some contained value, as opposed to being "values" themselves.
159///
160/// Assuming you agree with this analogy, then we can apply the same logic to
161/// `ReadVar` and `WriteVar` as Rust does to `Box<T>` wrt. what the `Eq`
162/// implementation should mean.
163///
164/// Namely: `Eq` should check the equality of the _contained objects_, as
165/// opposed to the pointers themselves.
166///
167/// Unfortunately, unlike `Box<T>`, it is _impossible_ to have an `Eq` impl for
168/// `ReadVar` / `WriteVar` that checks contents for equality, due to the fact
169/// that these types exist at flow resolution time, whereas the values they
170/// contain only exist at flow runtime.
171///
172/// As such, we have a separate trait to perform different kinds of equality
173/// checks on Vars.
174pub trait VarEqBacking {
175    /// Check if `self` is backed by the same variable as `other`.
176    fn eq(&self, other: &Self) -> bool;
177}
178
179impl<T> VarEqBacking for WriteVar<T>
180where
181    T: Serialize + DeserializeOwned,
182{
183    fn eq(&self, other: &Self) -> bool {
184        self.backing_var == other.backing_var
185    }
186}
187
188impl<T> VarEqBacking for ReadVar<T>
189where
190    T: Serialize + DeserializeOwned + PartialEq + Eq + Clone,
191{
192    fn eq(&self, other: &Self) -> bool {
193        self.backing_var == other.backing_var
194    }
195}
196
197// TODO: this should be generic across all tuple sizes
198impl<T, U> VarEqBacking for (T, U)
199where
200    T: VarEqBacking,
201    U: VarEqBacking,
202{
203    fn eq(&self, other: &Self) -> bool {
204        (self.0.eq(&other.0)) && (self.1.eq(&other.1))
205    }
206}
207
208/// Type corresponding to a step which performs a side-effect,
209/// without returning a specific value.
210///
211/// e.g: A step responsible for installing a package from `apt` might claim a
212/// `WriteVar<SideEffect>`, with any step requiring the package to have been
213/// installed prior being able to claim the corresponding `ReadVar<SideEffect>.`
214pub type SideEffect = ();
215
216/// Uninhabited type used to denote that a particular [`WriteVar`] / [`ReadVar`]
217/// is not currently claimed by any step, and cannot be directly accessed.
218#[derive(Clone, Debug, Serialize, Deserialize)]
219pub enum VarNotClaimed {}
220
221/// Uninhabited type used to denote that a particular [`WriteVar`] / [`ReadVar`]
222/// is currently claimed by a step, and can be read/written to.
223#[derive(Clone, Debug, Serialize, Deserialize)]
224pub enum VarClaimed {}
225
226/// Write a value into a flowey Var at runtime, which can then be read via a
227/// corresponding [`ReadVar`].
228///
229/// Vars in flowey must be serde de/serializable, in order to be de/serialized
230/// between multiple steps/nodes.
231///
232/// In order to write a value into a `WriteVar`, it must first be _claimed_ by a
233/// particular step (using the [`ClaimVar::claim`] API). Once claimed, the Var
234/// can be written to using APIs such as [`RustRuntimeServices::write`], or
235/// [`AdoStepServices::set_var`]
236///
237/// Note that it is only possible to write a value into a `WriteVar` _once_.
238/// Once the value has been written, the `WriteVar` type is immediately
239/// consumed, making it impossible to overwrite the stored value at some later
240/// point in execution.
241///
242/// This "write-once" property is foundational to flowey's execution model, as
243/// by recoding what step wrote to a Var, and what step(s) read from the Var, it
244/// is possible to infer what order steps must be run in.
245#[derive(Debug, Serialize, Deserialize)]
246pub struct WriteVar<T: Serialize + DeserializeOwned, C = VarNotClaimed> {
247    backing_var: String,
248    /// If true, then readers on this var expect to read a side effect (`()`)
249    /// and not `T`.
250    is_side_effect: bool,
251
252    #[serde(skip)]
253    _kind: core::marker::PhantomData<(T, C)>,
254}
255
256/// A [`WriteVar`] which has been claimed by a particular step, allowing it
257/// to be written to at runtime.
258pub type ClaimedWriteVar<T> = WriteVar<T, VarClaimed>;
259
260impl<T: Serialize + DeserializeOwned> WriteVar<T, VarNotClaimed> {
261    /// (Internal API) Switch the claim marker to "claimed".
262    fn into_claimed(self) -> WriteVar<T, VarClaimed> {
263        let Self {
264            backing_var,
265            is_side_effect,
266            _kind,
267        } = self;
268
269        WriteVar {
270            backing_var,
271            is_side_effect,
272            _kind: std::marker::PhantomData,
273        }
274    }
275
276    /// Write a static value into the Var.
277    #[track_caller]
278    pub fn write_static(self, ctx: &mut NodeCtx<'_>, val: T)
279    where
280        T: 'static,
281    {
282        let val = ReadVar::from_static(val);
283        val.write_into(ctx, self, |v| v);
284    }
285
286    pub(crate) fn into_json(self) -> WriteVar<serde_json::Value> {
287        WriteVar {
288            backing_var: self.backing_var,
289            is_side_effect: self.is_side_effect,
290            _kind: std::marker::PhantomData,
291        }
292    }
293}
294
295impl WriteVar<SideEffect, VarNotClaimed> {
296    /// Transforms this writer into one that can be used to write a `T`.
297    ///
298    /// This is useful when a reader only cares about the side effect of an
299    /// operation, but the writer wants to provide output as well.
300    pub fn discard_result<T: Serialize + DeserializeOwned>(self) -> WriteVar<T> {
301        WriteVar {
302            backing_var: self.backing_var,
303            is_side_effect: true,
304            _kind: std::marker::PhantomData,
305        }
306    }
307}
308
309/// Claim one or more flowey Vars for a particular step.
310///
311/// By having this be a trait, it is possible to `claim` both single instances
312/// of `ReadVar` / `WriteVar`, as well as whole _collections_ of Vars.
313//
314// FUTURE: flowey should include a derive macro for easily claiming read/write
315// vars in user-defined structs / enums.
316pub trait ClaimVar {
317    /// The claimed version of Self.
318    type Claimed;
319    /// Claim the Var for this step, allowing it to be accessed at runtime.
320    fn claim(self, ctx: &mut StepCtx<'_>) -> Self::Claimed;
321}
322
323/// Read the value of one or more flowey Vars.
324///
325/// By having this be a trait, it is possible to `read` both single
326/// instances of `ReadVar` / `WriteVar`, as well as whole _collections_ of
327/// Vars.
328pub trait ReadVarValue {
329    /// The read value of Self.
330    type Value;
331    /// Read the value of the Var at runtime.
332    fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value;
333}
334
335impl<T: Serialize + DeserializeOwned> ClaimVar for ReadVar<T> {
336    type Claimed = ClaimedReadVar<T>;
337
338    fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedReadVar<T> {
339        if let ReadVarBacking::RuntimeVar {
340            var,
341            is_side_effect: _,
342        } = &self.backing_var
343        {
344            ctx.backend.borrow_mut().on_claimed_runtime_var(var, true);
345        }
346        self.into_claimed()
347    }
348}
349
350impl<T: Serialize + DeserializeOwned> ClaimVar for WriteVar<T> {
351    type Claimed = ClaimedWriteVar<T>;
352
353    fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedWriteVar<T> {
354        ctx.backend
355            .borrow_mut()
356            .on_claimed_runtime_var(&self.backing_var, false);
357        self.into_claimed()
358    }
359}
360
361impl<T: Serialize + DeserializeOwned> ReadVarValue for ClaimedReadVar<T> {
362    type Value = T;
363
364    fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
365        match self.backing_var {
366            ReadVarBacking::RuntimeVar {
367                var,
368                is_side_effect,
369            } => {
370                // Always get the data to validate that the variable is actually there.
371                let data = rt.get_var(&var, is_side_effect);
372                if is_side_effect {
373                    // This was converted into a `ReadVar<SideEffect>` from
374                    // another type, so parse the value that a
375                    // `WriteVar<SideEffect>` would have written.
376                    serde_json::from_slice(b"null").expect("should be deserializing into ()")
377                } else {
378                    // This is a normal variable.
379                    serde_json::from_slice(&data).expect("improve this error path")
380                }
381            }
382            ReadVarBacking::Inline(val) => val,
383        }
384    }
385}
386
387impl<T: ClaimVar> ClaimVar for Vec<T> {
388    type Claimed = Vec<T::Claimed>;
389
390    fn claim(self, ctx: &mut StepCtx<'_>) -> Vec<T::Claimed> {
391        self.into_iter().map(|v| v.claim(ctx)).collect()
392    }
393}
394
395impl<T: ReadVarValue> ReadVarValue for Vec<T> {
396    type Value = Vec<T::Value>;
397
398    fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
399        self.into_iter().map(|v| v.read_value(rt)).collect()
400    }
401}
402
403impl<T: ClaimVar> ClaimVar for Option<T> {
404    type Claimed = Option<T::Claimed>;
405
406    fn claim(self, ctx: &mut StepCtx<'_>) -> Option<T::Claimed> {
407        self.map(|x| x.claim(ctx))
408    }
409}
410
411impl<T: ReadVarValue> ReadVarValue for Option<T> {
412    type Value = Option<T::Value>;
413
414    fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
415        self.map(|x| x.read_value(rt))
416    }
417}
418
419impl<U: Ord, T: ClaimVar> ClaimVar for BTreeMap<U, T> {
420    type Claimed = BTreeMap<U, T::Claimed>;
421
422    fn claim(self, ctx: &mut StepCtx<'_>) -> BTreeMap<U, T::Claimed> {
423        self.into_iter().map(|(k, v)| (k, v.claim(ctx))).collect()
424    }
425}
426
427impl<U: Ord, T: ReadVarValue> ReadVarValue for BTreeMap<U, T> {
428    type Value = BTreeMap<U, T::Value>;
429
430    fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
431        self.into_iter()
432            .map(|(k, v)| (k, v.read_value(rt)))
433            .collect()
434    }
435}
436
437macro_rules! impl_tuple_claim {
438    ($($T:tt)*) => {
439        impl<$($T,)*> $crate::node::ClaimVar for ($($T,)*)
440        where
441            $($T: $crate::node::ClaimVar,)*
442        {
443            type Claimed = ($($T::Claimed,)*);
444
445            #[expect(non_snake_case)]
446            fn claim(self, ctx: &mut $crate::node::StepCtx<'_>) -> Self::Claimed {
447                let ($($T,)*) = self;
448                ($($T.claim(ctx),)*)
449            }
450        }
451
452        impl<$($T,)*> $crate::node::ReadVarValue for ($($T,)*)
453        where
454            $($T: $crate::node::ReadVarValue,)*
455        {
456            type Value = ($($T::Value,)*);
457
458            #[expect(non_snake_case)]
459            fn read_value(self, rt: &mut $crate::node::RustRuntimeServices<'_>) -> Self::Value {
460                let ($($T,)*) = self;
461                ($($T.read_value(rt),)*)
462            }
463        }
464    };
465}
466
467impl_tuple_claim!(A B C D E F G H I J);
468impl_tuple_claim!(A B C D E F G H I);
469impl_tuple_claim!(A B C D E F G H);
470impl_tuple_claim!(A B C D E F G);
471impl_tuple_claim!(A B C D E F);
472impl_tuple_claim!(A B C D E);
473impl_tuple_claim!(A B C D);
474impl_tuple_claim!(A B C);
475impl_tuple_claim!(A B);
476impl_tuple_claim!(A);
477
478impl ClaimVar for () {
479    type Claimed = ();
480
481    fn claim(self, _ctx: &mut StepCtx<'_>) -> Self::Claimed {}
482}
483
484impl ReadVarValue for () {
485    type Value = ();
486
487    fn read_value(self, _rt: &mut RustRuntimeServices<'_>) -> Self::Value {}
488}
489
490/// Read a custom, user-defined secret by passing in the secret name.
491///
492/// Intended usage is to get a secret using the [`crate::pipeline::Pipeline::gh_use_secret`] API
493/// and to use the returned value through the [`NodeCtx::get_gh_context_var`] API.
494#[derive(Serialize, Deserialize, Clone)]
495pub struct GhUserSecretVar(pub(crate) String);
496
497/// Read a value from a flowey Var at runtime, returning the value written by
498/// the Var's corresponding [`WriteVar`].
499///
500/// Vars in flowey must be serde de/serializable, in order to be de/serialized
501/// between multiple steps/nodes.
502///
503/// In order to read the value contained within a `ReadVar`, it must first be
504/// _claimed_ by a particular step (using the [`ClaimVar::claim`] API). Once
505/// claimed, the Var can be read using APIs such as
506/// [`RustRuntimeServices::read`], or [`AdoStepServices::get_var`]
507///
508/// Note that all `ReadVar`s in flowey are _immutable_. In other words:
509/// reading the value of a `ReadVar` multiple times from multiple nodes will
510/// _always_ return the same value.
511///
512/// This is a natural consequence `ReadVar` obtaining its value from the result
513/// of a write into [`WriteVar`], whose API enforces that there can only ever be
514/// a single Write to a `WriteVar`.
515#[derive(Debug, Serialize, Deserialize)]
516pub struct ReadVar<T, C = VarNotClaimed> {
517    backing_var: ReadVarBacking<T>,
518    #[serde(skip)]
519    _kind: std::marker::PhantomData<C>,
520}
521
522/// A [`ReadVar`] which has been claimed by a particular step, allowing it to
523/// be read at runtime.
524pub type ClaimedReadVar<T> = ReadVar<T, VarClaimed>;
525
526// cloning is fine, since you can totally have multiple dependents
527impl<T: Serialize + DeserializeOwned, C> Clone for ReadVar<T, C> {
528    fn clone(&self) -> Self {
529        ReadVar {
530            backing_var: self.backing_var.clone(),
531            _kind: std::marker::PhantomData,
532        }
533    }
534}
535
536#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
537enum ReadVarBacking<T> {
538    RuntimeVar {
539        var: String,
540        /// If true, then don't try to parse this variable--it was converted
541        /// into a side effect (of type `()`) from another type, so the
542        /// serialization will not match.
543        ///
544        /// If false, it may still be a "side effect" variable, but type `T`
545        /// matches its serialization.
546        is_side_effect: bool,
547    },
548    Inline(T),
549}
550
551// avoid requiring types to include an explicit clone bound
552impl<T: Serialize + DeserializeOwned> Clone for ReadVarBacking<T> {
553    fn clone(&self) -> Self {
554        match self {
555            Self::RuntimeVar {
556                var,
557                is_side_effect,
558            } => Self::RuntimeVar {
559                var: var.clone(),
560                is_side_effect: *is_side_effect,
561            },
562            Self::Inline(v) => {
563                Self::Inline(serde_json::from_value(serde_json::to_value(v).unwrap()).unwrap())
564            }
565        }
566    }
567}
568
569impl<T: Serialize + DeserializeOwned> ReadVar<T> {
570    /// (Internal API) Switch the claim marker to "claimed".
571    fn into_claimed(self) -> ReadVar<T, VarClaimed> {
572        let Self { backing_var, _kind } = self;
573
574        ReadVar {
575            backing_var,
576            _kind: std::marker::PhantomData,
577        }
578    }
579
580    /// Discard any type information associated with the Var, and treat the Var
581    /// as through it was only a side effect.
582    ///
583    /// e.g: if a Node returns a `ReadVar<PathBuf>`, but you know that the mere
584    /// act of having _run_ the node has ensured the file is placed in a "magic
585    /// location" for some other node, then it may be useful to treat the
586    /// `ReadVar<PathBuf>` as a simple `ReadVar<SideEffect>`, which can be
587    /// passed along as part of a larger bundle of `Vec<ReadVar<SideEffect>>`.
588    #[must_use]
589    pub fn into_side_effect(self) -> ReadVar<SideEffect> {
590        ReadVar {
591            backing_var: match self.backing_var {
592                ReadVarBacking::RuntimeVar {
593                    var,
594                    is_side_effect: _,
595                } => ReadVarBacking::RuntimeVar {
596                    var,
597                    is_side_effect: true,
598                },
599                ReadVarBacking::Inline(_) => ReadVarBacking::Inline(()),
600            },
601            _kind: std::marker::PhantomData,
602        }
603    }
604
605    /// Maps a `ReadVar<T>` to a new `ReadVar<U>`, by applying a function to the
606    /// Var at runtime.
607    #[track_caller]
608    #[must_use]
609    pub fn map<F, U>(&self, ctx: &mut NodeCtx<'_>, f: F) -> ReadVar<U>
610    where
611        T: 'static,
612        U: Serialize + DeserializeOwned + 'static,
613        F: FnOnce(T) -> U + 'static,
614    {
615        let (read_from, write_into) = ctx.new_var();
616        self.write_into(ctx, write_into, f);
617        read_from
618    }
619
620    /// Maps a `ReadVar<T>` into an existing `WriteVar<U>` by applying a
621    /// function to the Var at runtime.
622    #[track_caller]
623    pub fn write_into<F, U>(&self, ctx: &mut NodeCtx<'_>, write_into: WriteVar<U>, f: F)
624    where
625        T: 'static,
626        U: Serialize + DeserializeOwned + 'static,
627        F: FnOnce(T) -> U + 'static,
628    {
629        let this = self.clone();
630        ctx.emit_minor_rust_step("🌼 write_into Var", move |ctx| {
631            let this = this.claim(ctx);
632            let write_into = write_into.claim(ctx);
633            move |rt| {
634                let this = rt.read(this);
635                rt.write(write_into, &f(this));
636            }
637        });
638    }
639
640    /// Zips self (`ReadVar<T>`) with another `ReadVar<U>`, returning a new
641    /// `ReadVar<(T, U)>`
642    #[track_caller]
643    #[must_use]
644    pub fn zip<U>(&self, ctx: &mut NodeCtx<'_>, other: ReadVar<U>) -> ReadVar<(T, U)>
645    where
646        T: 'static,
647        U: Serialize + DeserializeOwned + 'static,
648    {
649        let (read_from, write_into) = ctx.new_var();
650        let this = self.clone();
651        ctx.emit_minor_rust_step("🌼 Zip Vars", move |ctx| {
652            let this = this.claim(ctx);
653            let other = other.claim(ctx);
654            let write_into = write_into.claim(ctx);
655            move |rt| {
656                let this = rt.read(this);
657                let other = rt.read(other);
658                rt.write(write_into, &(this, other));
659            }
660        });
661        read_from
662    }
663
664    /// Create a new `ReadVar` from a static value.
665    ///
666    /// **WARNING:** Static values **CANNOT BE SECRETS**, as they are encoded as
667    /// plain-text in the output flow.
668    #[track_caller]
669    #[must_use]
670    pub fn from_static(val: T) -> ReadVar<T>
671    where
672        T: 'static,
673    {
674        ReadVar {
675            backing_var: ReadVarBacking::Inline(val),
676            _kind: std::marker::PhantomData,
677        }
678    }
679
680    /// If this [`ReadVar`] contains a static value, return it.
681    ///
682    /// Nodes can opt-in to using this method as a way to generate optimized
683    /// steps in cases where the value of a variable is known ahead of time.
684    ///
685    /// e.g: a node doing a git checkout could leverage this method to decide
686    /// whether its ADO backend should emit a conditional step for checking out
687    /// a repo, or if it can statically include / exclude the checkout request.
688    pub fn get_static(&self) -> Option<T> {
689        match self.clone().backing_var {
690            ReadVarBacking::Inline(v) => Some(v),
691            _ => None,
692        }
693    }
694
695    /// Transpose a `Vec<ReadVar<T>>` into a `ReadVar<Vec<T>>`
696    #[track_caller]
697    #[must_use]
698    pub fn transpose_vec(ctx: &mut NodeCtx<'_>, vec: Vec<ReadVar<T>>) -> ReadVar<Vec<T>>
699    where
700        T: 'static,
701    {
702        let (read_from, write_into) = ctx.new_var();
703        ctx.emit_minor_rust_step("🌼 Transpose Vec<ReadVar<T>>", move |ctx| {
704            let vec = vec.claim(ctx);
705            let write_into = write_into.claim(ctx);
706            move |rt| {
707                let mut v = Vec::new();
708                for var in vec {
709                    v.push(rt.read(var));
710                }
711                rt.write(write_into, &v);
712            }
713        });
714        read_from
715    }
716
717    /// Returns a new instance of this variable with an artificial dependency on
718    /// `other`.
719    ///
720    /// This is useful for making explicit a non-explicit dependency between the
721    /// two variables. For example, if `self` contains a path to a file, and
722    /// `other` is only written once that file has been created, then this
723    /// method can be used to return a new `ReadVar` which depends on `other`
724    /// but is otherwise identical to `self`. This ensures that when the new
725    /// variable is read, the file has been created.
726    ///
727    /// In general, it is better to ensure that the dependency is explicit, so
728    /// that if you have a variable with a path, then you know that the file
729    /// exists when you read it. This method is useful in cases where this is
730    /// not naturally the case, e.g., when you are providing a path as part of a
731    /// request, as opposed to the path being returned to you.
732    #[must_use]
733    pub fn depending_on<U>(&self, ctx: &mut NodeCtx<'_>, other: &ReadVar<U>) -> Self
734    where
735        T: 'static,
736        U: Serialize + DeserializeOwned + 'static,
737    {
738        // This could probably be handled without an additional Rust step with some
739        // additional work in the backend, but this is simple enough for now.
740        ctx.emit_minor_rust_stepv("🌼 Add dependency", |ctx| {
741            let this = self.clone().claim(ctx);
742            other.clone().claim(ctx);
743            move |rt| rt.read(this)
744        })
745    }
746
747    /// Consume this `ReadVar` outside the context of a step, signalling that it
748    /// won't be used.
749    pub fn claim_unused(self, ctx: &mut NodeCtx<'_>) {
750        match self.backing_var {
751            ReadVarBacking::RuntimeVar {
752                var,
753                is_side_effect: _,
754            } => ctx.backend.borrow_mut().on_unused_read_var(&var),
755            ReadVarBacking::Inline(_) => {}
756        }
757    }
758
759    pub(crate) fn into_json(self) -> ReadVar<serde_json::Value> {
760        match self.backing_var {
761            ReadVarBacking::RuntimeVar {
762                var,
763                is_side_effect,
764            } => ReadVar {
765                backing_var: ReadVarBacking::RuntimeVar {
766                    var,
767                    is_side_effect,
768                },
769                _kind: std::marker::PhantomData,
770            },
771            ReadVarBacking::Inline(v) => ReadVar {
772                backing_var: ReadVarBacking::Inline(serde_json::to_value(v).unwrap()),
773                _kind: std::marker::PhantomData,
774            },
775        }
776    }
777}
778
779/// DANGER: obtain a handle to a [`ReadVar`] "out of thin air".
780///
781/// This should NEVER be used from within a flowey node. This is a sharp tool,
782/// and should only be used by code implementing flow / pipeline resolution
783/// logic.
784#[must_use]
785pub fn thin_air_read_runtime_var<T>(backing_var: String) -> ReadVar<T>
786where
787    T: Serialize + DeserializeOwned,
788{
789    ReadVar {
790        backing_var: ReadVarBacking::RuntimeVar {
791            var: backing_var,
792            is_side_effect: false,
793        },
794        _kind: std::marker::PhantomData,
795    }
796}
797
798/// DANGER: obtain a handle to a [`WriteVar`] "out of thin air".
799///
800/// This should NEVER be used from within a flowey node. This is a sharp tool,
801/// and should only be used by code implementing flow / pipeline resolution
802/// logic.
803#[must_use]
804pub fn thin_air_write_runtime_var<T>(backing_var: String) -> WriteVar<T>
805where
806    T: Serialize + DeserializeOwned,
807{
808    WriteVar {
809        backing_var,
810        is_side_effect: false,
811        _kind: std::marker::PhantomData,
812    }
813}
814
815/// DANGER: obtain a [`ReadVar`] backing variable and side effect status.
816///
817/// This should NEVER be used from within a flowey node. This relies on
818/// flowey variable implementation details, and should only be used by code
819/// implementing flow / pipeline resolution logic.
820pub fn read_var_internals<T: Serialize + DeserializeOwned, C>(
821    var: &ReadVar<T, C>,
822) -> (Option<String>, bool) {
823    match var.backing_var {
824        ReadVarBacking::RuntimeVar {
825            var: ref s,
826            is_side_effect,
827        } => (Some(s.clone()), is_side_effect),
828        ReadVarBacking::Inline(_) => (None, false),
829    }
830}
831
832pub trait ImportCtxBackend {
833    fn on_possible_dep(&mut self, node_handle: NodeHandle);
834}
835
836/// Context passed to [`FlowNode::imports`].
837pub struct ImportCtx<'a> {
838    backend: &'a mut dyn ImportCtxBackend,
839}
840
841impl ImportCtx<'_> {
842    /// Declare that a Node can be referenced in [`FlowNode::emit`]
843    pub fn import<N: FlowNodeBase + 'static>(&mut self) {
844        self.backend.on_possible_dep(NodeHandle::from_type::<N>())
845    }
846}
847
848pub fn new_import_ctx(backend: &mut dyn ImportCtxBackend) -> ImportCtx<'_> {
849    ImportCtx { backend }
850}
851
852#[derive(Debug)]
853pub enum CtxAnchor {
854    PostJob,
855}
856
857pub trait NodeCtxBackend {
858    /// Handle to the current node this `ctx` corresponds to
859    fn current_node(&self) -> NodeHandle;
860
861    /// Return a string which uniquely identifies this particular Var
862    /// registration.
863    ///
864    /// Typically consists of `{current node handle}{ordinal}`
865    fn on_new_var(&mut self) -> String;
866
867    /// Invoked when a node claims a particular runtime variable
868    fn on_claimed_runtime_var(&mut self, var: &str, is_read: bool);
869
870    /// Invoked when a node marks a particular runtime variable as unused
871    fn on_unused_read_var(&mut self, var: &str);
872
873    /// Invoked when a node sets a request on a node.
874    ///
875    /// - `node_typeid` will always correspond to a node that was previously
876    ///   passed to `on_register`.
877    /// - `req` may be an error, in the case where the NodeCtx failed to
878    ///   serialize the provided request.
879    // FIXME: this should be using type-erased serde
880    fn on_request(&mut self, node_handle: NodeHandle, req: anyhow::Result<Box<[u8]>>);
881
882    fn on_emit_rust_step(
883        &mut self,
884        label: &str,
885        can_merge: bool,
886        code: Box<dyn for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()>>,
887    );
888
889    fn on_emit_ado_step(
890        &mut self,
891        label: &str,
892        yaml_snippet: Box<dyn for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String>,
893        inline_script: Option<
894            Box<dyn for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()>>,
895        >,
896        condvar: Option<String>,
897    );
898
899    fn on_emit_gh_step(
900        &mut self,
901        label: &str,
902        uses: &str,
903        with: BTreeMap<String, ClaimedGhParam>,
904        condvar: Option<String>,
905        outputs: BTreeMap<String, Vec<GhOutput>>,
906        permissions: BTreeMap<GhPermission, GhPermissionValue>,
907        gh_to_rust: Vec<GhToRust>,
908        rust_to_gh: Vec<RustToGh>,
909    );
910
911    fn on_emit_side_effect_step(&mut self);
912
913    fn backend(&mut self) -> FlowBackend;
914    fn platform(&mut self) -> FlowPlatform;
915    fn arch(&mut self) -> FlowArch;
916
917    /// Return a node-specific persistent store path. The backend does not need
918    /// to ensure that the path exists - flowey will automatically emit a step
919    /// to construct the directory at runtime.
920    fn persistent_dir_path_var(&mut self) -> Option<String>;
921}
922
923pub fn new_node_ctx(backend: &mut dyn NodeCtxBackend) -> NodeCtx<'_> {
924    NodeCtx {
925        backend: Rc::new(RefCell::new(backend)),
926    }
927}
928
929/// What backend the flow is being running on.
930#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
931pub enum FlowBackend {
932    /// Running locally.
933    Local,
934    /// Running on ADO.
935    Ado,
936    /// Running on GitHub Actions
937    Github,
938}
939
940/// The kind platform the flow is being running on, Windows or Unix.
941#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
942pub enum FlowPlatformKind {
943    Windows,
944    Unix,
945}
946
947/// The kind platform the flow is being running on, Windows or Unix.
948#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
949pub enum FlowPlatformLinuxDistro {
950    /// Fedora (including WSL2)
951    Fedora,
952    /// Ubuntu (including WSL2)
953    Ubuntu,
954    /// Arch Linux (including WSL2)
955    Arch,
956    /// An unknown distribution
957    Unknown,
958}
959
960/// What platform the flow is being running on.
961#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
962#[non_exhaustive]
963pub enum FlowPlatform {
964    /// Windows
965    Windows,
966    /// Linux (including WSL2)
967    Linux(FlowPlatformLinuxDistro),
968    /// macOS
969    MacOs,
970}
971
972impl FlowPlatform {
973    pub fn kind(&self) -> FlowPlatformKind {
974        match self {
975            Self::Windows => FlowPlatformKind::Windows,
976            Self::Linux(_) | Self::MacOs => FlowPlatformKind::Unix,
977        }
978    }
979
980    fn as_str(&self) -> &'static str {
981        match self {
982            Self::Windows => "windows",
983            Self::Linux(_) => "linux",
984            Self::MacOs => "macos",
985        }
986    }
987
988    /// The suffix to use for executables on this platform.
989    pub fn exe_suffix(&self) -> &'static str {
990        if self == &Self::Windows { ".exe" } else { "" }
991    }
992
993    /// The full name for a binary on this platform (i.e. `name + self.exe_suffix()`).
994    pub fn binary(&self, name: &str) -> String {
995        format!("{}{}", name, self.exe_suffix())
996    }
997}
998
999impl std::fmt::Display for FlowPlatform {
1000    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1001        f.pad(self.as_str())
1002    }
1003}
1004
1005/// What architecture the flow is being running on.
1006#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1007#[non_exhaustive]
1008pub enum FlowArch {
1009    X86_64,
1010    Aarch64,
1011}
1012
1013impl FlowArch {
1014    fn as_str(&self) -> &'static str {
1015        match self {
1016            Self::X86_64 => "x86_64",
1017            Self::Aarch64 => "aarch64",
1018        }
1019    }
1020}
1021
1022impl std::fmt::Display for FlowArch {
1023    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1024        f.pad(self.as_str())
1025    }
1026}
1027
1028/// Context object for an individual step.
1029pub struct StepCtx<'a> {
1030    backend: Rc<RefCell<&'a mut dyn NodeCtxBackend>>,
1031}
1032
1033impl StepCtx<'_> {
1034    /// What backend the flow is being running on (e.g: locally, ADO, GitHub,
1035    /// etc...)
1036    pub fn backend(&self) -> FlowBackend {
1037        self.backend.borrow_mut().backend()
1038    }
1039
1040    /// What platform the flow is being running on (e.g: windows, linux, wsl2,
1041    /// etc...).
1042    pub fn platform(&self) -> FlowPlatform {
1043        self.backend.borrow_mut().platform()
1044    }
1045}
1046
1047const NO_ADO_INLINE_SCRIPT: Option<
1048    for<'a> fn(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()>,
1049> = None;
1050
1051/// Context object for a `FlowNode`.
1052pub struct NodeCtx<'a> {
1053    backend: Rc<RefCell<&'a mut dyn NodeCtxBackend>>,
1054}
1055
1056impl<'ctx> NodeCtx<'ctx> {
1057    /// Emit a Rust-based step.
1058    ///
1059    /// As a convenience feature, this function returns a special _optional_
1060    /// [`ReadVar<SideEffect>`], which will not result in a "unused variable"
1061    /// error if no subsequent step ends up claiming it.
1062    pub fn emit_rust_step<F, G>(&mut self, label: impl AsRef<str>, code: F) -> ReadVar<SideEffect>
1063    where
1064        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1065        G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1066    {
1067        self.emit_rust_step_inner(label.as_ref(), false, code)
1068    }
1069
1070    /// Emit a Rust-based step that cannot fail.
1071    ///
1072    /// This is equivalent to `emit_rust_step`, but it is for steps that cannot
1073    /// fail and that do not need to be emitted as a separate step in a YAML
1074    /// pipeline. This simplifies the pipeline logs.
1075    ///
1076    /// As a convenience feature, this function returns a special _optional_
1077    /// [`ReadVar<SideEffect>`], which will not result in a "unused variable"
1078    /// error if no subsequent step ends up claiming it.
1079    pub fn emit_minor_rust_step<F, G>(
1080        &mut self,
1081        label: impl AsRef<str>,
1082        code: F,
1083    ) -> ReadVar<SideEffect>
1084    where
1085        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1086        G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) + 'static,
1087    {
1088        self.emit_rust_step_inner(label.as_ref(), true, |ctx| {
1089            let f = code(ctx);
1090            |rt| {
1091                f(rt);
1092                Ok(())
1093            }
1094        })
1095    }
1096
1097    /// Emit a Rust-based step, creating a new `ReadVar<T>` from the step's
1098    /// return value.
1099    ///
1100    /// This is a convenience function that streamlines the following common
1101    /// flowey pattern:
1102    ///
1103    /// ```ignore
1104    /// // creating a new Var explicitly
1105    /// let (read_foo, write_foo) = ctx.new_var();
1106    /// ctx.emit_rust_step("foo", |ctx| {
1107    ///     let write_foo = write_foo.claim(ctx);
1108    ///     |rt| {
1109    ///         rt.write(write_foo, &get_foo());
1110    ///         Ok(())
1111    ///     }
1112    /// });
1113    ///
1114    /// // creating a new Var automatically
1115    /// let read_foo = ctx.emit_rust_stepv("foo", |ctx| |rt| Ok(get_foo()));
1116    /// ```
1117    #[must_use]
1118    #[track_caller]
1119    pub fn emit_rust_stepv<T, F, G>(&mut self, label: impl AsRef<str>, code: F) -> ReadVar<T>
1120    where
1121        T: Serialize + DeserializeOwned + 'static,
1122        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1123        G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<T> + 'static,
1124    {
1125        self.emit_rust_stepv_inner(label.as_ref(), false, code)
1126    }
1127
1128    /// Emit a Rust-based step, creating a new `ReadVar<T>` from the step's
1129    /// return value.
1130    ///
1131    /// This is equivalent to `emit_rust_stepv`, but it is for steps that cannot
1132    /// fail and that do not need to be emitted as a separate step in a YAML
1133    /// pipeline. This simplifies the pipeline logs.
1134    ///
1135    /// This is a convenience function that streamlines the following common
1136    /// flowey pattern:
1137    ///
1138    /// ```ignore
1139    /// // creating a new Var explicitly
1140    /// let (read_foo, write_foo) = ctx.new_var();
1141    /// ctx.emit_minor_rust_step("foo", |ctx| {
1142    ///     let write_foo = write_foo.claim(ctx);
1143    ///     |rt| {
1144    ///         rt.write(write_foo, &get_foo());
1145    ///     }
1146    /// });
1147    ///
1148    /// // creating a new Var automatically
1149    /// let read_foo = ctx.emit_minor_rust_stepv("foo", |ctx| |rt| get_foo());
1150    /// ```
1151    #[must_use]
1152    #[track_caller]
1153    pub fn emit_minor_rust_stepv<T, F, G>(&mut self, label: impl AsRef<str>, code: F) -> ReadVar<T>
1154    where
1155        T: Serialize + DeserializeOwned + 'static,
1156        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1157        G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> T + 'static,
1158    {
1159        self.emit_rust_stepv_inner(label.as_ref(), true, |ctx| {
1160            let f = code(ctx);
1161            |rt| Ok(f(rt))
1162        })
1163    }
1164
1165    fn emit_rust_step_inner<F, G>(
1166        &mut self,
1167        label: &str,
1168        can_merge: bool,
1169        code: F,
1170    ) -> ReadVar<SideEffect>
1171    where
1172        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1173        G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1174    {
1175        let (read, write) = self.new_prefixed_var("auto_se");
1176
1177        let ctx = &mut StepCtx {
1178            backend: self.backend.clone(),
1179        };
1180        write.claim(ctx);
1181
1182        let code = code(ctx);
1183        self.backend
1184            .borrow_mut()
1185            .on_emit_rust_step(label.as_ref(), can_merge, Box::new(code));
1186        read
1187    }
1188
1189    #[must_use]
1190    #[track_caller]
1191    fn emit_rust_stepv_inner<T, F, G>(
1192        &mut self,
1193        label: impl AsRef<str>,
1194        can_merge: bool,
1195        code: F,
1196    ) -> ReadVar<T>
1197    where
1198        T: Serialize + DeserializeOwned + 'static,
1199        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1200        G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<T> + 'static,
1201    {
1202        let (read, write) = self.new_var();
1203
1204        let ctx = &mut StepCtx {
1205            backend: self.backend.clone(),
1206        };
1207        let write = write.claim(ctx);
1208
1209        let code = code(ctx);
1210        self.backend.borrow_mut().on_emit_rust_step(
1211            label.as_ref(),
1212            can_merge,
1213            Box::new(|rt| {
1214                let val = code(rt)?;
1215                rt.write(write, &val);
1216                Ok(())
1217            }),
1218        );
1219        read
1220    }
1221
1222    /// Load an ADO global runtime variable into a flowey [`ReadVar`].
1223    #[track_caller]
1224    #[must_use]
1225    pub fn get_ado_variable(&mut self, ado_var: AdoRuntimeVar) -> ReadVar<String> {
1226        let (var, write_var) = self.new_var();
1227        self.emit_ado_step(format!("🌼 read {}", ado_var.as_raw_var_name()), |ctx| {
1228            let write_var = write_var.claim(ctx);
1229            |rt| {
1230                rt.set_var(write_var, ado_var);
1231                "".into()
1232            }
1233        });
1234        var
1235    }
1236
1237    /// Emit an ADO step.
1238    pub fn emit_ado_step<F, G>(&mut self, display_name: impl AsRef<str>, yaml_snippet: F)
1239    where
1240        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1241        G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1242    {
1243        self.emit_ado_step_inner(display_name, None, |ctx| {
1244            (yaml_snippet(ctx), NO_ADO_INLINE_SCRIPT)
1245        })
1246    }
1247
1248    /// Emit an ADO step, conditionally executed based on the value of `cond` at
1249    /// runtime.
1250    pub fn emit_ado_step_with_condition<F, G>(
1251        &mut self,
1252        display_name: impl AsRef<str>,
1253        cond: ReadVar<bool>,
1254        yaml_snippet: F,
1255    ) where
1256        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1257        G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1258    {
1259        self.emit_ado_step_inner(display_name, Some(cond), |ctx| {
1260            (yaml_snippet(ctx), NO_ADO_INLINE_SCRIPT)
1261        })
1262    }
1263
1264    /// Emit an ADO step, conditionally executed based on the value of`cond` at
1265    /// runtime.
1266    pub fn emit_ado_step_with_condition_optional<F, G>(
1267        &mut self,
1268        display_name: impl AsRef<str>,
1269        cond: Option<ReadVar<bool>>,
1270        yaml_snippet: F,
1271    ) where
1272        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1273        G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1274    {
1275        self.emit_ado_step_inner(display_name, cond, |ctx| {
1276            (yaml_snippet(ctx), NO_ADO_INLINE_SCRIPT)
1277        })
1278    }
1279
1280    /// Emit an ADO step which invokes a rust callback using an inline script.
1281    ///
1282    /// By using the `{{FLOWEY_INLINE_SCRIPT}}` template in the returned yaml
1283    /// snippet, flowey will interpolate a command ~roughly akin to `flowey
1284    /// exec-snippet <rust-snippet-id>` into the generated yaml.
1285    ///
1286    /// e.g: if we wanted to _manually_ wrap the bash ADO snippet for whatever
1287    /// reason:
1288    ///
1289    /// ```text
1290    /// - bash: |
1291    ///     echo "hello there!"
1292    ///     {{FLOWEY_INLINE_SCRIPT}}
1293    ///     echo echo "bye!"
1294    /// ```
1295    ///
1296    /// # Limitations
1297    ///
1298    /// At the moment, due to flowey API limitations, it is only possible to
1299    /// embed a single inline script into a YAML step.
1300    ///
1301    /// In the future, rather than having separate methods for "emit step with X
1302    /// inline scripts", flowey should support declaring "first-class" callbacks
1303    /// via a (hypothetical) `ctx.new_callback_var(|ctx| |rt, input: Input| ->
1304    /// Output { ... })` API, at which point.
1305    ///
1306    /// If such an API were to exist, one could simply use the "vanilla" emit
1307    /// yaml step functions with these first-class callbacks.
1308    pub fn emit_ado_step_with_inline_script<F, G, H>(
1309        &mut self,
1310        display_name: impl AsRef<str>,
1311        yaml_snippet: F,
1312    ) where
1313        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> (G, H),
1314        G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1315        H: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1316    {
1317        self.emit_ado_step_inner(display_name, None, |ctx| {
1318            let (f, g) = yaml_snippet(ctx);
1319            (f, Some(g))
1320        })
1321    }
1322
1323    fn emit_ado_step_inner<F, G, H>(
1324        &mut self,
1325        display_name: impl AsRef<str>,
1326        cond: Option<ReadVar<bool>>,
1327        yaml_snippet: F,
1328    ) where
1329        F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> (G, Option<H>),
1330        G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1331        H: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1332    {
1333        let condvar = match cond.map(|c| c.backing_var) {
1334            // it seems silly to allow this... but it's not hard so why not?
1335            Some(ReadVarBacking::Inline(cond)) => {
1336                if !cond {
1337                    return;
1338                } else {
1339                    None
1340                }
1341            }
1342            Some(ReadVarBacking::RuntimeVar {
1343                var,
1344                is_side_effect,
1345            }) => {
1346                assert!(!is_side_effect);
1347                self.backend.borrow_mut().on_claimed_runtime_var(&var, true);
1348                Some(var)
1349            }
1350            None => None,
1351        };
1352
1353        let (yaml_snippet, inline_script) = yaml_snippet(&mut StepCtx {
1354            backend: self.backend.clone(),
1355        });
1356        self.backend.borrow_mut().on_emit_ado_step(
1357            display_name.as_ref(),
1358            Box::new(yaml_snippet),
1359            if let Some(inline_script) = inline_script {
1360                Some(Box::new(inline_script))
1361            } else {
1362                None
1363            },
1364            condvar,
1365        );
1366    }
1367
1368    /// Load a GitHub context variable into a flowey [`ReadVar`].
1369    #[track_caller]
1370    #[must_use]
1371    pub fn get_gh_context_var(&mut self) -> GhContextVarReader<'ctx, Root> {
1372        GhContextVarReader {
1373            ctx: NodeCtx {
1374                backend: self.backend.clone(),
1375            },
1376            _state: std::marker::PhantomData,
1377        }
1378    }
1379
1380    /// Emit a GitHub Actions action step.
1381    pub fn emit_gh_step(
1382        &mut self,
1383        display_name: impl AsRef<str>,
1384        uses: impl AsRef<str>,
1385    ) -> GhStepBuilder {
1386        GhStepBuilder::new(display_name, uses)
1387    }
1388
1389    fn emit_gh_step_inner(
1390        &mut self,
1391        display_name: impl AsRef<str>,
1392        cond: Option<ReadVar<bool>>,
1393        uses: impl AsRef<str>,
1394        with: Option<BTreeMap<String, GhParam>>,
1395        outputs: BTreeMap<String, Vec<WriteVar<String>>>,
1396        run_after: Vec<ReadVar<SideEffect>>,
1397        permissions: BTreeMap<GhPermission, GhPermissionValue>,
1398    ) {
1399        let condvar = match cond.map(|c| c.backing_var) {
1400            // it seems silly to allow this... but it's not hard so why not?
1401            Some(ReadVarBacking::Inline(cond)) => {
1402                if !cond {
1403                    return;
1404                } else {
1405                    None
1406                }
1407            }
1408            Some(ReadVarBacking::RuntimeVar {
1409                var,
1410                is_side_effect,
1411            }) => {
1412                assert!(!is_side_effect);
1413                self.backend.borrow_mut().on_claimed_runtime_var(&var, true);
1414                Some(var)
1415            }
1416            None => None,
1417        };
1418
1419        let with = with
1420            .unwrap_or_default()
1421            .into_iter()
1422            .map(|(k, v)| {
1423                (
1424                    k.clone(),
1425                    v.claim(&mut StepCtx {
1426                        backend: self.backend.clone(),
1427                    }),
1428                )
1429            })
1430            .collect();
1431
1432        for var in run_after {
1433            var.claim(&mut StepCtx {
1434                backend: self.backend.clone(),
1435            });
1436        }
1437
1438        let outputvars = outputs
1439            .into_iter()
1440            .map(|(name, vars)| {
1441                (
1442                    name,
1443                    vars.into_iter()
1444                        .map(|var| {
1445                            let var = var.claim(&mut StepCtx {
1446                                backend: self.backend.clone(),
1447                            });
1448                            GhOutput {
1449                                backing_var: var.backing_var,
1450                                is_secret: false,
1451                                is_object: false,
1452                            }
1453                        })
1454                        .collect(),
1455                )
1456            })
1457            .collect();
1458
1459        self.backend.borrow_mut().on_emit_gh_step(
1460            display_name.as_ref(),
1461            uses.as_ref(),
1462            with,
1463            condvar,
1464            outputvars,
1465            permissions,
1466            Vec::new(),
1467            Vec::new(),
1468        );
1469    }
1470
1471    /// Emit a "side-effect" step, which simply claims a set of side-effects in
1472    /// order to resolve another set of side effects.
1473    ///
1474    /// The same functionality could be achieved (less efficiently) by emitting
1475    /// a Rust step (or ADO step, or github step, etc...) that claims both sets
1476    /// of side-effects, and then does nothing. By using this method - flowey is
1477    /// able to avoid emitting that additional noop step at runtime.
1478    pub fn emit_side_effect_step(
1479        &mut self,
1480        use_side_effects: impl IntoIterator<Item = ReadVar<SideEffect>>,
1481        resolve_side_effects: impl IntoIterator<Item = WriteVar<SideEffect>>,
1482    ) {
1483        let mut backend = self.backend.borrow_mut();
1484        for var in use_side_effects.into_iter() {
1485            if let ReadVarBacking::RuntimeVar {
1486                var,
1487                is_side_effect: _,
1488            } = &var.backing_var
1489            {
1490                backend.on_claimed_runtime_var(var, true);
1491            }
1492        }
1493
1494        for var in resolve_side_effects.into_iter() {
1495            backend.on_claimed_runtime_var(&var.backing_var, false);
1496        }
1497
1498        backend.on_emit_side_effect_step();
1499    }
1500
1501    /// What backend the flow is being running on (e.g: locally, ADO, GitHub,
1502    /// etc...)
1503    pub fn backend(&self) -> FlowBackend {
1504        self.backend.borrow_mut().backend()
1505    }
1506
1507    /// What platform the flow is being running on (e.g: windows, linux, wsl2,
1508    /// etc...).
1509    pub fn platform(&self) -> FlowPlatform {
1510        self.backend.borrow_mut().platform()
1511    }
1512
1513    /// What architecture the flow is being running on (x86_64 or Aarch64)
1514    pub fn arch(&self) -> FlowArch {
1515        self.backend.borrow_mut().arch()
1516    }
1517
1518    /// Set a request on a particular node.
1519    pub fn req<R>(&mut self, req: R)
1520    where
1521        R: IntoRequest + 'static,
1522    {
1523        let mut backend = self.backend.borrow_mut();
1524        backend.on_request(
1525            NodeHandle::from_type::<R::Node>(),
1526            serde_json::to_vec(&req.into_request())
1527                .map(Into::into)
1528                .map_err(Into::into),
1529        );
1530    }
1531
1532    /// Set a request on a particular node, simultaneously creating a new flowey
1533    /// Var in the process.
1534    #[track_caller]
1535    #[must_use]
1536    pub fn reqv<T, R>(&mut self, f: impl FnOnce(WriteVar<T>) -> R) -> ReadVar<T>
1537    where
1538        T: Serialize + DeserializeOwned,
1539        R: IntoRequest + 'static,
1540    {
1541        let (read, write) = self.new_var();
1542        self.req::<R>(f(write));
1543        read
1544    }
1545
1546    /// Set multiple requests on a particular node.
1547    pub fn requests<N>(&mut self, reqs: impl IntoIterator<Item = N::Request>)
1548    where
1549        N: FlowNodeBase + 'static,
1550    {
1551        let mut backend = self.backend.borrow_mut();
1552        for req in reqs.into_iter() {
1553            backend.on_request(
1554                NodeHandle::from_type::<N>(),
1555                serde_json::to_vec(&req).map(Into::into).map_err(Into::into),
1556            );
1557        }
1558    }
1559
1560    /// Allocate a new flowey Var, returning two handles: one for reading the
1561    /// value, and another for writing the value.
1562    #[track_caller]
1563    #[must_use]
1564    pub fn new_var<T>(&self) -> (ReadVar<T>, WriteVar<T>)
1565    where
1566        T: Serialize + DeserializeOwned,
1567    {
1568        self.new_prefixed_var("")
1569    }
1570
1571    #[track_caller]
1572    #[must_use]
1573    fn new_prefixed_var<T>(&self, prefix: &'static str) -> (ReadVar<T>, WriteVar<T>)
1574    where
1575        T: Serialize + DeserializeOwned,
1576    {
1577        // normalize call path to ensure determinism between windows and linux
1578        let caller = std::panic::Location::caller()
1579            .to_string()
1580            .replace('\\', "/");
1581
1582        // until we have a proper way to "split" debug info related to vars, we
1583        // kinda just lump it in with the var name itself.
1584        //
1585        // HACK: to work around cases where - depending on what the
1586        // current-working-dir is when incoking flowey - the returned
1587        // caller.file() path may leak the full path of the file (as opposed to
1588        // the relative path), resulting in inconsistencies between build
1589        // environments.
1590        //
1591        // For expediency, and to preserve some semblance of useful error
1592        // messages, we decided to play some sketchy games with the resulting
1593        // string to only preserve the _consistent_ bit of the path for a human
1594        // to use as reference.
1595        //
1596        // This is not ideal in the slightest, but it works OK for now
1597        let caller = caller
1598            .split_once("flowey/")
1599            .expect("due to a known limitation with flowey, all flowey code must have an ancestor dir called 'flowey/' somewhere in its full path")
1600            .1;
1601
1602        let colon = if prefix.is_empty() { "" } else { ":" };
1603        let ordinal = self.backend.borrow_mut().on_new_var();
1604        let backing_var = format!("{prefix}{colon}{ordinal}:{caller}");
1605
1606        (
1607            ReadVar {
1608                backing_var: ReadVarBacking::RuntimeVar {
1609                    var: backing_var.clone(),
1610                    is_side_effect: false,
1611                },
1612                _kind: std::marker::PhantomData,
1613            },
1614            WriteVar {
1615                backing_var,
1616                is_side_effect: false,
1617                _kind: std::marker::PhantomData,
1618            },
1619        )
1620    }
1621
1622    /// Allocate special [`SideEffect`] var which can be used to schedule a
1623    /// "post-job" step associated with some existing step.
1624    ///
1625    /// This "post-job" step will then only run after all other regular steps
1626    /// have run (i.e: steps required to complete any top-level objectives
1627    /// passed in via [`crate::pipeline::PipelineJob::dep_on`]). This makes it
1628    /// useful for implementing various "cleanup" or "finalize" tasks.
1629    ///
1630    /// e.g: the Cache node uses this to upload the contents of a cache
1631    /// directory at the end of a Job.
1632    #[track_caller]
1633    #[must_use]
1634    pub fn new_post_job_side_effect(&self) -> (ReadVar<SideEffect>, WriteVar<SideEffect>) {
1635        self.new_prefixed_var("post_job")
1636    }
1637
1638    /// Return a flowey Var pointing to a **node-specific** directory which
1639    /// will be persisted between runs, if such a directory is available.
1640    ///
1641    /// WARNING: this method is _very likely_ to return None when running on CI
1642    /// machines, as most CI agents are wiped between jobs!
1643    ///
1644    /// As such, it is NOT recommended that node authors reach for this method
1645    /// directly, and instead use abstractions such as the
1646    /// `flowey_lib_common::cache` Node, which implements node-level persistence
1647    /// in a way that works _regardless_ if a persistent_dir is available (e.g:
1648    /// by falling back to uploading / downloading artifacts to a "cache store"
1649    /// on platforms like ADO or Github Actions).
1650    #[track_caller]
1651    #[must_use]
1652    pub fn persistent_dir(&mut self) -> Option<ReadVar<PathBuf>> {
1653        let path: ReadVar<PathBuf> = ReadVar {
1654            backing_var: ReadVarBacking::RuntimeVar {
1655                var: self.backend.borrow_mut().persistent_dir_path_var()?,
1656                is_side_effect: false,
1657            },
1658            _kind: std::marker::PhantomData,
1659        };
1660
1661        let folder_name = self
1662            .backend
1663            .borrow_mut()
1664            .current_node()
1665            .modpath()
1666            .replace("::", "__");
1667
1668        Some(
1669            self.emit_rust_stepv("🌼 Create persistent store dir", |ctx| {
1670                let path = path.claim(ctx);
1671                |rt| {
1672                    let dir = rt.read(path).join(folder_name);
1673                    fs_err::create_dir_all(&dir)?;
1674                    Ok(dir)
1675                }
1676            }),
1677        )
1678    }
1679
1680    /// Check to see if a persistent dir is available, without yet creating it.
1681    pub fn supports_persistent_dir(&mut self) -> bool {
1682        self.backend
1683            .borrow_mut()
1684            .persistent_dir_path_var()
1685            .is_some()
1686    }
1687}
1688
1689// FUTURE: explore using type-erased serde here, instead of relying on
1690// `serde_json` in `flowey_core`.
1691pub trait RuntimeVarDb {
1692    fn get_var(&mut self, var_name: &str) -> (Vec<u8>, bool) {
1693        self.try_get_var(var_name)
1694            .unwrap_or_else(|| panic!("db is missing var {}", var_name))
1695    }
1696
1697    fn try_get_var(&mut self, var_name: &str) -> Option<(Vec<u8>, bool)>;
1698    fn set_var(&mut self, var_name: &str, is_secret: bool, value: Vec<u8>);
1699}
1700
1701impl RuntimeVarDb for Box<dyn RuntimeVarDb> {
1702    fn try_get_var(&mut self, var_name: &str) -> Option<(Vec<u8>, bool)> {
1703        (**self).try_get_var(var_name)
1704    }
1705
1706    fn set_var(&mut self, var_name: &str, is_secret: bool, value: Vec<u8>) {
1707        (**self).set_var(var_name, is_secret, value)
1708    }
1709}
1710
1711pub mod steps {
1712    pub mod ado {
1713        use crate::node::ClaimedReadVar;
1714        use crate::node::ClaimedWriteVar;
1715        use crate::node::ReadVarBacking;
1716        use serde::Deserialize;
1717        use serde::Serialize;
1718        use std::borrow::Cow;
1719
1720        /// An ADO repository declared as a resource in the top-level pipeline.
1721        ///
1722        /// Created via [`crate::pipeline::Pipeline::ado_add_resources_repository`].
1723        ///
1724        /// Consumed via [`AdoStepServices::resolve_repository_id`].
1725        #[derive(Debug, Clone, Serialize, Deserialize)]
1726        pub struct AdoResourcesRepositoryId {
1727            pub(crate) repo_id: String,
1728        }
1729
1730        impl AdoResourcesRepositoryId {
1731            /// Create a `AdoResourcesRepositoryId` corresponding to `self`
1732            /// (i.e: the repo which stores the current pipeline).
1733            ///
1734            /// This is safe to do from any context, as the `self` resource will
1735            /// _always_ be available.
1736            pub fn new_self() -> Self {
1737                Self {
1738                    repo_id: "self".into(),
1739                }
1740            }
1741
1742            /// (dangerous) get the raw ID associated with this resource.
1743            ///
1744            /// It is highly recommended to avoid losing type-safety, and
1745            /// sticking to [`AdoStepServices::resolve_repository_id`].in order
1746            /// to resolve this type to a String.
1747            pub fn dangerous_get_raw_id(&self) -> &str {
1748                &self.repo_id
1749            }
1750
1751            /// (dangerous) create a new ID out of thin air.
1752            ///
1753            /// It is highly recommended to avoid losing type-safety, and
1754            /// sticking to [`AdoStepServices::resolve_repository_id`].in order
1755            /// to resolve this type to a String.
1756            pub fn dangerous_new(repo_id: &str) -> Self {
1757                Self {
1758                    repo_id: repo_id.into(),
1759                }
1760            }
1761        }
1762
1763        /// Handle to an ADO variable.
1764        ///
1765        /// Includes a (non-exhaustive) list of associated constants
1766        /// corresponding to global ADO vars which are _always_ available.
1767        #[derive(Clone, Debug, Serialize, Deserialize)]
1768        pub struct AdoRuntimeVar {
1769            is_secret: bool,
1770            ado_var: Cow<'static, str>,
1771        }
1772
1773        #[allow(non_upper_case_globals)]
1774        impl AdoRuntimeVar {
1775            /// `build.SourceBranch`
1776            ///
1777            /// NOTE: Includes the full branch ref (ex: `refs/heads/main`) so
1778            /// unlike `build.SourceBranchName`, a branch like `user/foo/bar`
1779            /// won't be stripped to just `bar`
1780            pub const BUILD__SOURCE_BRANCH: AdoRuntimeVar =
1781                AdoRuntimeVar::new("build.SourceBranch");
1782
1783            /// `build.BuildNumber`
1784            pub const BUILD__BUILD_NUMBER: AdoRuntimeVar = AdoRuntimeVar::new("build.BuildNumber");
1785
1786            /// `System.AccessToken`
1787            pub const SYSTEM__ACCESS_TOKEN: AdoRuntimeVar =
1788                AdoRuntimeVar::new_secret("System.AccessToken");
1789
1790            /// `System.System.JobAttempt`
1791            pub const SYSTEM__JOB_ATTEMPT: AdoRuntimeVar =
1792                AdoRuntimeVar::new_secret("System.JobAttempt");
1793        }
1794
1795        impl AdoRuntimeVar {
1796            const fn new(s: &'static str) -> Self {
1797                Self {
1798                    is_secret: false,
1799                    ado_var: Cow::Borrowed(s),
1800                }
1801            }
1802
1803            const fn new_secret(s: &'static str) -> Self {
1804                Self {
1805                    is_secret: true,
1806                    ado_var: Cow::Borrowed(s),
1807                }
1808            }
1809
1810            /// Check if the ADO var is tagged as being a secret
1811            pub fn is_secret(&self) -> bool {
1812                self.is_secret
1813            }
1814
1815            /// Get the raw underlying ADO variable name
1816            pub fn as_raw_var_name(&self) -> String {
1817                self.ado_var.as_ref().into()
1818            }
1819
1820            /// Get a handle to an ADO runtime variable corresponding to a
1821            /// global ADO variable with the given name.
1822            ///
1823            /// This method should be used rarely and with great care!
1824            ///
1825            /// ADO variables are global, and sidestep the type-safe data flow
1826            /// between flowey nodes entirely!
1827            pub fn dangerous_from_global(ado_var_name: impl AsRef<str>, is_secret: bool) -> Self {
1828                Self {
1829                    is_secret,
1830                    ado_var: ado_var_name.as_ref().to_owned().into(),
1831                }
1832            }
1833        }
1834
1835        pub fn new_ado_step_services(
1836            fresh_ado_var: &mut dyn FnMut() -> String,
1837        ) -> AdoStepServices<'_> {
1838            AdoStepServices {
1839                fresh_ado_var,
1840                ado_to_rust: Vec::new(),
1841                rust_to_ado: Vec::new(),
1842            }
1843        }
1844
1845        pub struct CompletedAdoStepServices {
1846            pub ado_to_rust: Vec<(String, String, bool)>,
1847            pub rust_to_ado: Vec<(String, String)>,
1848        }
1849
1850        impl CompletedAdoStepServices {
1851            pub fn from_ado_step_services(access: AdoStepServices<'_>) -> Self {
1852                let AdoStepServices {
1853                    fresh_ado_var: _,
1854                    ado_to_rust,
1855                    rust_to_ado,
1856                } = access;
1857
1858                Self {
1859                    ado_to_rust,
1860                    rust_to_ado,
1861                }
1862            }
1863        }
1864
1865        pub struct AdoStepServices<'a> {
1866            fresh_ado_var: &'a mut dyn FnMut() -> String,
1867            ado_to_rust: Vec<(String, String, bool)>,
1868            rust_to_ado: Vec<(String, String)>,
1869        }
1870
1871        impl AdoStepServices<'_> {
1872            /// Return the raw string identifier for the given
1873            /// [`AdoResourcesRepositoryId`].
1874            pub fn resolve_repository_id(&self, repo_id: AdoResourcesRepositoryId) -> String {
1875                repo_id.repo_id
1876            }
1877
1878            /// Set the specified flowey Var using the value of the given ADO var.
1879            // TODO: is there a good way to allow auto-casting the ADO var back
1880            // to a WriteVar<T>, instead of just a String? It's complicated by
1881            // the fact that the ADO var to flowey bridge is handled by the ADO
1882            // backend, which itself needs to know type info...
1883            pub fn set_var(&mut self, var: ClaimedWriteVar<String>, from_ado_var: AdoRuntimeVar) {
1884                self.ado_to_rust.push((
1885                    from_ado_var.ado_var.into(),
1886                    var.backing_var,
1887                    from_ado_var.is_secret,
1888                ))
1889            }
1890
1891            /// Get the value of a flowey Var as a ADO runtime variable.
1892            pub fn get_var(&mut self, var: ClaimedReadVar<String>) -> AdoRuntimeVar {
1893                let backing_var = if let ReadVarBacking::RuntimeVar {
1894                    var,
1895                    is_side_effect,
1896                } = &var.backing_var
1897                {
1898                    assert!(!is_side_effect);
1899                    var
1900                } else {
1901                    todo!("support inline ado read vars")
1902                };
1903
1904                let new_ado_var_name = (self.fresh_ado_var)();
1905
1906                self.rust_to_ado
1907                    .push((backing_var.clone(), new_ado_var_name.clone()));
1908                AdoRuntimeVar::dangerous_from_global(new_ado_var_name, false)
1909            }
1910        }
1911    }
1912
1913    pub mod github {
1914        use crate::node::ClaimVar;
1915        use crate::node::NodeCtx;
1916        use crate::node::ReadVar;
1917        use crate::node::ReadVarBacking;
1918        use crate::node::SideEffect;
1919        use crate::node::StepCtx;
1920        use crate::node::VarClaimed;
1921        use crate::node::VarNotClaimed;
1922        use crate::node::WriteVar;
1923        use std::collections::BTreeMap;
1924
1925        pub struct GhStepBuilder {
1926            display_name: String,
1927            cond: Option<ReadVar<bool>>,
1928            uses: String,
1929            with: Option<BTreeMap<String, GhParam>>,
1930            outputs: BTreeMap<String, Vec<WriteVar<String>>>,
1931            run_after: Vec<ReadVar<SideEffect>>,
1932            permissions: BTreeMap<GhPermission, GhPermissionValue>,
1933        }
1934
1935        impl GhStepBuilder {
1936            /// Creates a new GitHub step builder, with the given display name and
1937            /// action to use. For example, the following code generates the following yaml:
1938            ///
1939            /// ```ignore
1940            /// GhStepBuilder::new("Check out repository code", "actions/checkout@v4").finish()
1941            /// ```
1942            ///
1943            /// ```ignore
1944            /// - name: Check out repository code
1945            ///   uses: actions/checkout@v4
1946            /// ```
1947            ///
1948            /// For more information on the yaml syntax for the `name` and `uses` parameters,
1949            /// see <https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsname>
1950            pub fn new(display_name: impl AsRef<str>, uses: impl AsRef<str>) -> Self {
1951                Self {
1952                    display_name: display_name.as_ref().into(),
1953                    cond: None,
1954                    uses: uses.as_ref().into(),
1955                    with: None,
1956                    outputs: BTreeMap::new(),
1957                    run_after: Vec::new(),
1958                    permissions: BTreeMap::new(),
1959                }
1960            }
1961
1962            /// Adds a condition [`ReadVar<bool>`] to the step,
1963            /// such that the step only executes if the condition is true.
1964            /// This is equivalent to using an `if` conditional in the yaml.
1965            ///
1966            /// For more information on the yaml syntax for `if` conditionals, see
1967            /// <https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsname>
1968            pub fn condition(mut self, cond: ReadVar<bool>) -> Self {
1969                self.cond = Some(cond);
1970                self
1971            }
1972
1973            /// Adds a parameter to the step, specified as a key-value pair corresponding
1974            /// to the param name and value. For example the following code generates the following yaml:
1975            ///
1976            /// ```rust,ignore
1977            /// let (client_id, write_client_id) = ctx.new_var();
1978            /// let (tenant_id, write_tenant_id) = ctx.new_var();
1979            /// let (subscription_id, write_subscription_id) = ctx.new_var();
1980            /// // ... insert rust step writing to each of those secrets ...
1981            /// GhStepBuilder::new("Azure Login", "Azure/login@v2")
1982            ///               .with("client-id", client_id)
1983            ///               .with("tenant-id", tenant_id)
1984            ///               .with("subscription-id", subscription_id)
1985            /// ```
1986            ///
1987            /// ```text
1988            /// - name: Azure Login
1989            ///   uses: Azure/login@v2
1990            ///   with:
1991            ///     client-id: ${{ env.floweyvar1 }} // Assuming the backend wrote client_id to floweyvar1
1992            ///     tenant-id: ${{ env.floweyvar2 }} // Assuming the backend wrote tenant-id to floweyvar2
1993            ///     subscription-id: ${{ env.floweyvar3 }} // Assuming the backend wrote subscription-id to floweyvar3
1994            /// ```
1995            ///
1996            /// For more information on the yaml syntax for the `with` parameters,
1997            /// see <https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_idstepswith>
1998            pub fn with(mut self, k: impl AsRef<str>, v: impl Into<GhParam>) -> Self {
1999                self.with.get_or_insert_with(BTreeMap::new);
2000                if let Some(with) = &mut self.with {
2001                    with.insert(k.as_ref().to_string(), v.into());
2002                }
2003                self
2004            }
2005
2006            /// Specifies an output to read from the step, specified as a key-value pair
2007            /// corresponding to the output name and the flowey var to write the output to.
2008            ///
2009            /// This is equivalent to writing into `v` the output of a step in the yaml using:
2010            /// `${{ steps.<backend-assigned-step-id>.outputs.<k> }}`
2011            ///
2012            /// For more information on step outputs, see
2013            /// <https://docs.github.com/en/actions/sharing-automations/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions>
2014            pub fn output(mut self, k: impl AsRef<str>, v: WriteVar<String>) -> Self {
2015                self.outputs
2016                    .entry(k.as_ref().to_string())
2017                    .or_default()
2018                    .push(v);
2019                self
2020            }
2021
2022            /// Specifies a side-effect that must be resolved before this step can run.
2023            pub fn run_after(mut self, side_effect: ReadVar<SideEffect>) -> Self {
2024                self.run_after.push(side_effect);
2025                self
2026            }
2027
2028            /// Declare that this step requires a certain GITHUB_TOKEN permission in order to run.
2029            ///
2030            /// For more info about Github Actions permissions, see [`gh_grant_permissions`](crate::pipeline::PipelineJob::gh_grant_permissions) and
2031            /// <https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/assigning-permissions-to-jobs>
2032            pub fn requires_permission(
2033                mut self,
2034                perm: GhPermission,
2035                value: GhPermissionValue,
2036            ) -> Self {
2037                self.permissions.insert(perm, value);
2038                self
2039            }
2040
2041            /// Finish building the step, emitting it to the backend and returning a side-effect.
2042            #[track_caller]
2043            pub fn finish(self, ctx: &mut NodeCtx<'_>) -> ReadVar<SideEffect> {
2044                let (side_effect, claim_side_effect) = ctx.new_prefixed_var("auto_se");
2045                ctx.backend
2046                    .borrow_mut()
2047                    .on_claimed_runtime_var(&claim_side_effect.backing_var, false);
2048
2049                ctx.emit_gh_step_inner(
2050                    self.display_name,
2051                    self.cond,
2052                    self.uses,
2053                    self.with,
2054                    self.outputs,
2055                    self.run_after,
2056                    self.permissions,
2057                );
2058
2059                side_effect
2060            }
2061        }
2062
2063        #[derive(Clone, Debug)]
2064        pub enum GhParam<C = VarNotClaimed> {
2065            Static(String),
2066            FloweyVar(ReadVar<String, C>),
2067        }
2068
2069        impl From<String> for GhParam {
2070            fn from(param: String) -> GhParam {
2071                GhParam::Static(param)
2072            }
2073        }
2074
2075        impl From<&str> for GhParam {
2076            fn from(param: &str) -> GhParam {
2077                GhParam::Static(param.to_string())
2078            }
2079        }
2080
2081        impl From<ReadVar<String>> for GhParam {
2082            fn from(param: ReadVar<String>) -> GhParam {
2083                GhParam::FloweyVar(param)
2084            }
2085        }
2086
2087        pub type ClaimedGhParam = GhParam<VarClaimed>;
2088
2089        impl ClaimVar for GhParam {
2090            type Claimed = ClaimedGhParam;
2091
2092            fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedGhParam {
2093                match self {
2094                    GhParam::Static(s) => ClaimedGhParam::Static(s),
2095                    GhParam::FloweyVar(var) => match &var.backing_var {
2096                        ReadVarBacking::RuntimeVar { is_side_effect, .. } => {
2097                            assert!(!is_side_effect);
2098                            ClaimedGhParam::FloweyVar(var.claim(ctx))
2099                        }
2100                        ReadVarBacking::Inline(var) => ClaimedGhParam::Static(var.clone()),
2101                    },
2102                }
2103            }
2104        }
2105
2106        /// The assigned permission value for a scope.
2107        ///
2108        /// For more details on how these values affect a particular scope, refer to:
2109        /// <https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs>
2110        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2111        pub enum GhPermissionValue {
2112            Read,
2113            Write,
2114            None,
2115        }
2116
2117        /// Refers to the scope of a permission granted to the GITHUB_TOKEN
2118        /// for a job.
2119        ///
2120        /// For more details on each scope, refer to:
2121        /// <https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs>
2122        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2123        pub enum GhPermission {
2124            Actions,
2125            Attestations,
2126            Checks,
2127            Contents,
2128            Deployments,
2129            Discussions,
2130            IdToken,
2131            Issues,
2132            Packages,
2133            Pages,
2134            PullRequests,
2135            RepositoryProjects,
2136            SecurityEvents,
2137            Statuses,
2138        }
2139    }
2140
2141    pub mod rust {
2142        use crate::node::ClaimedWriteVar;
2143        use crate::node::FlowArch;
2144        use crate::node::FlowBackend;
2145        use crate::node::FlowPlatform;
2146        use crate::node::ReadVarValue;
2147        use crate::node::RuntimeVarDb;
2148        use serde::Serialize;
2149        use serde::de::DeserializeOwned;
2150
2151        pub fn new_rust_runtime_services(
2152            runtime_var_db: &mut dyn RuntimeVarDb,
2153            backend: FlowBackend,
2154            platform: FlowPlatform,
2155            arch: FlowArch,
2156        ) -> RustRuntimeServices<'_> {
2157            RustRuntimeServices {
2158                runtime_var_db,
2159                backend,
2160                platform,
2161                arch,
2162                has_read_secret: false,
2163            }
2164        }
2165
2166        pub struct RustRuntimeServices<'a> {
2167            runtime_var_db: &'a mut dyn RuntimeVarDb,
2168            backend: FlowBackend,
2169            platform: FlowPlatform,
2170            arch: FlowArch,
2171            has_read_secret: bool,
2172        }
2173
2174        impl RustRuntimeServices<'_> {
2175            /// What backend the flow is being running on (e.g: locally, ADO,
2176            /// GitHub, etc...)
2177            pub fn backend(&self) -> FlowBackend {
2178                self.backend
2179            }
2180
2181            /// What platform the flow is being running on (e.g: windows, linux,
2182            /// etc...).
2183            pub fn platform(&self) -> FlowPlatform {
2184                self.platform
2185            }
2186
2187            /// What arch the flow is being running on (X86_64 or Aarch64)
2188            pub fn arch(&self) -> FlowArch {
2189                self.arch
2190            }
2191
2192            /// Write a value.
2193            ///
2194            /// If this step has already read a secret value, then this will be
2195            /// written as a secret value, as a conservative estimate to avoid
2196            /// leaking secrets. Use [`write_secret`](Self::write_secret) or
2197            /// [`write_not_secret`](Self::write_not_secret) to override this
2198            /// behavior.
2199            pub fn write<T>(&mut self, var: ClaimedWriteVar<T>, val: &T)
2200            where
2201                T: Serialize + DeserializeOwned,
2202            {
2203                self.write_maybe_secret(var, val, self.has_read_secret)
2204            }
2205
2206            /// Write a secret value, such as a key or token.
2207            ///
2208            /// Flowey will avoid logging this value, and if the value is
2209            /// converted to a CI environment variable, the CI system will be
2210            /// told not to print the value either.
2211            pub fn write_secret<T>(&mut self, var: ClaimedWriteVar<T>, val: &T)
2212            where
2213                T: Serialize + DeserializeOwned,
2214            {
2215                self.write_maybe_secret(var, val, true)
2216            }
2217
2218            /// Write a value that is not secret, even if this step has already
2219            /// read secret values.
2220            ///
2221            /// Usually [`write`](Self::write) is preferred--use this only when
2222            /// your step reads secret values and you explicitly want to write a
2223            /// non-secret value.
2224            pub fn write_not_secret<T>(&mut self, var: ClaimedWriteVar<T>, val: &T)
2225            where
2226                T: Serialize + DeserializeOwned,
2227            {
2228                self.write_maybe_secret(var, val, false)
2229            }
2230
2231            fn write_maybe_secret<T>(&mut self, var: ClaimedWriteVar<T>, val: &T, is_secret: bool)
2232            where
2233                T: Serialize + DeserializeOwned,
2234            {
2235                let val = if var.is_side_effect {
2236                    b"null".to_vec()
2237                } else {
2238                    serde_json::to_vec(val).expect("improve this error path")
2239                };
2240                self.runtime_var_db
2241                    .set_var(&var.backing_var, is_secret, val);
2242            }
2243
2244            pub fn write_all<T>(
2245                &mut self,
2246                vars: impl IntoIterator<Item = ClaimedWriteVar<T>>,
2247                val: &T,
2248            ) where
2249                T: Serialize + DeserializeOwned,
2250            {
2251                for var in vars {
2252                    self.write(var, val)
2253                }
2254            }
2255
2256            pub fn read<T: ReadVarValue>(&mut self, var: T) -> T::Value {
2257                var.read_value(self)
2258            }
2259
2260            pub(crate) fn get_var(&mut self, var: &str, is_side_effect: bool) -> Vec<u8> {
2261                let (v, is_secret) = self.runtime_var_db.get_var(var);
2262                self.has_read_secret |= is_secret && !is_side_effect;
2263                v
2264            }
2265
2266            /// DANGEROUS: Set the value of _Global_ Environment Variable (GitHub Actions only).
2267            ///
2268            /// It is up to the caller to ensure that the variable does not get
2269            /// unintentionally overwritten or used.
2270            ///
2271            /// This method should be used rarely and with great care!
2272            pub fn dangerous_gh_set_global_env_var(
2273                &mut self,
2274                var: String,
2275                gh_env_var: String,
2276            ) -> anyhow::Result<()> {
2277                if !matches!(self.backend, FlowBackend::Github) {
2278                    return Err(anyhow::anyhow!(
2279                        "dangerous_set_gh_env_var can only be used on GitHub Actions"
2280                    ));
2281                }
2282
2283                let gh_env_file_path = std::env::var("GITHUB_ENV")?;
2284                let mut gh_env_file = fs_err::OpenOptions::new()
2285                    .append(true)
2286                    .open(gh_env_file_path)?;
2287                let gh_env_var_assignment = format!(
2288                    r#"{}<<EOF
2289{}
2290EOF
2291"#,
2292                    gh_env_var, var
2293                );
2294                std::io::Write::write_all(&mut gh_env_file, gh_env_var_assignment.as_bytes())?;
2295
2296                Ok(())
2297            }
2298        }
2299    }
2300}
2301
2302/// The base underlying implementation of all FlowNode variants.
2303///
2304/// Do not implement this directly! Use the `new_flow_node!` family of macros
2305/// instead!
2306pub trait FlowNodeBase {
2307    type Request: Serialize + DeserializeOwned;
2308
2309    fn imports(&mut self, ctx: &mut ImportCtx<'_>);
2310    fn emit(&mut self, requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()>;
2311
2312    /// A noop method that all human-written impls of `FlowNodeBase` are
2313    /// required to implement.
2314    ///
2315    /// By implementing this method, you're stating that you "know what you're
2316    /// doing" by having this manual impl.
2317    fn i_know_what_im_doing_with_this_manual_impl(&mut self);
2318}
2319
2320pub mod erased {
2321    use crate::node::FlowNodeBase;
2322    use crate::node::NodeCtx;
2323    use crate::node::user_facing::*;
2324
2325    pub struct ErasedNode<N: FlowNodeBase>(pub N);
2326
2327    impl<N: FlowNodeBase> ErasedNode<N> {
2328        pub fn from_node(node: N) -> Self {
2329            Self(node)
2330        }
2331    }
2332
2333    impl<N> FlowNodeBase for ErasedNode<N>
2334    where
2335        N: FlowNodeBase,
2336    {
2337        // FIXME: this should be using type-erased serde
2338        type Request = Box<[u8]>;
2339
2340        fn imports(&mut self, ctx: &mut ImportCtx<'_>) {
2341            self.0.imports(ctx)
2342        }
2343
2344        fn emit(&mut self, requests: Vec<Box<[u8]>>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
2345            let mut converted_requests = Vec::new();
2346            for req in requests {
2347                converted_requests.push(serde_json::from_slice(&req)?)
2348            }
2349
2350            self.0.emit(converted_requests, ctx)
2351        }
2352
2353        fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2354    }
2355}
2356
2357/// Cheap handle to a registered [`FlowNode`]
2358#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2359pub struct NodeHandle(std::any::TypeId);
2360
2361impl Ord for NodeHandle {
2362    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
2363        self.modpath().cmp(other.modpath())
2364    }
2365}
2366
2367impl PartialOrd for NodeHandle {
2368    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
2369        Some(self.cmp(other))
2370    }
2371}
2372
2373impl std::fmt::Debug for NodeHandle {
2374    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2375        std::fmt::Debug::fmt(&self.try_modpath(), f)
2376    }
2377}
2378
2379impl NodeHandle {
2380    pub fn from_type<N: FlowNodeBase + 'static>() -> NodeHandle {
2381        NodeHandle(std::any::TypeId::of::<N>())
2382    }
2383
2384    pub fn from_modpath(modpath: &str) -> NodeHandle {
2385        node_luts::erased_node_by_modpath().get(modpath).unwrap().0
2386    }
2387
2388    pub fn try_from_modpath(modpath: &str) -> Option<NodeHandle> {
2389        node_luts::erased_node_by_modpath()
2390            .get(modpath)
2391            .map(|(s, _)| *s)
2392    }
2393
2394    pub fn new_erased_node(&self) -> Box<dyn FlowNodeBase<Request = Box<[u8]>>> {
2395        let ctor = node_luts::erased_node_by_typeid().get(self).unwrap();
2396        ctor()
2397    }
2398
2399    pub fn modpath(&self) -> &'static str {
2400        node_luts::modpath_by_node_typeid().get(self).unwrap()
2401    }
2402
2403    pub fn try_modpath(&self) -> Option<&'static str> {
2404        node_luts::modpath_by_node_typeid().get(self).cloned()
2405    }
2406
2407    /// Return a dummy NodeHandle, which will panic if `new_erased_node` is ever
2408    /// called on it.
2409    pub fn dummy() -> NodeHandle {
2410        NodeHandle(std::any::TypeId::of::<()>())
2411    }
2412}
2413
2414pub fn list_all_registered_nodes() -> impl Iterator<Item = NodeHandle> {
2415    node_luts::modpath_by_node_typeid().keys().cloned()
2416}
2417
2418// Encapsulate these look up tables in their own module to limit the scope of
2419// the HashMap import.
2420//
2421// In general, using HashMap in flowey is a recipe for disaster, given that
2422// iterating through the hash-map will result in non-deterministic orderings,
2423// which can cause annoying ordering churn.
2424//
2425// That said, in this case, it's OK since the code using these LUTs won't ever
2426// iterate through the map.
2427//
2428// Why is the HashMap even necessary vs. a BTreeMap?
2429//
2430// Well... NodeHandle's `Ord` impl does a `modpath` comparison instead of a
2431// TypeId comparison, since TypeId will vary between compilations.
2432mod node_luts {
2433    use super::FlowNodeBase;
2434    use super::NodeHandle;
2435    use std::collections::HashMap;
2436    use std::sync::OnceLock;
2437
2438    pub(super) fn modpath_by_node_typeid() -> &'static HashMap<NodeHandle, &'static str> {
2439        static TYPEID_TO_MODPATH: OnceLock<HashMap<NodeHandle, &'static str>> = OnceLock::new();
2440
2441        TYPEID_TO_MODPATH.get_or_init(|| {
2442            let mut lookup = HashMap::new();
2443            for crate::node::private::FlowNodeMeta {
2444                module_path,
2445                ctor: _,
2446                typeid,
2447            } in crate::node::private::FLOW_NODES
2448            {
2449                let existing = lookup.insert(
2450                    NodeHandle(*typeid),
2451                    module_path
2452                        .strip_suffix("::_only_one_call_to_flowey_node_per_module")
2453                        .unwrap(),
2454                );
2455                // if this were to fire for an array where the key is a TypeId...
2456                // something has gone _terribly_ wrong
2457                assert!(existing.is_none())
2458            }
2459
2460            lookup
2461        })
2462    }
2463
2464    pub(super) fn erased_node_by_typeid()
2465    -> &'static HashMap<NodeHandle, fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>> {
2466        static LOOKUP: OnceLock<
2467            HashMap<NodeHandle, fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>>,
2468        > = OnceLock::new();
2469
2470        LOOKUP.get_or_init(|| {
2471            let mut lookup = HashMap::new();
2472            for crate::node::private::FlowNodeMeta {
2473                module_path: _,
2474                ctor,
2475                typeid,
2476            } in crate::node::private::FLOW_NODES
2477            {
2478                let existing = lookup.insert(NodeHandle(*typeid), *ctor);
2479                // if this were to fire for an array where the key is a TypeId...
2480                // something has gone _terribly_ wrong
2481                assert!(existing.is_none())
2482            }
2483
2484            lookup
2485        })
2486    }
2487
2488    pub(super) fn erased_node_by_modpath() -> &'static HashMap<
2489        &'static str,
2490        (
2491            NodeHandle,
2492            fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>,
2493        ),
2494    > {
2495        static MODPATH_LOOKUP: OnceLock<
2496            HashMap<
2497                &'static str,
2498                (
2499                    NodeHandle,
2500                    fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>,
2501                ),
2502            >,
2503        > = OnceLock::new();
2504
2505        MODPATH_LOOKUP.get_or_init(|| {
2506            let mut lookup = HashMap::new();
2507            for crate::node::private::FlowNodeMeta { module_path, ctor, typeid } in crate::node::private::FLOW_NODES {
2508                let existing = lookup.insert(module_path.strip_suffix("::_only_one_call_to_flowey_node_per_module").unwrap(), (NodeHandle(*typeid), *ctor));
2509                if existing.is_some() {
2510                    panic!("conflicting node registrations at {module_path}! please ensure there is a single node per module!")
2511                }
2512            }
2513            lookup
2514        })
2515    }
2516}
2517
2518#[doc(hidden)]
2519pub mod private {
2520    pub use linkme;
2521
2522    pub struct FlowNodeMeta {
2523        pub module_path: &'static str,
2524        pub ctor: fn() -> Box<dyn super::FlowNodeBase<Request = Box<[u8]>>>,
2525        pub typeid: std::any::TypeId,
2526    }
2527
2528    #[linkme::distributed_slice]
2529    pub static FLOW_NODES: [FlowNodeMeta] = [..];
2530
2531    // UNSAFETY: linkme uses manual link sections, which are unsafe.
2532    #[expect(unsafe_code)]
2533    #[linkme::distributed_slice(FLOW_NODES)]
2534    static DUMMY_FLOW_NODE: FlowNodeMeta = FlowNodeMeta {
2535        module_path: "<dummy>::_only_one_call_to_flowey_node_per_module",
2536        ctor: || unreachable!(),
2537        typeid: std::any::TypeId::of::<()>(),
2538    };
2539}
2540
2541#[doc(hidden)]
2542#[macro_export]
2543macro_rules! new_flow_node_base {
2544    (struct Node) => {
2545        /// (see module-level docs)
2546        #[non_exhaustive]
2547        pub struct Node;
2548
2549        mod _only_one_call_to_flowey_node_per_module {
2550            const _: () = {
2551                use $crate::node::private::linkme;
2552
2553                fn new_erased() -> Box<dyn $crate::node::FlowNodeBase<Request = Box<[u8]>>> {
2554                    Box::new($crate::node::erased::ErasedNode(super::Node))
2555                }
2556
2557                #[linkme::distributed_slice($crate::node::private::FLOW_NODES)]
2558                #[linkme(crate = linkme)]
2559                static FLOW_NODE: $crate::node::private::FlowNodeMeta =
2560                    $crate::node::private::FlowNodeMeta {
2561                        module_path: module_path!(),
2562                        ctor: new_erased,
2563                        typeid: std::any::TypeId::of::<super::Node>(),
2564                    };
2565            };
2566        }
2567    };
2568}
2569
2570/// A reusable unit of automation logic in flowey.
2571///
2572/// FlowNodes process requests, emit steps, and can depend on other nodes. They are
2573/// the building blocks for creating complex automation workflows.
2574///
2575/// # The Node/Request Pattern
2576///
2577/// Every node has an associated **Request** type that defines what the node can do.
2578/// Nodes receive a vector of requests and process them together, allowing for
2579/// aggregation and conflict resolution.
2580///
2581/// # Example: Basic FlowNode Implementation
2582///
2583/// ```rust,ignore
2584/// use flowey_core::node::*;
2585///
2586/// // Define the node
2587/// new_flow_node!(struct Node);
2588///
2589/// // Define requests using the flowey_request! macro
2590/// flowey_request! {
2591///     pub enum Request {
2592///         InstallRust(String),                    // Install specific version
2593///         EnsureInstalled(WriteVar<SideEffect>),  // Ensure it's installed
2594///         GetCargoHome(WriteVar<PathBuf>),        // Get CARGO_HOME path
2595///     }
2596/// }
2597///
2598/// impl FlowNode for Node {
2599///     type Request = Request;
2600///     
2601///     fn imports(ctx: &mut ImportCtx<'_>) {
2602///         // Declare node dependencies
2603///         ctx.import::<other_node::Node>();
2604///     }
2605///     
2606///     fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
2607///         // 1. Aggregate and validate requests
2608///         let mut version = None;
2609///         let mut ensure_installed = Vec::new();
2610///         let mut get_cargo_home = Vec::new();
2611///         
2612///         for req in requests {
2613///             match req {
2614///                 Request::InstallRust(v) => {
2615///                     same_across_all_reqs("version", &mut version, v)?;
2616///                 }
2617///                 Request::EnsureInstalled(var) => ensure_installed.push(var),
2618///                 Request::GetCargoHome(var) => get_cargo_home.push(var),
2619///             }
2620///         }
2621///         
2622///         let version = version.ok_or(anyhow::anyhow!("Version not specified"))?;
2623///         
2624///         // 2. Emit steps to do the work
2625///         ctx.emit_rust_step("install rust", |ctx| {
2626///             let ensure_installed = ensure_installed.claim(ctx);
2627///             let get_cargo_home = get_cargo_home.claim(ctx);
2628///             move |rt| {
2629///                 // Install rust with the specified version
2630///                 // Write to all the output variables
2631///                 for var in ensure_installed {
2632///                     rt.write(var, &());
2633///                 }
2634///                 for var in get_cargo_home {
2635///                     rt.write(var, &PathBuf::from("/path/to/cargo"));
2636///                 }
2637///                 Ok(())
2638///             }
2639///         });
2640///         
2641///         Ok(())
2642///     }
2643/// }
2644/// ```
2645///
2646/// # When to Use FlowNode vs SimpleFlowNode
2647///
2648/// **Use `FlowNode`** when you need to:
2649/// - Aggregate multiple requests and process them together
2650/// - Resolve conflicts between requests
2651/// - Perform complex request validation
2652///
2653/// **Use [`SimpleFlowNode`]** when:
2654/// - Each request can be processed independently
2655/// - No aggregation logic is needed
2656pub trait FlowNode {
2657    /// The request type that defines what operations this node can perform.
2658    ///
2659    /// Use the [`crate::flowey_request!`] macro to define this type.
2660    type Request: Serialize + DeserializeOwned;
2661
2662    /// A list of nodes that this node is capable of taking a dependency on.
2663    ///
2664    /// Attempting to take a dep on a node that wasn't imported via this method
2665    /// will result in an error during flow resolution time.
2666    ///
2667    /// * * *
2668    ///
2669    /// To put it bluntly: This is boilerplate.
2670    ///
2671    /// We (the flowey devs) are thinking about ways to avoid requiring this
2672    /// method, but do not have a good solution at this time.
2673    fn imports(ctx: &mut ImportCtx<'_>);
2674
2675    /// Given a set of incoming `requests`, emit various steps to run, set
2676    /// various dependencies, etc...
2677    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()>;
2678}
2679
2680#[macro_export]
2681macro_rules! new_flow_node {
2682    (struct Node) => {
2683        $crate::new_flow_node_base!(struct Node);
2684
2685        impl $crate::node::FlowNodeBase for Node
2686        where
2687            Node: FlowNode,
2688        {
2689            type Request = <Node as FlowNode>::Request;
2690
2691            fn imports(&mut self, dep: &mut $crate::node::ImportCtx<'_>) {
2692                <Node as FlowNode>::imports(dep)
2693            }
2694
2695            fn emit(
2696                &mut self,
2697                requests: Vec<Self::Request>,
2698                ctx: &mut $crate::node::NodeCtx<'_>,
2699            ) -> anyhow::Result<()> {
2700                <Node as FlowNode>::emit(requests, ctx)
2701            }
2702
2703            fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2704        }
2705    };
2706}
2707
2708/// A helper trait to streamline implementing [`FlowNode`] instances that only
2709/// ever operate on a single request at a time.
2710///
2711/// In essence, [`SimpleFlowNode`] handles the boilerplate (and rightward-drift)
2712/// of manually writing:
2713///
2714/// ```ignore
2715/// impl FlowNode for Node {
2716///     fn imports(dep: &mut ImportCtx<'_>) { ... }
2717///     fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) {
2718///         for req in requests {
2719///             Node::process_request(req, ctx)
2720///         }
2721///     }
2722/// }
2723/// ```
2724///
2725/// Nodes which accept a `struct Request` often fall into this pattern, whereas
2726/// nodes which accept a `enum Request` typically require additional logic to
2727/// aggregate / resolve incoming requests.
2728pub trait SimpleFlowNode {
2729    type Request: Serialize + DeserializeOwned;
2730
2731    /// A list of nodes that this node is capable of taking a dependency on.
2732    ///
2733    /// Attempting to take a dep on a node that wasn't imported via this method
2734    /// will result in an error during flow resolution time.
2735    ///
2736    /// * * *
2737    ///
2738    /// To put it bluntly: This is boilerplate.
2739    ///
2740    /// We (the flowey devs) are thinking about ways to avoid requiring this
2741    /// method, but do not have a good solution at this time.
2742    fn imports(ctx: &mut ImportCtx<'_>);
2743
2744    /// Process a single incoming `Self::Request`
2745    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()>;
2746}
2747
2748#[macro_export]
2749macro_rules! new_simple_flow_node {
2750    (struct Node) => {
2751        $crate::new_flow_node_base!(struct Node);
2752
2753        impl $crate::node::FlowNodeBase for Node
2754        where
2755            Node: $crate::node::SimpleFlowNode,
2756        {
2757            type Request = <Node as $crate::node::SimpleFlowNode>::Request;
2758
2759            fn imports(&mut self, dep: &mut $crate::node::ImportCtx<'_>) {
2760                <Node as $crate::node::SimpleFlowNode>::imports(dep)
2761            }
2762
2763            fn emit(&mut self, requests: Vec<Self::Request>, ctx: &mut $crate::node::NodeCtx<'_>) -> anyhow::Result<()> {
2764                for req in requests {
2765                    <Node as $crate::node::SimpleFlowNode>::process_request(req, ctx)?
2766                }
2767
2768                Ok(())
2769            }
2770
2771            fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2772        }
2773    };
2774}
2775
2776/// A "glue" trait which improves [`NodeCtx::req`] ergonomics, by tying a
2777/// particular `Request` type to its corresponding [`FlowNode`].
2778///
2779/// This trait should be autogenerated via [`flowey_request!`] - do not try to
2780/// implement it manually!
2781///
2782/// [`flowey_request!`]: crate::flowey_request
2783pub trait IntoRequest {
2784    type Node: FlowNodeBase;
2785    fn into_request(self) -> <Self::Node as FlowNodeBase>::Request;
2786
2787    /// By implementing this method manually, you're indicating that you know what you're
2788    /// doing,
2789    #[doc(hidden)]
2790    #[expect(nonstandard_style)]
2791    fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self);
2792}
2793
2794#[doc(hidden)]
2795#[macro_export]
2796macro_rules! __flowey_request_inner {
2797    //
2798    // @emit_struct: emit structs for each variant of the request enum
2799    //
2800    (@emit_struct [$req:ident]
2801        $(#[$a:meta])*
2802        $variant:ident($($tt:tt)*),
2803        $($rest:tt)*
2804    ) => {
2805        $(#[$a])*
2806        #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
2807        pub struct $variant($($tt)*);
2808
2809        impl IntoRequest for $variant {
2810            type Node = Node;
2811            fn into_request(self) -> $req {
2812                $req::$variant(self)
2813            }
2814            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
2815        }
2816
2817        $crate::__flowey_request_inner!(@emit_struct [$req] $($rest)*);
2818    };
2819    (@emit_struct [$req:ident]
2820        $(#[$a:meta])*
2821        $variant:ident { $($tt:tt)* },
2822        $($rest:tt)*
2823    ) => {
2824        $(#[$a])*
2825        #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
2826        pub struct $variant {
2827            $($tt)*
2828        }
2829
2830        impl IntoRequest for $variant {
2831            type Node = Node;
2832            fn into_request(self) -> $req {
2833                $req::$variant(self)
2834            }
2835            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
2836        }
2837
2838        $crate::__flowey_request_inner!(@emit_struct [$req] $($rest)*);
2839    };
2840    (@emit_struct [$req:ident]
2841        $(#[$a:meta])*
2842        $variant:ident,
2843        $($rest:tt)*
2844    ) => {
2845        $(#[$a])*
2846        #[derive(Serialize, Deserialize)]
2847        pub struct $variant;
2848
2849        impl IntoRequest for $variant {
2850            type Node = Node;
2851            fn into_request(self) -> $req {
2852                $req::$variant(self)
2853            }
2854            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
2855        }
2856
2857        $crate::__flowey_request_inner!(@emit_struct [$req] $($rest)*);
2858    };
2859    (@emit_struct [$req:ident]
2860    ) => {};
2861
2862    //
2863    // @emit_req_enum: build up root request enum
2864    //
2865    (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
2866        $(#[$a:meta])*
2867        $variant:ident($($tt:tt)*),
2868        $($rest:tt)*
2869    ) => {
2870        $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*), $($prev[$($prev_a,)*])* $variant[$($a,)*]] $($rest)*);
2871    };
2872    (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
2873        $(#[$a:meta])*
2874        $variant:ident { $($tt:tt)* },
2875        $($rest:tt)*
2876    ) => {
2877        $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*), $($prev[$($prev_a,)*])* $variant[$($a,)*]] $($rest)*);
2878    };
2879    (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
2880        $(#[$a:meta])*
2881        $variant:ident,
2882        $($rest:tt)*
2883    ) => {
2884        $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*), $($prev[$($prev_a,)*])* $variant[$($a,)*]] $($rest)*);
2885    };
2886    (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
2887    ) => {
2888        #[derive(Serialize, Deserialize)]
2889        pub enum $req {$(
2890            $(#[$prev_a])*
2891            $prev(self::req::$prev),
2892        )*}
2893
2894        impl IntoRequest for $req {
2895            type Node = Node;
2896            fn into_request(self) -> $req {
2897                self
2898            }
2899            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
2900        }
2901    };
2902}
2903
2904/// Declare a new `Request` type for the current `Node`.
2905///
2906/// ## `struct` and `enum` Requests
2907///
2908/// When wrapping a vanilla Rust `struct` and `enum` declaration, this macro
2909/// simply derives [`Serialize`], [`Deserialize`], and [`IntoRequest`] for the
2910/// type, and does nothing else.
2911///
2912/// ## `enum_struct` Requests
2913///
2914/// This macro also supports a special kind of `enum_struct` derive, which
2915/// allows declaring a Request enum where each variant is split off into its own
2916/// separate (named) `struct`.
2917///
2918/// e.g:
2919///
2920/// ```ignore
2921/// flowey_request! {
2922///     pub enum_struct Foo {
2923///         Bar,
2924///         Baz(pub usize),
2925///         Qux(pub String),
2926///     }
2927/// }
2928/// ```
2929///
2930/// will be expanded into:
2931///
2932/// ```ignore
2933/// #[derive(Serialize, Deserialize)]
2934/// pub enum Foo {
2935///    Bar(req::Bar),
2936///    Baz(req::Baz),
2937///    Qux(req::Qux),
2938/// }
2939///
2940/// pud mod req {
2941///     #[derive(Serialize, Deserialize)]
2942///     pub struct Bar;
2943///
2944///     #[derive(Serialize, Deserialize)]
2945///     pub struct Baz(pub usize);
2946///
2947///     #[derive(Serialize, Deserialize)]
2948///     pub struct Qux(pub String);
2949/// }
2950/// ```
2951#[macro_export]
2952macro_rules! flowey_request {
2953    (
2954        $(#[$root_a:meta])*
2955        pub enum_struct $req:ident {
2956            $($tt:tt)*
2957        }
2958    ) => {
2959        $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*),] $($tt)*);
2960        pub mod req {
2961            use super::*;
2962            $crate::__flowey_request_inner!(@emit_struct [$req] $($tt)*);
2963        }
2964    };
2965
2966    (
2967        $(#[$a:meta])*
2968        pub enum $req:ident {
2969            $($tt:tt)*
2970        }
2971    ) => {
2972        $(#[$a])*
2973        #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
2974        pub enum $req {
2975            $($tt)*
2976        }
2977
2978        impl $crate::node::IntoRequest for $req {
2979            type Node = Node;
2980            fn into_request(self) -> $req {
2981                self
2982            }
2983            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
2984        }
2985    };
2986
2987    (
2988        $(#[$a:meta])*
2989        pub struct $req:ident {
2990            $($tt:tt)*
2991        }
2992    ) => {
2993        $(#[$a])*
2994        #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
2995        pub struct $req {
2996            $($tt)*
2997        }
2998
2999        impl $crate::node::IntoRequest for $req {
3000            type Node = Node;
3001            fn into_request(self) -> $req {
3002                self
3003            }
3004            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3005        }
3006    };
3007
3008    (
3009        $(#[$a:meta])*
3010        pub struct $req:ident($($tt:tt)*);
3011    ) => {
3012        $(#[$a])*
3013        #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
3014        pub struct $req($($tt)*);
3015
3016        impl $crate::node::IntoRequest for $req {
3017            type Node = Node;
3018            fn into_request(self) -> $req {
3019                self
3020            }
3021            fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3022        }
3023    };
3024}