Skip to main content

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