1mod 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
31pub 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
75 pub fn same_across_all_reqs<T: PartialEq>(
107 req_name: &str,
108 var: &mut Option<T>,
109 new: T,
110 ) -> anyhow::Result<()> {
111 match (var.as_ref(), new) {
112 (None, v) => *var = Some(v),
113 (Some(old), new) => {
114 if *old != new {
115 anyhow::bail!("`{}` must be consistent across requests", req_name);
116 }
117 }
118 }
119
120 Ok(())
121 }
122
123 pub fn same_across_all_reqs_backing_var<V: VarEqBacking>(
127 req_name: &str,
128 var: &mut Option<V>,
129 new: V,
130 ) -> anyhow::Result<()> {
131 match (var.as_ref(), new) {
132 (None, v) => *var = Some(v),
133 (Some(old), new) => {
134 if !old.eq(&new) {
135 anyhow::bail!("`{}` must be consistent across requests", req_name);
136 }
137 }
138 }
139
140 Ok(())
141 }
142
143 #[macro_export]
147 macro_rules! match_arch {
148 ($host_arch:expr, $match_arch:pat, $expr:expr) => {
149 if matches!($host_arch, $match_arch) {
150 $expr
151 } else {
152 anyhow::bail!("Linux distro not supported on host arch {}", $host_arch);
153 }
154 };
155 }
156}
157
158pub trait VarEqBacking {
182 fn eq(&self, other: &Self) -> bool;
184}
185
186impl<T> VarEqBacking for WriteVar<T>
187where
188 T: Serialize + DeserializeOwned,
189{
190 fn eq(&self, other: &Self) -> bool {
191 self.backing_var == other.backing_var
192 }
193}
194
195impl<T> VarEqBacking for ReadVar<T>
196where
197 T: Serialize + DeserializeOwned + PartialEq + Eq + Clone,
198{
199 fn eq(&self, other: &Self) -> bool {
200 self.backing_var == other.backing_var
201 }
202}
203
204impl<T, U> VarEqBacking for (T, U)
206where
207 T: VarEqBacking,
208 U: VarEqBacking,
209{
210 fn eq(&self, other: &Self) -> bool {
211 (self.0.eq(&other.0)) && (self.1.eq(&other.1))
212 }
213}
214
215#[derive(Serialize, Deserialize)]
233#[serde(bound(serialize = "T: Serialize", deserialize = "T: DeserializeOwned"))]
234pub struct ConfigVar<T>(pub ReadVar<T>);
235
236impl<T: Serialize + DeserializeOwned> Clone for ConfigVar<T> {
237 fn clone(&self) -> Self {
238 ConfigVar(self.0.clone())
239 }
240}
241
242impl<T> std::fmt::Debug for ConfigVar<T> {
243 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244 f.debug_tuple("ConfigVar").finish()
245 }
246}
247
248impl<T: Serialize + DeserializeOwned + PartialEq + Eq + Clone> PartialEq for ConfigVar<T> {
249 fn eq(&self, other: &Self) -> bool {
250 VarEqBacking::eq(&self.0, &other.0)
251 }
252}
253
254impl<T: Serialize + DeserializeOwned + PartialEq + Eq + Clone> ClaimVar for ConfigVar<T> {
255 type Claimed = ClaimedReadVar<T>;
256
257 fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedReadVar<T> {
258 self.0.claim(ctx)
259 }
260}
261
262impl<T: Serialize + DeserializeOwned + PartialEq + Eq + Clone> From<ReadVar<T>> for ConfigVar<T> {
263 fn from(v: ReadVar<T>) -> Self {
264 ConfigVar(v)
265 }
266}
267
268pub type SideEffect = ();
275
276#[derive(Clone, Debug, Serialize, Deserialize)]
279pub enum VarNotClaimed {}
280
281#[derive(Clone, Debug, Serialize, Deserialize)]
284pub enum VarClaimed {}
285
286#[derive(Debug, Serialize, Deserialize)]
306pub struct WriteVar<T: Serialize + DeserializeOwned, C = VarNotClaimed> {
307 backing_var: String,
308 is_side_effect: bool,
311
312 #[serde(skip)]
313 _kind: core::marker::PhantomData<(T, C)>,
314}
315
316pub type ClaimedWriteVar<T> = WriteVar<T, VarClaimed>;
319
320impl<T: Serialize + DeserializeOwned> WriteVar<T, VarNotClaimed> {
321 fn into_claimed(self) -> WriteVar<T, VarClaimed> {
323 let Self {
324 backing_var,
325 is_side_effect,
326 _kind,
327 } = self;
328
329 WriteVar {
330 backing_var,
331 is_side_effect,
332 _kind: std::marker::PhantomData,
333 }
334 }
335
336 #[track_caller]
338 pub fn write_static(self, ctx: &mut NodeCtx<'_>, val: T)
339 where
340 T: 'static,
341 {
342 let val = ReadVar::from_static(val);
343 val.write_into(ctx, self, |v| v);
344 }
345
346 pub(crate) fn into_json(self) -> WriteVar<serde_json::Value> {
347 WriteVar {
348 backing_var: self.backing_var,
349 is_side_effect: self.is_side_effect,
350 _kind: std::marker::PhantomData,
351 }
352 }
353}
354
355impl WriteVar<SideEffect, VarNotClaimed> {
356 pub fn discard_result<T: Serialize + DeserializeOwned>(self) -> WriteVar<T> {
361 WriteVar {
362 backing_var: self.backing_var,
363 is_side_effect: true,
364 _kind: std::marker::PhantomData,
365 }
366 }
367}
368
369pub trait ClaimVar {
377 type Claimed;
379 fn claim(self, ctx: &mut StepCtx<'_>) -> Self::Claimed;
381}
382
383pub trait ReadVarValue {
389 type Value;
391 fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value;
393}
394
395impl<T: Serialize + DeserializeOwned> ClaimVar for ReadVar<T> {
396 type Claimed = ClaimedReadVar<T>;
397
398 fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedReadVar<T> {
399 if let ReadVarBacking::RuntimeVar {
400 var,
401 is_side_effect: _,
402 } = &self.backing_var
403 {
404 ctx.backend.borrow_mut().on_claimed_runtime_var(var, true);
405 }
406 self.into_claimed()
407 }
408}
409
410impl<T: Serialize + DeserializeOwned> ClaimVar for WriteVar<T> {
411 type Claimed = ClaimedWriteVar<T>;
412
413 fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedWriteVar<T> {
414 ctx.backend
415 .borrow_mut()
416 .on_claimed_runtime_var(&self.backing_var, false);
417 self.into_claimed()
418 }
419}
420
421impl<T: Serialize + DeserializeOwned> ReadVarValue for ClaimedReadVar<T> {
422 type Value = T;
423
424 fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
425 match self.backing_var {
426 ReadVarBacking::RuntimeVar {
427 var,
428 is_side_effect,
429 } => {
430 let data = rt.get_var(&var, is_side_effect);
432 if is_side_effect {
433 serde_json::from_slice(b"null").expect("should be deserializing into ()")
437 } else {
438 serde_json::from_slice(&data).expect("improve this error path")
440 }
441 }
442 ReadVarBacking::Inline(val) => val,
443 }
444 }
445}
446
447impl<T: ClaimVar> ClaimVar for Vec<T> {
448 type Claimed = Vec<T::Claimed>;
449
450 fn claim(self, ctx: &mut StepCtx<'_>) -> Vec<T::Claimed> {
451 self.into_iter().map(|v| v.claim(ctx)).collect()
452 }
453}
454
455impl<T: ReadVarValue> ReadVarValue for Vec<T> {
456 type Value = Vec<T::Value>;
457
458 fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
459 self.into_iter().map(|v| v.read_value(rt)).collect()
460 }
461}
462
463impl<T: ClaimVar> ClaimVar for Option<T> {
464 type Claimed = Option<T::Claimed>;
465
466 fn claim(self, ctx: &mut StepCtx<'_>) -> Option<T::Claimed> {
467 self.map(|x| x.claim(ctx))
468 }
469}
470
471impl<T: ReadVarValue> ReadVarValue for Option<T> {
472 type Value = Option<T::Value>;
473
474 fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
475 self.map(|x| x.read_value(rt))
476 }
477}
478
479impl<U: Ord, T: ClaimVar> ClaimVar for BTreeMap<U, T> {
480 type Claimed = BTreeMap<U, T::Claimed>;
481
482 fn claim(self, ctx: &mut StepCtx<'_>) -> BTreeMap<U, T::Claimed> {
483 self.into_iter().map(|(k, v)| (k, v.claim(ctx))).collect()
484 }
485}
486
487impl<U: Ord, T: ReadVarValue> ReadVarValue for BTreeMap<U, T> {
488 type Value = BTreeMap<U, T::Value>;
489
490 fn read_value(self, rt: &mut RustRuntimeServices<'_>) -> Self::Value {
491 self.into_iter()
492 .map(|(k, v)| (k, v.read_value(rt)))
493 .collect()
494 }
495}
496
497macro_rules! impl_tuple_claim {
498 ($($T:tt)*) => {
499 impl<$($T,)*> $crate::node::ClaimVar for ($($T,)*)
500 where
501 $($T: $crate::node::ClaimVar,)*
502 {
503 type Claimed = ($($T::Claimed,)*);
504
505 #[expect(non_snake_case)]
506 fn claim(self, ctx: &mut $crate::node::StepCtx<'_>) -> Self::Claimed {
507 let ($($T,)*) = self;
508 ($($T.claim(ctx),)*)
509 }
510 }
511
512 impl<$($T,)*> $crate::node::ReadVarValue for ($($T,)*)
513 where
514 $($T: $crate::node::ReadVarValue,)*
515 {
516 type Value = ($($T::Value,)*);
517
518 #[expect(non_snake_case)]
519 fn read_value(self, rt: &mut $crate::node::RustRuntimeServices<'_>) -> Self::Value {
520 let ($($T,)*) = self;
521 ($($T.read_value(rt),)*)
522 }
523 }
524 };
525}
526
527impl_tuple_claim!(A B C D E F G H I J);
528impl_tuple_claim!(A B C D E F G H I);
529impl_tuple_claim!(A B C D E F G H);
530impl_tuple_claim!(A B C D E F G);
531impl_tuple_claim!(A B C D E F);
532impl_tuple_claim!(A B C D E);
533impl_tuple_claim!(A B C D);
534impl_tuple_claim!(A B C);
535impl_tuple_claim!(A B);
536impl_tuple_claim!(A);
537
538impl ClaimVar for () {
539 type Claimed = ();
540
541 fn claim(self, _ctx: &mut StepCtx<'_>) -> Self::Claimed {}
542}
543
544impl ReadVarValue for () {
545 type Value = ();
546
547 fn read_value(self, _rt: &mut RustRuntimeServices<'_>) -> Self::Value {}
548}
549
550#[derive(Serialize, Deserialize, Clone)]
555pub struct GhUserSecretVar(pub(crate) String);
556
557#[derive(Debug, Serialize, Deserialize)]
576pub struct ReadVar<T, C = VarNotClaimed> {
577 backing_var: ReadVarBacking<T>,
578 #[serde(skip)]
579 _kind: std::marker::PhantomData<C>,
580}
581
582pub type ClaimedReadVar<T> = ReadVar<T, VarClaimed>;
585
586impl<T: Serialize + DeserializeOwned, C> Clone for ReadVar<T, C> {
588 fn clone(&self) -> Self {
589 ReadVar {
590 backing_var: self.backing_var.clone(),
591 _kind: std::marker::PhantomData,
592 }
593 }
594}
595
596#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
597enum ReadVarBacking<T> {
598 RuntimeVar {
599 var: String,
600 is_side_effect: bool,
607 },
608 Inline(T),
609}
610
611impl<T: Serialize + DeserializeOwned> Clone for ReadVarBacking<T> {
613 fn clone(&self) -> Self {
614 match self {
615 Self::RuntimeVar {
616 var,
617 is_side_effect,
618 } => Self::RuntimeVar {
619 var: var.clone(),
620 is_side_effect: *is_side_effect,
621 },
622 Self::Inline(v) => {
623 Self::Inline(serde_json::from_value(serde_json::to_value(v).unwrap()).unwrap())
624 }
625 }
626 }
627}
628
629impl<T: Serialize + DeserializeOwned> ReadVar<T> {
630 fn into_claimed(self) -> ReadVar<T, VarClaimed> {
632 let Self { backing_var, _kind } = self;
633
634 ReadVar {
635 backing_var,
636 _kind: std::marker::PhantomData,
637 }
638 }
639
640 #[must_use]
649 pub fn into_side_effect(self) -> ReadVar<SideEffect> {
650 ReadVar {
651 backing_var: match self.backing_var {
652 ReadVarBacking::RuntimeVar {
653 var,
654 is_side_effect: _,
655 } => ReadVarBacking::RuntimeVar {
656 var,
657 is_side_effect: true,
658 },
659 ReadVarBacking::Inline(_) => ReadVarBacking::Inline(()),
660 },
661 _kind: std::marker::PhantomData,
662 }
663 }
664
665 #[track_caller]
668 #[must_use]
669 pub fn map<F, U>(&self, ctx: &mut NodeCtx<'_>, f: F) -> ReadVar<U>
670 where
671 T: 'static,
672 U: Serialize + DeserializeOwned + 'static,
673 F: FnOnce(T) -> U + 'static,
674 {
675 let (read_from, write_into) = ctx.new_var();
676 self.write_into(ctx, write_into, f);
677 read_from
678 }
679
680 #[track_caller]
683 pub fn write_into<F, U>(&self, ctx: &mut NodeCtx<'_>, write_into: WriteVar<U>, f: F)
684 where
685 T: 'static,
686 U: Serialize + DeserializeOwned + 'static,
687 F: FnOnce(T) -> U + 'static,
688 {
689 let this = self.clone();
690 ctx.emit_minor_rust_step("🌼 write_into Var", move |ctx| {
691 let this = this.claim(ctx);
692 let write_into = write_into.claim(ctx);
693 move |rt| {
694 let this = rt.read(this);
695 rt.write(write_into, &f(this));
696 }
697 });
698 }
699
700 #[track_caller]
703 #[must_use]
704 pub fn zip<U>(&self, ctx: &mut NodeCtx<'_>, other: ReadVar<U>) -> ReadVar<(T, U)>
705 where
706 T: 'static,
707 U: Serialize + DeserializeOwned + 'static,
708 {
709 let (read_from, write_into) = ctx.new_var();
710 let this = self.clone();
711 ctx.emit_minor_rust_step("🌼 Zip Vars", move |ctx| {
712 let this = this.claim(ctx);
713 let other = other.claim(ctx);
714 let write_into = write_into.claim(ctx);
715 move |rt| {
716 let this = rt.read(this);
717 let other = rt.read(other);
718 rt.write(write_into, &(this, other));
719 }
720 });
721 read_from
722 }
723
724 #[track_caller]
729 #[must_use]
730 pub fn from_static(val: T) -> ReadVar<T>
731 where
732 T: 'static,
733 {
734 ReadVar {
735 backing_var: ReadVarBacking::Inline(val),
736 _kind: std::marker::PhantomData,
737 }
738 }
739
740 pub fn get_static(&self) -> Option<T> {
749 match self.clone().backing_var {
750 ReadVarBacking::Inline(v) => Some(v),
751 _ => None,
752 }
753 }
754
755 #[track_caller]
757 #[must_use]
758 pub fn transpose_vec(ctx: &mut NodeCtx<'_>, vec: Vec<ReadVar<T>>) -> ReadVar<Vec<T>>
759 where
760 T: 'static,
761 {
762 let (read_from, write_into) = ctx.new_var();
763 ctx.emit_minor_rust_step("🌼 Transpose Vec<ReadVar<T>>", move |ctx| {
764 let vec = vec.claim(ctx);
765 let write_into = write_into.claim(ctx);
766 move |rt| {
767 let mut v = Vec::new();
768 for var in vec {
769 v.push(rt.read(var));
770 }
771 rt.write(write_into, &v);
772 }
773 });
774 read_from
775 }
776
777 #[must_use]
793 pub fn depending_on<U>(&self, ctx: &mut NodeCtx<'_>, other: &ReadVar<U>) -> Self
794 where
795 T: 'static,
796 U: Serialize + DeserializeOwned + 'static,
797 {
798 ctx.emit_minor_rust_stepv("🌼 Add dependency", |ctx| {
801 let this = self.clone().claim(ctx);
802 other.clone().claim(ctx);
803 move |rt| rt.read(this)
804 })
805 }
806
807 pub fn claim_unused(self, ctx: &mut NodeCtx<'_>) {
810 match self.backing_var {
811 ReadVarBacking::RuntimeVar {
812 var,
813 is_side_effect: _,
814 } => ctx.backend.borrow_mut().on_unused_read_var(&var),
815 ReadVarBacking::Inline(_) => {}
816 }
817 }
818
819 pub(crate) fn into_json(self) -> ReadVar<serde_json::Value> {
820 match self.backing_var {
821 ReadVarBacking::RuntimeVar {
822 var,
823 is_side_effect,
824 } => ReadVar {
825 backing_var: ReadVarBacking::RuntimeVar {
826 var,
827 is_side_effect,
828 },
829 _kind: std::marker::PhantomData,
830 },
831 ReadVarBacking::Inline(v) => ReadVar {
832 backing_var: ReadVarBacking::Inline(serde_json::to_value(v).unwrap()),
833 _kind: std::marker::PhantomData,
834 },
835 }
836 }
837}
838
839#[must_use]
845pub fn thin_air_read_runtime_var<T>(backing_var: String) -> ReadVar<T>
846where
847 T: Serialize + DeserializeOwned,
848{
849 ReadVar {
850 backing_var: ReadVarBacking::RuntimeVar {
851 var: backing_var,
852 is_side_effect: false,
853 },
854 _kind: std::marker::PhantomData,
855 }
856}
857
858#[must_use]
864pub fn thin_air_write_runtime_var<T>(backing_var: String) -> WriteVar<T>
865where
866 T: Serialize + DeserializeOwned,
867{
868 WriteVar {
869 backing_var,
870 is_side_effect: false,
871 _kind: std::marker::PhantomData,
872 }
873}
874
875pub fn read_var_internals<T: Serialize + DeserializeOwned, C>(
881 var: &ReadVar<T, C>,
882) -> (Option<String>, bool) {
883 match var.backing_var {
884 ReadVarBacking::RuntimeVar {
885 var: ref s,
886 is_side_effect,
887 } => (Some(s.clone()), is_side_effect),
888 ReadVarBacking::Inline(_) => (None, false),
889 }
890}
891
892pub trait ImportCtxBackend {
893 fn on_possible_dep(&mut self, node_handle: NodeHandle);
894}
895
896pub struct ImportCtx<'a> {
898 backend: &'a mut dyn ImportCtxBackend,
899}
900
901impl ImportCtx<'_> {
902 pub fn import<N: FlowNodeBase + 'static>(&mut self) {
904 self.backend.on_possible_dep(NodeHandle::from_type::<N>())
905 }
906}
907
908pub fn new_import_ctx(backend: &mut dyn ImportCtxBackend) -> ImportCtx<'_> {
909 ImportCtx { backend }
910}
911
912#[derive(Debug)]
913pub enum CtxAnchor {
914 PostJob,
915}
916
917pub trait NodeCtxBackend {
918 fn current_node(&self) -> NodeHandle;
920
921 fn on_new_var(&mut self) -> String;
926
927 fn on_claimed_runtime_var(&mut self, var: &str, is_read: bool);
929
930 fn on_unused_read_var(&mut self, var: &str);
932
933 fn on_request(&mut self, node_handle: NodeHandle, req: anyhow::Result<Box<[u8]>>);
941
942 fn on_config(&mut self, node_handle: NodeHandle, config: anyhow::Result<Box<[u8]>>);
946
947 fn on_emit_rust_step(
948 &mut self,
949 label: &str,
950 can_merge: bool,
951 code: Box<dyn for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()>>,
952 );
953
954 fn on_emit_ado_step(
955 &mut self,
956 label: &str,
957 yaml_snippet: Box<dyn for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String>,
958 inline_script: Option<
959 Box<dyn for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()>>,
960 >,
961 condvar: Option<String>,
962 );
963
964 fn on_emit_gh_step(
965 &mut self,
966 label: &str,
967 uses: &str,
968 with: BTreeMap<String, ClaimedGhParam>,
969 condvar: Option<String>,
970 outputs: BTreeMap<String, Vec<GhOutput>>,
971 permissions: BTreeMap<GhPermission, GhPermissionValue>,
972 gh_to_rust: Vec<GhToRust>,
973 rust_to_gh: Vec<RustToGh>,
974 );
975
976 fn on_emit_side_effect_step(&mut self);
977
978 fn backend(&mut self) -> FlowBackend;
979 fn platform(&mut self) -> FlowPlatform;
980 fn arch(&mut self) -> FlowArch;
981
982 fn persistent_dir_path_var(&mut self) -> Option<String>;
986}
987
988pub fn new_node_ctx(backend: &mut dyn NodeCtxBackend) -> NodeCtx<'_> {
989 NodeCtx {
990 backend: Rc::new(RefCell::new(backend)),
991 }
992}
993
994#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
996pub enum FlowBackend {
997 Local,
999 Ado,
1001 Github,
1003}
1004
1005#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1007pub enum FlowPlatformKind {
1008 Windows,
1009 Unix,
1010}
1011
1012#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
1014pub enum FlowPlatformLinuxDistro {
1015 Fedora,
1017 Ubuntu,
1019 AzureLinux,
1021 Arch,
1023 Nix,
1025 Unknown,
1027}
1028
1029#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
1031#[non_exhaustive]
1032pub enum FlowPlatform {
1033 Windows,
1035 Linux(FlowPlatformLinuxDistro),
1037 MacOs,
1039}
1040
1041impl FlowPlatform {
1042 pub fn kind(&self) -> FlowPlatformKind {
1043 match self {
1044 Self::Windows => FlowPlatformKind::Windows,
1045 Self::Linux(_) | Self::MacOs => FlowPlatformKind::Unix,
1046 }
1047 }
1048
1049 fn as_str(&self) -> &'static str {
1050 match self {
1051 Self::Windows => "windows",
1052 Self::Linux(_) => "linux",
1053 Self::MacOs => "macos",
1054 }
1055 }
1056
1057 pub fn exe_suffix(&self) -> &'static str {
1059 if self == &Self::Windows { ".exe" } else { "" }
1060 }
1061
1062 pub fn binary(&self, name: &str) -> String {
1064 format!("{}{}", name, self.exe_suffix())
1065 }
1066}
1067
1068impl std::fmt::Display for FlowPlatform {
1069 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1070 f.pad(self.as_str())
1071 }
1072}
1073
1074#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
1076#[non_exhaustive]
1077pub enum FlowArch {
1078 X86_64,
1079 Aarch64,
1080}
1081
1082impl FlowArch {
1083 fn as_str(&self) -> &'static str {
1084 match self {
1085 Self::X86_64 => "x86_64",
1086 Self::Aarch64 => "aarch64",
1087 }
1088 }
1089}
1090
1091impl std::fmt::Display for FlowArch {
1092 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1093 f.pad(self.as_str())
1094 }
1095}
1096
1097pub struct StepCtx<'a> {
1099 backend: Rc<RefCell<&'a mut dyn NodeCtxBackend>>,
1100}
1101
1102impl StepCtx<'_> {
1103 pub fn backend(&self) -> FlowBackend {
1106 self.backend.borrow_mut().backend()
1107 }
1108
1109 pub fn platform(&self) -> FlowPlatform {
1112 self.backend.borrow_mut().platform()
1113 }
1114}
1115
1116const NO_ADO_INLINE_SCRIPT: Option<
1117 for<'a> fn(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()>,
1118> = None;
1119
1120pub struct NodeCtx<'a> {
1122 backend: Rc<RefCell<&'a mut dyn NodeCtxBackend>>,
1123}
1124
1125impl<'ctx> NodeCtx<'ctx> {
1126 pub fn emit_rust_step<F, G>(&mut self, label: impl AsRef<str>, code: F) -> ReadVar<SideEffect>
1132 where
1133 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1134 G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1135 {
1136 self.emit_rust_step_inner(label.as_ref(), false, code)
1137 }
1138
1139 pub fn emit_minor_rust_step<F, G>(
1149 &mut self,
1150 label: impl AsRef<str>,
1151 code: F,
1152 ) -> ReadVar<SideEffect>
1153 where
1154 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1155 G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) + 'static,
1156 {
1157 self.emit_rust_step_inner(label.as_ref(), true, |ctx| {
1158 let f = code(ctx);
1159 |rt| {
1160 f(rt);
1161 Ok(())
1162 }
1163 })
1164 }
1165
1166 #[must_use]
1187 #[track_caller]
1188 pub fn emit_rust_stepv<T, F, G>(&mut self, label: impl AsRef<str>, code: F) -> ReadVar<T>
1189 where
1190 T: Serialize + DeserializeOwned + 'static,
1191 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1192 G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<T> + 'static,
1193 {
1194 self.emit_rust_stepv_inner(label.as_ref(), false, code)
1195 }
1196
1197 #[must_use]
1221 #[track_caller]
1222 pub fn emit_minor_rust_stepv<T, F, G>(&mut self, label: impl AsRef<str>, code: F) -> ReadVar<T>
1223 where
1224 T: Serialize + DeserializeOwned + 'static,
1225 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1226 G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> T + 'static,
1227 {
1228 self.emit_rust_stepv_inner(label.as_ref(), true, |ctx| {
1229 let f = code(ctx);
1230 |rt| Ok(f(rt))
1231 })
1232 }
1233
1234 fn emit_rust_step_inner<F, G>(
1235 &mut self,
1236 label: &str,
1237 can_merge: bool,
1238 code: F,
1239 ) -> ReadVar<SideEffect>
1240 where
1241 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1242 G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1243 {
1244 let (read, write) = self.new_prefixed_var("auto_se");
1245
1246 let ctx = &mut StepCtx {
1247 backend: self.backend.clone(),
1248 };
1249 write.claim(ctx);
1250
1251 let code = code(ctx);
1252 self.backend
1253 .borrow_mut()
1254 .on_emit_rust_step(label.as_ref(), can_merge, Box::new(code));
1255 read
1256 }
1257
1258 #[must_use]
1259 #[track_caller]
1260 fn emit_rust_stepv_inner<T, F, G>(
1261 &mut self,
1262 label: impl AsRef<str>,
1263 can_merge: bool,
1264 code: F,
1265 ) -> ReadVar<T>
1266 where
1267 T: Serialize + DeserializeOwned + 'static,
1268 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1269 G: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<T> + 'static,
1270 {
1271 let (read, write) = self.new_var();
1272
1273 let ctx = &mut StepCtx {
1274 backend: self.backend.clone(),
1275 };
1276 let write = write.claim(ctx);
1277
1278 let code = code(ctx);
1279 self.backend.borrow_mut().on_emit_rust_step(
1280 label.as_ref(),
1281 can_merge,
1282 Box::new(|rt| {
1283 let val = code(rt)?;
1284 rt.write(write, &val);
1285 Ok(())
1286 }),
1287 );
1288 read
1289 }
1290
1291 #[track_caller]
1293 #[must_use]
1294 pub fn get_ado_variable(&mut self, ado_var: AdoRuntimeVar) -> ReadVar<String> {
1295 let (var, write_var) = self.new_var();
1296 self.emit_ado_step(format!("🌼 read {}", ado_var.as_raw_var_name()), |ctx| {
1297 let write_var = write_var.claim(ctx);
1298 |rt| {
1299 rt.set_var(write_var, ado_var);
1300 "".into()
1301 }
1302 });
1303 var
1304 }
1305
1306 pub fn emit_ado_step<F, G>(&mut self, display_name: impl AsRef<str>, yaml_snippet: F)
1308 where
1309 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1310 G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1311 {
1312 self.emit_ado_step_inner(display_name, None, |ctx| {
1313 (yaml_snippet(ctx), NO_ADO_INLINE_SCRIPT)
1314 })
1315 }
1316
1317 pub fn emit_ado_step_with_condition<F, G>(
1320 &mut self,
1321 display_name: impl AsRef<str>,
1322 cond: ReadVar<bool>,
1323 yaml_snippet: F,
1324 ) where
1325 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1326 G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1327 {
1328 self.emit_ado_step_inner(display_name, Some(cond), |ctx| {
1329 (yaml_snippet(ctx), NO_ADO_INLINE_SCRIPT)
1330 })
1331 }
1332
1333 pub fn emit_ado_step_with_condition_optional<F, G>(
1336 &mut self,
1337 display_name: impl AsRef<str>,
1338 cond: Option<ReadVar<bool>>,
1339 yaml_snippet: F,
1340 ) where
1341 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> G,
1342 G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1343 {
1344 self.emit_ado_step_inner(display_name, cond, |ctx| {
1345 (yaml_snippet(ctx), NO_ADO_INLINE_SCRIPT)
1346 })
1347 }
1348
1349 pub fn emit_ado_step_with_inline_script<F, G, H>(
1378 &mut self,
1379 display_name: impl AsRef<str>,
1380 yaml_snippet: F,
1381 ) where
1382 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> (G, H),
1383 G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1384 H: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1385 {
1386 self.emit_ado_step_inner(display_name, None, |ctx| {
1387 let (f, g) = yaml_snippet(ctx);
1388 (f, Some(g))
1389 })
1390 }
1391
1392 fn emit_ado_step_inner<F, G, H>(
1393 &mut self,
1394 display_name: impl AsRef<str>,
1395 cond: Option<ReadVar<bool>>,
1396 yaml_snippet: F,
1397 ) where
1398 F: for<'a> FnOnce(&'a mut StepCtx<'_>) -> (G, Option<H>),
1399 G: for<'a> FnOnce(&'a mut AdoStepServices<'_>) -> String + 'static,
1400 H: for<'a> FnOnce(&'a mut RustRuntimeServices<'_>) -> anyhow::Result<()> + 'static,
1401 {
1402 let condvar = match cond.map(|c| c.backing_var) {
1403 Some(ReadVarBacking::Inline(cond)) => {
1405 if !cond {
1406 return;
1407 } else {
1408 None
1409 }
1410 }
1411 Some(ReadVarBacking::RuntimeVar {
1412 var,
1413 is_side_effect,
1414 }) => {
1415 assert!(!is_side_effect);
1416 self.backend.borrow_mut().on_claimed_runtime_var(&var, true);
1417 Some(var)
1418 }
1419 None => None,
1420 };
1421
1422 let (yaml_snippet, inline_script) = yaml_snippet(&mut StepCtx {
1423 backend: self.backend.clone(),
1424 });
1425 self.backend.borrow_mut().on_emit_ado_step(
1426 display_name.as_ref(),
1427 Box::new(yaml_snippet),
1428 if let Some(inline_script) = inline_script {
1429 Some(Box::new(inline_script))
1430 } else {
1431 None
1432 },
1433 condvar,
1434 );
1435 }
1436
1437 #[track_caller]
1439 #[must_use]
1440 pub fn get_gh_context_var(&mut self) -> GhContextVarReader<'ctx, Root> {
1441 GhContextVarReader {
1442 ctx: NodeCtx {
1443 backend: self.backend.clone(),
1444 },
1445 _state: std::marker::PhantomData,
1446 }
1447 }
1448
1449 pub fn emit_gh_step(
1451 &mut self,
1452 display_name: impl AsRef<str>,
1453 uses: impl AsRef<str>,
1454 ) -> GhStepBuilder {
1455 GhStepBuilder::new(display_name, uses)
1456 }
1457
1458 fn emit_gh_step_inner(
1459 &mut self,
1460 display_name: impl AsRef<str>,
1461 cond: Option<ReadVar<bool>>,
1462 uses: impl AsRef<str>,
1463 with: Option<BTreeMap<String, GhParam>>,
1464 outputs: BTreeMap<String, Vec<WriteVar<String>>>,
1465 run_after: Vec<ReadVar<SideEffect>>,
1466 permissions: BTreeMap<GhPermission, GhPermissionValue>,
1467 ) {
1468 let condvar = match cond.map(|c| c.backing_var) {
1469 Some(ReadVarBacking::Inline(cond)) => {
1471 if !cond {
1472 return;
1473 } else {
1474 None
1475 }
1476 }
1477 Some(ReadVarBacking::RuntimeVar {
1478 var,
1479 is_side_effect,
1480 }) => {
1481 assert!(!is_side_effect);
1482 self.backend.borrow_mut().on_claimed_runtime_var(&var, true);
1483 Some(var)
1484 }
1485 None => None,
1486 };
1487
1488 let with = with
1489 .unwrap_or_default()
1490 .into_iter()
1491 .map(|(k, v)| {
1492 (
1493 k.clone(),
1494 v.claim(&mut StepCtx {
1495 backend: self.backend.clone(),
1496 }),
1497 )
1498 })
1499 .collect();
1500
1501 for var in run_after {
1502 var.claim(&mut StepCtx {
1503 backend: self.backend.clone(),
1504 });
1505 }
1506
1507 let outputvars = outputs
1508 .into_iter()
1509 .map(|(name, vars)| {
1510 (
1511 name,
1512 vars.into_iter()
1513 .map(|var| {
1514 let var = var.claim(&mut StepCtx {
1515 backend: self.backend.clone(),
1516 });
1517 GhOutput {
1518 backing_var: var.backing_var,
1519 is_secret: false,
1520 is_object: false,
1521 }
1522 })
1523 .collect(),
1524 )
1525 })
1526 .collect();
1527
1528 self.backend.borrow_mut().on_emit_gh_step(
1529 display_name.as_ref(),
1530 uses.as_ref(),
1531 with,
1532 condvar,
1533 outputvars,
1534 permissions,
1535 Vec::new(),
1536 Vec::new(),
1537 );
1538 }
1539
1540 pub fn emit_side_effect_step(
1548 &mut self,
1549 use_side_effects: impl IntoIterator<Item = ReadVar<SideEffect>>,
1550 resolve_side_effects: impl IntoIterator<Item = WriteVar<SideEffect>>,
1551 ) {
1552 let mut backend = self.backend.borrow_mut();
1553 for var in use_side_effects.into_iter() {
1554 if let ReadVarBacking::RuntimeVar {
1555 var,
1556 is_side_effect: _,
1557 } = &var.backing_var
1558 {
1559 backend.on_claimed_runtime_var(var, true);
1560 }
1561 }
1562
1563 for var in resolve_side_effects.into_iter() {
1564 backend.on_claimed_runtime_var(&var.backing_var, false);
1565 }
1566
1567 backend.on_emit_side_effect_step();
1568 }
1569
1570 pub fn backend(&self) -> FlowBackend {
1573 self.backend.borrow_mut().backend()
1574 }
1575
1576 pub fn platform(&self) -> FlowPlatform {
1579 self.backend.borrow_mut().platform()
1580 }
1581
1582 pub fn arch(&self) -> FlowArch {
1584 self.backend.borrow_mut().arch()
1585 }
1586
1587 pub fn req<R>(&mut self, req: R)
1589 where
1590 R: IntoRequest + 'static,
1591 {
1592 let mut backend = self.backend.borrow_mut();
1593 backend.on_request(
1594 NodeHandle::from_type::<R::Node>(),
1595 serde_json::to_vec(&req.into_request())
1596 .map(Into::into)
1597 .map_err(Into::into),
1598 );
1599 }
1600
1601 pub fn config<C>(&mut self, config: C)
1606 where
1607 C: IntoConfig + 'static,
1608 {
1609 let mut backend = self.backend.borrow_mut();
1610 backend.on_config(
1611 NodeHandle::from_type::<C::Node>(),
1612 serde_json::to_vec(&config)
1613 .map(Into::into)
1614 .map_err(Into::into),
1615 );
1616 }
1617
1618 #[track_caller]
1621 #[must_use]
1622 pub fn reqv<T, R>(&mut self, f: impl FnOnce(WriteVar<T>) -> R) -> ReadVar<T>
1623 where
1624 T: Serialize + DeserializeOwned,
1625 R: IntoRequest + 'static,
1626 {
1627 let (read, write) = self.new_var();
1628 self.req::<R>(f(write));
1629 read
1630 }
1631
1632 pub fn requests<N>(&mut self, reqs: impl IntoIterator<Item = N::Request>)
1634 where
1635 N: FlowNodeBase + 'static,
1636 {
1637 let mut backend = self.backend.borrow_mut();
1638 for req in reqs.into_iter() {
1639 backend.on_request(
1640 NodeHandle::from_type::<N>(),
1641 serde_json::to_vec(&req).map(Into::into).map_err(Into::into),
1642 );
1643 }
1644 }
1645
1646 #[track_caller]
1649 #[must_use]
1650 pub fn new_var<T>(&self) -> (ReadVar<T>, WriteVar<T>)
1651 where
1652 T: Serialize + DeserializeOwned,
1653 {
1654 self.new_prefixed_var("")
1655 }
1656
1657 #[track_caller]
1658 #[must_use]
1659 fn new_prefixed_var<T>(&self, prefix: &'static str) -> (ReadVar<T>, WriteVar<T>)
1660 where
1661 T: Serialize + DeserializeOwned,
1662 {
1663 let caller = std::panic::Location::caller()
1665 .to_string()
1666 .replace('\\', "/");
1667
1668 let caller = caller
1684 .split_once("flowey/")
1685 .expect("due to a known limitation with flowey, all flowey code must have an ancestor dir called 'flowey/' somewhere in its full path")
1686 .1;
1687
1688 let colon = if prefix.is_empty() { "" } else { ":" };
1689 let ordinal = self.backend.borrow_mut().on_new_var();
1690 let backing_var = format!("{prefix}{colon}{ordinal}:{caller}");
1691
1692 (
1693 ReadVar {
1694 backing_var: ReadVarBacking::RuntimeVar {
1695 var: backing_var.clone(),
1696 is_side_effect: false,
1697 },
1698 _kind: std::marker::PhantomData,
1699 },
1700 WriteVar {
1701 backing_var,
1702 is_side_effect: false,
1703 _kind: std::marker::PhantomData,
1704 },
1705 )
1706 }
1707
1708 #[track_caller]
1719 #[must_use]
1720 pub fn new_post_job_side_effect(&self) -> (ReadVar<SideEffect>, WriteVar<SideEffect>) {
1721 self.new_prefixed_var("post_job")
1722 }
1723
1724 #[track_caller]
1737 #[must_use]
1738 pub fn persistent_dir(&mut self) -> Option<ReadVar<PathBuf>> {
1739 let path: ReadVar<PathBuf> = ReadVar {
1740 backing_var: ReadVarBacking::RuntimeVar {
1741 var: self.backend.borrow_mut().persistent_dir_path_var()?,
1742 is_side_effect: false,
1743 },
1744 _kind: std::marker::PhantomData,
1745 };
1746
1747 let folder_name = self
1748 .backend
1749 .borrow_mut()
1750 .current_node()
1751 .modpath()
1752 .replace("::", "__");
1753
1754 Some(
1755 self.emit_rust_stepv("🌼 Create persistent store dir", |ctx| {
1756 let path = path.claim(ctx);
1757 |rt| {
1758 let dir = rt.read(path).join(folder_name);
1759 fs_err::create_dir_all(&dir)?;
1760 Ok(dir)
1761 }
1762 }),
1763 )
1764 }
1765
1766 pub fn supports_persistent_dir(&mut self) -> bool {
1768 self.backend
1769 .borrow_mut()
1770 .persistent_dir_path_var()
1771 .is_some()
1772 }
1773}
1774
1775pub trait RuntimeVarDb {
1778 fn get_var(&mut self, var_name: &str) -> (Vec<u8>, bool) {
1779 self.try_get_var(var_name)
1780 .unwrap_or_else(|| panic!("db is missing var {}", var_name))
1781 }
1782
1783 fn try_get_var(&mut self, var_name: &str) -> Option<(Vec<u8>, bool)>;
1784 fn set_var(&mut self, var_name: &str, is_secret: bool, value: Vec<u8>);
1785}
1786
1787impl RuntimeVarDb for Box<dyn RuntimeVarDb> {
1788 fn try_get_var(&mut self, var_name: &str) -> Option<(Vec<u8>, bool)> {
1789 (**self).try_get_var(var_name)
1790 }
1791
1792 fn set_var(&mut self, var_name: &str, is_secret: bool, value: Vec<u8>) {
1793 (**self).set_var(var_name, is_secret, value)
1794 }
1795}
1796
1797pub mod steps {
1798 pub mod ado {
1799 use crate::node::ClaimedReadVar;
1800 use crate::node::ClaimedWriteVar;
1801 use crate::node::ReadVarBacking;
1802 use serde::Deserialize;
1803 use serde::Serialize;
1804 use std::borrow::Cow;
1805
1806 #[derive(Debug, Clone, Serialize, Deserialize)]
1812 pub struct AdoResourcesRepositoryId {
1813 pub(crate) repo_id: String,
1814 }
1815
1816 impl AdoResourcesRepositoryId {
1817 pub fn new_self() -> Self {
1823 Self {
1824 repo_id: "self".into(),
1825 }
1826 }
1827
1828 pub fn dangerous_get_raw_id(&self) -> &str {
1834 &self.repo_id
1835 }
1836
1837 pub fn dangerous_new(repo_id: &str) -> Self {
1843 Self {
1844 repo_id: repo_id.into(),
1845 }
1846 }
1847 }
1848
1849 #[derive(Clone, Debug, Serialize, Deserialize)]
1854 pub struct AdoRuntimeVar {
1855 is_secret: bool,
1856 ado_var: Cow<'static, str>,
1857 }
1858
1859 impl AdoRuntimeVar {
1860 pub const BUILD_SOURCE_BRANCH: AdoRuntimeVar = AdoRuntimeVar::new("build.SourceBranch");
1866
1867 pub const BUILD_BUILD_NUMBER: AdoRuntimeVar = AdoRuntimeVar::new("build.BuildNumber");
1869
1870 pub const SYSTEM_ACCESS_TOKEN: AdoRuntimeVar =
1872 AdoRuntimeVar::new_secret("System.AccessToken");
1873
1874 pub const SYSTEM_JOB_ATTEMPT: AdoRuntimeVar =
1876 AdoRuntimeVar::new_secret("System.JobAttempt");
1877 }
1878
1879 impl AdoRuntimeVar {
1880 const fn new(s: &'static str) -> Self {
1881 Self {
1882 is_secret: false,
1883 ado_var: Cow::Borrowed(s),
1884 }
1885 }
1886
1887 const fn new_secret(s: &'static str) -> Self {
1888 Self {
1889 is_secret: true,
1890 ado_var: Cow::Borrowed(s),
1891 }
1892 }
1893
1894 pub fn is_secret(&self) -> bool {
1896 self.is_secret
1897 }
1898
1899 pub fn as_raw_var_name(&self) -> String {
1901 self.ado_var.as_ref().into()
1902 }
1903
1904 pub fn dangerous_from_global(ado_var_name: impl AsRef<str>, is_secret: bool) -> Self {
1912 Self {
1913 is_secret,
1914 ado_var: ado_var_name.as_ref().to_owned().into(),
1915 }
1916 }
1917 }
1918
1919 pub fn new_ado_step_services(
1920 fresh_ado_var: &mut dyn FnMut() -> String,
1921 ) -> AdoStepServices<'_> {
1922 AdoStepServices {
1923 fresh_ado_var,
1924 ado_to_rust: Vec::new(),
1925 rust_to_ado: Vec::new(),
1926 }
1927 }
1928
1929 pub struct CompletedAdoStepServices {
1930 pub ado_to_rust: Vec<(String, String, bool)>,
1931 pub rust_to_ado: Vec<(String, String)>,
1932 }
1933
1934 impl CompletedAdoStepServices {
1935 pub fn from_ado_step_services(access: AdoStepServices<'_>) -> Self {
1936 let AdoStepServices {
1937 fresh_ado_var: _,
1938 ado_to_rust,
1939 rust_to_ado,
1940 } = access;
1941
1942 Self {
1943 ado_to_rust,
1944 rust_to_ado,
1945 }
1946 }
1947 }
1948
1949 pub struct AdoStepServices<'a> {
1950 fresh_ado_var: &'a mut dyn FnMut() -> String,
1951 ado_to_rust: Vec<(String, String, bool)>,
1952 rust_to_ado: Vec<(String, String)>,
1953 }
1954
1955 impl AdoStepServices<'_> {
1956 pub fn resolve_repository_id(&self, repo_id: AdoResourcesRepositoryId) -> String {
1959 repo_id.repo_id
1960 }
1961
1962 pub fn set_var(&mut self, var: ClaimedWriteVar<String>, from_ado_var: AdoRuntimeVar) {
1968 self.ado_to_rust.push((
1969 from_ado_var.ado_var.into(),
1970 var.backing_var,
1971 from_ado_var.is_secret,
1972 ))
1973 }
1974
1975 pub fn get_var(&mut self, var: ClaimedReadVar<String>) -> AdoRuntimeVar {
1977 let backing_var = if let ReadVarBacking::RuntimeVar {
1978 var,
1979 is_side_effect,
1980 } = &var.backing_var
1981 {
1982 assert!(!is_side_effect);
1983 var
1984 } else {
1985 todo!("support inline ado read vars")
1986 };
1987
1988 let new_ado_var_name = (self.fresh_ado_var)();
1989
1990 self.rust_to_ado
1991 .push((backing_var.clone(), new_ado_var_name.clone()));
1992 AdoRuntimeVar::dangerous_from_global(new_ado_var_name, false)
1993 }
1994 }
1995 }
1996
1997 pub mod github {
1998 use crate::node::ClaimVar;
1999 use crate::node::NodeCtx;
2000 use crate::node::ReadVar;
2001 use crate::node::ReadVarBacking;
2002 use crate::node::SideEffect;
2003 use crate::node::StepCtx;
2004 use crate::node::VarClaimed;
2005 use crate::node::VarNotClaimed;
2006 use crate::node::WriteVar;
2007 use std::collections::BTreeMap;
2008
2009 pub struct GhStepBuilder {
2010 display_name: String,
2011 cond: Option<ReadVar<bool>>,
2012 uses: String,
2013 with: Option<BTreeMap<String, GhParam>>,
2014 outputs: BTreeMap<String, Vec<WriteVar<String>>>,
2015 run_after: Vec<ReadVar<SideEffect>>,
2016 permissions: BTreeMap<GhPermission, GhPermissionValue>,
2017 }
2018
2019 impl GhStepBuilder {
2020 pub fn new(display_name: impl AsRef<str>, uses: impl AsRef<str>) -> Self {
2035 Self {
2036 display_name: display_name.as_ref().into(),
2037 cond: None,
2038 uses: uses.as_ref().into(),
2039 with: None,
2040 outputs: BTreeMap::new(),
2041 run_after: Vec::new(),
2042 permissions: BTreeMap::new(),
2043 }
2044 }
2045
2046 pub fn condition(mut self, cond: ReadVar<bool>) -> Self {
2053 self.cond = Some(cond);
2054 self
2055 }
2056
2057 pub fn with(mut self, k: impl AsRef<str>, v: impl Into<GhParam>) -> Self {
2083 self.with.get_or_insert_with(BTreeMap::new);
2084 if let Some(with) = &mut self.with {
2085 with.insert(k.as_ref().to_string(), v.into());
2086 }
2087 self
2088 }
2089
2090 pub fn output(mut self, k: impl AsRef<str>, v: WriteVar<String>) -> Self {
2099 self.outputs
2100 .entry(k.as_ref().to_string())
2101 .or_default()
2102 .push(v);
2103 self
2104 }
2105
2106 pub fn run_after(mut self, side_effect: ReadVar<SideEffect>) -> Self {
2108 self.run_after.push(side_effect);
2109 self
2110 }
2111
2112 pub fn requires_permission(
2117 mut self,
2118 perm: GhPermission,
2119 value: GhPermissionValue,
2120 ) -> Self {
2121 self.permissions.insert(perm, value);
2122 self
2123 }
2124
2125 #[track_caller]
2127 pub fn finish(self, ctx: &mut NodeCtx<'_>) -> ReadVar<SideEffect> {
2128 let (side_effect, claim_side_effect) = ctx.new_prefixed_var("auto_se");
2129 ctx.backend
2130 .borrow_mut()
2131 .on_claimed_runtime_var(&claim_side_effect.backing_var, false);
2132
2133 ctx.emit_gh_step_inner(
2134 self.display_name,
2135 self.cond,
2136 self.uses,
2137 self.with,
2138 self.outputs,
2139 self.run_after,
2140 self.permissions,
2141 );
2142
2143 side_effect
2144 }
2145 }
2146
2147 #[derive(Clone, Debug)]
2148 pub enum GhParam<C = VarNotClaimed> {
2149 Static(String),
2150 FloweyVar(ReadVar<String, C>),
2151 }
2152
2153 impl From<String> for GhParam {
2154 fn from(param: String) -> GhParam {
2155 GhParam::Static(param)
2156 }
2157 }
2158
2159 impl From<&str> for GhParam {
2160 fn from(param: &str) -> GhParam {
2161 GhParam::Static(param.to_string())
2162 }
2163 }
2164
2165 impl From<ReadVar<String>> for GhParam {
2166 fn from(param: ReadVar<String>) -> GhParam {
2167 GhParam::FloweyVar(param)
2168 }
2169 }
2170
2171 pub type ClaimedGhParam = GhParam<VarClaimed>;
2172
2173 impl ClaimVar for GhParam {
2174 type Claimed = ClaimedGhParam;
2175
2176 fn claim(self, ctx: &mut StepCtx<'_>) -> ClaimedGhParam {
2177 match self {
2178 GhParam::Static(s) => ClaimedGhParam::Static(s),
2179 GhParam::FloweyVar(var) => match &var.backing_var {
2180 ReadVarBacking::RuntimeVar { is_side_effect, .. } => {
2181 assert!(!is_side_effect);
2182 ClaimedGhParam::FloweyVar(var.claim(ctx))
2183 }
2184 ReadVarBacking::Inline(var) => ClaimedGhParam::Static(var.clone()),
2185 },
2186 }
2187 }
2188 }
2189
2190 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
2195 pub enum GhPermissionValue {
2196 None = 0,
2197 Read = 1,
2198 Write = 2,
2199 }
2200
2201 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2207 pub enum GhPermission {
2208 Actions,
2209 Attestations,
2210 Checks,
2211 Contents,
2212 Deployments,
2213 Discussions,
2214 IdToken,
2215 Issues,
2216 Packages,
2217 Pages,
2218 PullRequests,
2219 RepositoryProjects,
2220 SecurityEvents,
2221 Statuses,
2222 }
2223 }
2224
2225 pub mod rust {
2226 use crate::node::ClaimedWriteVar;
2227 use crate::node::FlowArch;
2228 use crate::node::FlowBackend;
2229 use crate::node::FlowPlatform;
2230 use crate::node::ReadVarValue;
2231 use crate::node::RuntimeVarDb;
2232 use crate::shell::FloweyShell;
2233 use serde::Serialize;
2234 use serde::de::DeserializeOwned;
2235
2236 pub fn new_rust_runtime_services(
2237 runtime_var_db: &mut dyn RuntimeVarDb,
2238 backend: FlowBackend,
2239 platform: FlowPlatform,
2240 arch: FlowArch,
2241 ) -> anyhow::Result<RustRuntimeServices<'_>> {
2242 Ok(RustRuntimeServices {
2243 runtime_var_db,
2244 backend,
2245 platform,
2246 arch,
2247 has_read_secret: false,
2248 sh: FloweyShell::new()?,
2249 })
2250 }
2251
2252 pub struct RustRuntimeServices<'a> {
2253 runtime_var_db: &'a mut dyn RuntimeVarDb,
2254 backend: FlowBackend,
2255 platform: FlowPlatform,
2256 arch: FlowArch,
2257 has_read_secret: bool,
2258 pub sh: FloweyShell,
2264 }
2265
2266 impl RustRuntimeServices<'_> {
2267 pub fn backend(&self) -> FlowBackend {
2270 self.backend
2271 }
2272
2273 pub fn platform(&self) -> FlowPlatform {
2276 self.platform
2277 }
2278
2279 pub fn arch(&self) -> FlowArch {
2281 self.arch
2282 }
2283
2284 pub fn write<T>(&mut self, var: ClaimedWriteVar<T>, val: &T)
2292 where
2293 T: Serialize + DeserializeOwned,
2294 {
2295 self.write_maybe_secret(var, val, self.has_read_secret)
2296 }
2297
2298 pub fn write_secret<T>(&mut self, var: ClaimedWriteVar<T>, val: &T)
2304 where
2305 T: Serialize + DeserializeOwned,
2306 {
2307 self.write_maybe_secret(var, val, true)
2308 }
2309
2310 pub fn write_not_secret<T>(&mut self, var: ClaimedWriteVar<T>, val: &T)
2317 where
2318 T: Serialize + DeserializeOwned,
2319 {
2320 self.write_maybe_secret(var, val, false)
2321 }
2322
2323 fn write_maybe_secret<T>(&mut self, var: ClaimedWriteVar<T>, val: &T, is_secret: bool)
2324 where
2325 T: Serialize + DeserializeOwned,
2326 {
2327 let val = if var.is_side_effect {
2328 b"null".to_vec()
2329 } else {
2330 serde_json::to_vec(val).expect("improve this error path")
2331 };
2332 self.runtime_var_db
2333 .set_var(&var.backing_var, is_secret, val);
2334 }
2335
2336 pub fn write_all<T>(
2337 &mut self,
2338 vars: impl IntoIterator<Item = ClaimedWriteVar<T>>,
2339 val: &T,
2340 ) where
2341 T: Serialize + DeserializeOwned,
2342 {
2343 for var in vars {
2344 self.write(var, val)
2345 }
2346 }
2347
2348 pub fn read<T: ReadVarValue>(&mut self, var: T) -> T::Value {
2349 var.read_value(self)
2350 }
2351
2352 pub(crate) fn get_var(&mut self, var: &str, is_side_effect: bool) -> Vec<u8> {
2353 let (v, is_secret) = self.runtime_var_db.get_var(var);
2354 self.has_read_secret |= is_secret && !is_side_effect;
2355 v
2356 }
2357
2358 pub fn dangerous_gh_set_global_env_var(
2365 &mut self,
2366 var: String,
2367 gh_env_var: String,
2368 ) -> anyhow::Result<()> {
2369 if !matches!(self.backend, FlowBackend::Github) {
2370 return Err(anyhow::anyhow!(
2371 "dangerous_set_gh_env_var can only be used on GitHub Actions"
2372 ));
2373 }
2374
2375 let gh_env_file_path = std::env::var("GITHUB_ENV")?;
2376 let mut gh_env_file = fs_err::OpenOptions::new()
2377 .append(true)
2378 .open(gh_env_file_path)?;
2379 let gh_env_var_assignment = format!(
2380 r#"{}<<EOF
2381{}
2382EOF
2383"#,
2384 gh_env_var, var
2385 );
2386 std::io::Write::write_all(&mut gh_env_file, gh_env_var_assignment.as_bytes())?;
2387
2388 Ok(())
2389 }
2390 }
2391 }
2392}
2393
2394pub trait FlowNodeBase {
2399 type Request: Serialize + DeserializeOwned;
2400
2401 fn imports(&mut self, ctx: &mut ImportCtx<'_>);
2402 fn emit(
2403 &mut self,
2404 config_bytes: Vec<Box<[u8]>>,
2405 requests: Vec<Self::Request>,
2406 ctx: &mut NodeCtx<'_>,
2407 ) -> anyhow::Result<()>;
2408
2409 fn i_know_what_im_doing_with_this_manual_impl(&mut self);
2415}
2416
2417pub mod erased {
2418 use crate::node::FlowNodeBase;
2419 use crate::node::NodeCtx;
2420 use crate::node::user_facing::*;
2421
2422 pub struct ErasedNode<N: FlowNodeBase>(pub N);
2423
2424 impl<N: FlowNodeBase> ErasedNode<N> {
2425 pub fn from_node(node: N) -> Self {
2426 Self(node)
2427 }
2428 }
2429
2430 impl<N> FlowNodeBase for ErasedNode<N>
2431 where
2432 N: FlowNodeBase,
2433 {
2434 type Request = Box<[u8]>;
2436
2437 fn imports(&mut self, ctx: &mut ImportCtx<'_>) {
2438 self.0.imports(ctx)
2439 }
2440
2441 fn emit(
2442 &mut self,
2443 config_bytes: Vec<Box<[u8]>>,
2444 requests: Vec<Box<[u8]>>,
2445 ctx: &mut NodeCtx<'_>,
2446 ) -> anyhow::Result<()> {
2447 let mut converted_requests = Vec::new();
2448 for req in requests {
2449 converted_requests.push(serde_json::from_slice(&req)?)
2450 }
2451
2452 self.0.emit(config_bytes, converted_requests, ctx)
2453 }
2454
2455 fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2456 }
2457}
2458
2459#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2461pub struct NodeHandle(std::any::TypeId);
2462
2463impl Ord for NodeHandle {
2464 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
2465 self.modpath().cmp(other.modpath())
2466 }
2467}
2468
2469impl PartialOrd for NodeHandle {
2470 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
2471 Some(self.cmp(other))
2472 }
2473}
2474
2475impl std::fmt::Debug for NodeHandle {
2476 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2477 std::fmt::Debug::fmt(&self.try_modpath(), f)
2478 }
2479}
2480
2481impl NodeHandle {
2482 pub fn from_type<N: FlowNodeBase + 'static>() -> NodeHandle {
2483 NodeHandle(std::any::TypeId::of::<N>())
2484 }
2485
2486 pub fn from_modpath(modpath: &str) -> NodeHandle {
2487 node_luts::erased_node_by_modpath().get(modpath).unwrap().0
2488 }
2489
2490 pub fn try_from_modpath(modpath: &str) -> Option<NodeHandle> {
2491 node_luts::erased_node_by_modpath()
2492 .get(modpath)
2493 .map(|(s, _)| *s)
2494 }
2495
2496 pub fn new_erased_node(&self) -> Box<dyn FlowNodeBase<Request = Box<[u8]>>> {
2497 let ctor = node_luts::erased_node_by_typeid().get(self).unwrap();
2498 ctor()
2499 }
2500
2501 pub fn modpath(&self) -> &'static str {
2502 node_luts::modpath_by_node_typeid().get(self).unwrap()
2503 }
2504
2505 pub fn try_modpath(&self) -> Option<&'static str> {
2506 node_luts::modpath_by_node_typeid().get(self).cloned()
2507 }
2508
2509 pub fn dummy() -> NodeHandle {
2512 NodeHandle(std::any::TypeId::of::<()>())
2513 }
2514}
2515
2516pub fn list_all_registered_nodes() -> impl Iterator<Item = NodeHandle> {
2517 node_luts::modpath_by_node_typeid().keys().cloned()
2518}
2519
2520mod node_luts {
2535 use super::FlowNodeBase;
2536 use super::NodeHandle;
2537 use std::collections::HashMap;
2538 use std::sync::OnceLock;
2539
2540 pub(super) fn modpath_by_node_typeid() -> &'static HashMap<NodeHandle, &'static str> {
2541 static TYPEID_TO_MODPATH: OnceLock<HashMap<NodeHandle, &'static str>> = OnceLock::new();
2542
2543 TYPEID_TO_MODPATH.get_or_init(|| {
2544 let mut lookup = HashMap::new();
2545 for crate::node::private::FlowNodeMeta {
2546 module_path,
2547 ctor: _,
2548 typeid,
2549 } in crate::node::private::FLOW_NODES
2550 {
2551 let existing = lookup.insert(
2552 NodeHandle(*typeid),
2553 module_path
2554 .strip_suffix("::_only_one_call_to_flowey_node_per_module")
2555 .unwrap(),
2556 );
2557 assert!(existing.is_none())
2560 }
2561
2562 lookup
2563 })
2564 }
2565
2566 pub(super) fn erased_node_by_typeid()
2567 -> &'static HashMap<NodeHandle, fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>> {
2568 static LOOKUP: OnceLock<
2569 HashMap<NodeHandle, fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>>,
2570 > = OnceLock::new();
2571
2572 LOOKUP.get_or_init(|| {
2573 let mut lookup = HashMap::new();
2574 for crate::node::private::FlowNodeMeta {
2575 module_path: _,
2576 ctor,
2577 typeid,
2578 } in crate::node::private::FLOW_NODES
2579 {
2580 let existing = lookup.insert(NodeHandle(*typeid), *ctor);
2581 assert!(existing.is_none())
2584 }
2585
2586 lookup
2587 })
2588 }
2589
2590 pub(super) fn erased_node_by_modpath() -> &'static HashMap<
2591 &'static str,
2592 (
2593 NodeHandle,
2594 fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>,
2595 ),
2596 > {
2597 static MODPATH_LOOKUP: OnceLock<
2598 HashMap<
2599 &'static str,
2600 (
2601 NodeHandle,
2602 fn() -> Box<dyn FlowNodeBase<Request = Box<[u8]>>>,
2603 ),
2604 >,
2605 > = OnceLock::new();
2606
2607 MODPATH_LOOKUP.get_or_init(|| {
2608 let mut lookup = HashMap::new();
2609 for crate::node::private::FlowNodeMeta { module_path, ctor, typeid } in crate::node::private::FLOW_NODES {
2610 let existing = lookup.insert(module_path.strip_suffix("::_only_one_call_to_flowey_node_per_module").unwrap(), (NodeHandle(*typeid), *ctor));
2611 if existing.is_some() {
2612 panic!("conflicting node registrations at {module_path}! please ensure there is a single node per module!")
2613 }
2614 }
2615 lookup
2616 })
2617 }
2618}
2619
2620#[doc(hidden)]
2621pub mod private {
2622 pub use linkme;
2623
2624 pub struct FlowNodeMeta {
2625 pub module_path: &'static str,
2626 pub ctor: fn() -> Box<dyn super::FlowNodeBase<Request = Box<[u8]>>>,
2627 pub typeid: std::any::TypeId,
2628 }
2629
2630 #[linkme::distributed_slice]
2631 pub static FLOW_NODES: [FlowNodeMeta] = [..];
2632
2633 #[expect(unsafe_code)]
2635 #[linkme::distributed_slice(FLOW_NODES)]
2636 static DUMMY_FLOW_NODE: FlowNodeMeta = FlowNodeMeta {
2637 module_path: "<dummy>::_only_one_call_to_flowey_node_per_module",
2638 ctor: || unreachable!(),
2639 typeid: std::any::TypeId::of::<()>(),
2640 };
2641}
2642
2643#[doc(hidden)]
2644#[macro_export]
2645macro_rules! new_flow_node_base {
2646 (struct Node) => {
2647 #[non_exhaustive]
2649 pub struct Node;
2650
2651 mod _only_one_call_to_flowey_node_per_module {
2652 const _: () = {
2653 use $crate::node::private::linkme;
2654
2655 fn new_erased() -> Box<dyn $crate::node::FlowNodeBase<Request = Box<[u8]>>> {
2656 Box::new($crate::node::erased::ErasedNode(super::Node))
2657 }
2658
2659 #[linkme::distributed_slice($crate::node::private::FLOW_NODES)]
2660 #[linkme(crate = linkme)]
2661 static FLOW_NODE: $crate::node::private::FlowNodeMeta =
2662 $crate::node::private::FlowNodeMeta {
2663 module_path: module_path!(),
2664 ctor: new_erased,
2665 typeid: std::any::TypeId::of::<super::Node>(),
2666 };
2667 };
2668 }
2669 };
2670}
2671
2672pub trait FlowNode {
2759 type Request: Serialize + DeserializeOwned;
2763
2764 fn imports(ctx: &mut ImportCtx<'_>);
2776
2777 fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()>;
2780}
2781
2782#[macro_export]
2783macro_rules! new_flow_node {
2784 (struct Node) => {
2785 $crate::new_flow_node_base!(struct Node);
2786
2787 impl $crate::node::FlowNodeBase for Node
2788 where
2789 Node: FlowNode,
2790 {
2791 type Request = <Node as FlowNode>::Request;
2792
2793 fn imports(&mut self, dep: &mut $crate::node::ImportCtx<'_>) {
2794 <Node as FlowNode>::imports(dep)
2795 }
2796
2797 fn emit(
2798 &mut self,
2799 _config_bytes: Vec<Box<[u8]>>,
2800 requests: Vec<Self::Request>,
2801 ctx: &mut $crate::node::NodeCtx<'_>,
2802 ) -> anyhow::Result<()> {
2803 <Node as FlowNode>::emit(requests, ctx)
2804 }
2805
2806 fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2807 }
2808 };
2809}
2810
2811pub trait SimpleFlowNode {
2832 type Request: Serialize + DeserializeOwned;
2833
2834 fn imports(ctx: &mut ImportCtx<'_>);
2846
2847 fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()>;
2849}
2850
2851#[macro_export]
2852macro_rules! new_simple_flow_node {
2853 (struct Node) => {
2854 $crate::new_flow_node_base!(struct Node);
2855
2856 impl $crate::node::FlowNodeBase for Node
2857 where
2858 Node: $crate::node::SimpleFlowNode,
2859 {
2860 type Request = <Node as $crate::node::SimpleFlowNode>::Request;
2861
2862 fn imports(&mut self, dep: &mut $crate::node::ImportCtx<'_>) {
2863 <Node as $crate::node::SimpleFlowNode>::imports(dep)
2864 }
2865
2866 fn emit(
2867 &mut self,
2868 _config_bytes: Vec<Box<[u8]>>,
2869 requests: Vec<Self::Request>,
2870 ctx: &mut $crate::node::NodeCtx<'_>,
2871 ) -> anyhow::Result<()> {
2872 for req in requests {
2873 <Node as $crate::node::SimpleFlowNode>::process_request(req, ctx)?
2874 }
2875
2876 Ok(())
2877 }
2878
2879 fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2880 }
2881 };
2882}
2883
2884pub trait FlowNodeWithConfig {
2931 type Request: Serialize + DeserializeOwned;
2933
2934 type Config: ConfigMerge;
2941
2942 fn imports(ctx: &mut ImportCtx<'_>);
2944
2945 fn emit(
2947 config: Self::Config,
2948 requests: Vec<Self::Request>,
2949 ctx: &mut NodeCtx<'_>,
2950 ) -> anyhow::Result<()>;
2951}
2952
2953#[macro_export]
2954macro_rules! new_flow_node_with_config {
2955 (struct Node) => {
2956 $crate::new_flow_node_base!(struct Node);
2957
2958 impl $crate::node::FlowNodeBase for Node
2959 where
2960 Node: $crate::node::FlowNodeWithConfig,
2961 {
2962 type Request = <Node as $crate::node::FlowNodeWithConfig>::Request;
2963
2964 fn imports(&mut self, dep: &mut $crate::node::ImportCtx<'_>) {
2965 <Node as $crate::node::FlowNodeWithConfig>::imports(dep)
2966 }
2967
2968 fn emit(
2969 &mut self,
2970 config_bytes: Vec<Box<[u8]>>,
2971 requests: Vec<Self::Request>,
2972 ctx: &mut $crate::node::NodeCtx<'_>,
2973 ) -> anyhow::Result<()> {
2974 use $crate::node::ConfigMerge;
2975
2976 type C = <Node as $crate::node::FlowNodeWithConfig>::Config;
2977
2978 let mut merged = <C as Default>::default();
2979 for bytes in config_bytes {
2980 let partial: C = serde_json::from_slice(&bytes)?;
2981 merged.merge(partial)?;
2982 }
2983
2984 <Node as $crate::node::FlowNodeWithConfig>::emit(merged, requests, ctx)
2985 }
2986
2987 fn i_know_what_im_doing_with_this_manual_impl(&mut self) {}
2988 }
2989 };
2990}
2991
2992pub trait IntoRequest {
3000 type Node: FlowNodeBase;
3001 fn into_request(self) -> <Self::Node as FlowNodeBase>::Request;
3002
3003 #[doc(hidden)]
3006 #[expect(nonstandard_style)]
3007 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self);
3008}
3009
3010pub trait IntoConfig: Serialize {
3016 type Node: FlowNodeBase;
3017
3018 #[doc(hidden)]
3021 #[expect(nonstandard_style)]
3022 fn do_not_manually_impl_this_trait__use_the_flowey_config_macro_instead(&mut self);
3023}
3024
3025pub trait ConfigMerge: Serialize + DeserializeOwned + Default {
3028 fn merge(&mut self, other: Self) -> anyhow::Result<()>;
3031}
3032
3033pub trait ConfigField {
3040 fn merge_field(&mut self, field_name: &str, other: Self) -> anyhow::Result<()>;
3041}
3042
3043impl<T: PartialEq> ConfigField for Option<T> {
3044 fn merge_field(&mut self, field_name: &str, other: Self) -> anyhow::Result<()> {
3045 if let Some(new) = other {
3046 match self {
3047 None => *self = Some(new),
3048 Some(old) if *old == new => {}
3049 Some(_) => {
3050 anyhow::bail!("config field `{field_name}` mismatch");
3051 }
3052 }
3053 }
3054 Ok(())
3055 }
3056}
3057
3058impl<K: Ord + std::fmt::Debug, V: PartialEq> ConfigField for BTreeMap<K, V> {
3059 fn merge_field(&mut self, field_name: &str, other: Self) -> anyhow::Result<()> {
3060 for (k, v) in other {
3061 use std::collections::btree_map::Entry;
3062 match self.entry(k) {
3063 Entry::Vacant(e) => {
3064 e.insert(v);
3065 }
3066 Entry::Occupied(e) if *e.get() == v => {}
3067 Entry::Occupied(e) => {
3068 anyhow::bail!("config field `{field_name}` mismatch for key {:?}", e.key(),);
3069 }
3070 }
3071 }
3072 Ok(())
3073 }
3074}
3075
3076#[doc(hidden)]
3077#[macro_export]
3078macro_rules! __flowey_request_inner {
3079 (@emit_struct [$req:ident]
3083 $(#[$a:meta])*
3084 $variant:ident($($tt:tt)*),
3085 $($rest:tt)*
3086 ) => {
3087 $(#[$a])*
3088 #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
3089 pub struct $variant($($tt)*);
3090
3091 impl IntoRequest for $variant {
3092 type Node = Node;
3093 fn into_request(self) -> $req {
3094 $req::$variant(self)
3095 }
3096 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3097 }
3098
3099 $crate::__flowey_request_inner!(@emit_struct [$req] $($rest)*);
3100 };
3101 (@emit_struct [$req:ident]
3102 $(#[$a:meta])*
3103 $variant:ident { $($tt:tt)* },
3104 $($rest:tt)*
3105 ) => {
3106 $(#[$a])*
3107 #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
3108 pub struct $variant {
3109 $($tt)*
3110 }
3111
3112 impl IntoRequest for $variant {
3113 type Node = Node;
3114 fn into_request(self) -> $req {
3115 $req::$variant(self)
3116 }
3117 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3118 }
3119
3120 $crate::__flowey_request_inner!(@emit_struct [$req] $($rest)*);
3121 };
3122 (@emit_struct [$req:ident]
3123 $(#[$a:meta])*
3124 $variant:ident,
3125 $($rest:tt)*
3126 ) => {
3127 $(#[$a])*
3128 #[derive(Serialize, Deserialize)]
3129 pub struct $variant;
3130
3131 impl IntoRequest for $variant {
3132 type Node = Node;
3133 fn into_request(self) -> $req {
3134 $req::$variant(self)
3135 }
3136 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3137 }
3138
3139 $crate::__flowey_request_inner!(@emit_struct [$req] $($rest)*);
3140 };
3141 (@emit_struct [$req:ident]
3142 ) => {};
3143
3144 (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
3148 $(#[$a:meta])*
3149 $variant:ident($($tt:tt)*),
3150 $($rest:tt)*
3151 ) => {
3152 $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*), $($prev[$($prev_a,)*])* $variant[$($a,)*]] $($rest)*);
3153 };
3154 (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
3155 $(#[$a:meta])*
3156 $variant:ident { $($tt:tt)* },
3157 $($rest:tt)*
3158 ) => {
3159 $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*), $($prev[$($prev_a,)*])* $variant[$($a,)*]] $($rest)*);
3160 };
3161 (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
3162 $(#[$a:meta])*
3163 $variant:ident,
3164 $($rest:tt)*
3165 ) => {
3166 $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*), $($prev[$($prev_a,)*])* $variant[$($a,)*]] $($rest)*);
3167 };
3168 (@emit_req_enum [$req:ident($($root_a:meta,)*), $($prev:ident[$($prev_a:meta,)*])*]
3169 ) => {
3170 #[derive(Serialize, Deserialize)]
3171 pub enum $req {$(
3172 $(#[$prev_a])*
3173 $prev(self::req::$prev),
3174 )*}
3175
3176 impl IntoRequest for $req {
3177 type Node = Node;
3178 fn into_request(self) -> $req {
3179 self
3180 }
3181 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3182 }
3183 };
3184}
3185
3186#[macro_export]
3234macro_rules! flowey_request {
3235 (
3236 $(#[$root_a:meta])*
3237 pub enum_struct $req:ident {
3238 $($tt:tt)*
3239 }
3240 ) => {
3241 $crate::__flowey_request_inner!(@emit_req_enum [$req($($root_a,)*),] $($tt)*);
3242 pub mod req {
3243 use super::*;
3244 $crate::__flowey_request_inner!(@emit_struct [$req] $($tt)*);
3245 }
3246 };
3247
3248 (
3249 $(#[$a:meta])*
3250 pub enum $req:ident {
3251 $($tt:tt)*
3252 }
3253 ) => {
3254 $(#[$a])*
3255 #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
3256 pub enum $req {
3257 $($tt)*
3258 }
3259
3260 impl $crate::node::IntoRequest for $req {
3261 type Node = Node;
3262 fn into_request(self) -> $req {
3263 self
3264 }
3265 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3266 }
3267 };
3268
3269 (
3270 $(#[$a:meta])*
3271 pub struct $req:ident {
3272 $($tt:tt)*
3273 }
3274 ) => {
3275 $(#[$a])*
3276 #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
3277 pub struct $req {
3278 $($tt)*
3279 }
3280
3281 impl $crate::node::IntoRequest for $req {
3282 type Node = Node;
3283 fn into_request(self) -> $req {
3284 self
3285 }
3286 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3287 }
3288 };
3289
3290 (
3291 $(#[$a:meta])*
3292 pub struct $req:ident($($tt:tt)*);
3293 ) => {
3294 $(#[$a])*
3295 #[derive($crate::reexports::Serialize, $crate::reexports::Deserialize)]
3296 pub struct $req($($tt)*);
3297
3298 impl $crate::node::IntoRequest for $req {
3299 type Node = Node;
3300 fn into_request(self) -> $req {
3301 self
3302 }
3303 fn do_not_manually_impl_this_trait__use_the_flowey_request_macro_instead(&mut self) {}
3304 }
3305 };
3306}
3307
3308#[macro_export]
3346macro_rules! flowey_config {
3347 (
3348 $(#[$meta:meta])*
3349 pub struct $Config:ident {
3350 $(
3351 $(#[$field_meta:meta])*
3352 pub $field:ident : $ty:ty
3353 ),* $(,)?
3354 }
3355 ) => {
3356 $(#[$meta])*
3357 #[derive(
3358 $crate::reexports::Serialize,
3359 $crate::reexports::Deserialize,
3360 Default,
3361 )]
3362 pub struct $Config {
3363 $(
3364 $(#[$field_meta])*
3365 pub $field: $ty,
3366 )*
3367 }
3368
3369 impl $crate::node::ConfigMerge for $Config {
3370 fn merge(&mut self, other: Self) -> anyhow::Result<()> {
3371 $(
3372 $crate::node::ConfigField::merge_field(
3373 &mut self.$field,
3374 stringify!($field),
3375 other.$field,
3376 )?;
3377 )*
3378 Ok(())
3379 }
3380 }
3381
3382 impl $crate::node::IntoConfig for $Config {
3383 type Node = Node;
3384
3385 fn do_not_manually_impl_this_trait__use_the_flowey_config_macro_instead(&mut self) {}
3386 }
3387 };
3388}
3389
3390#[macro_export]
3407macro_rules! shell_cmd {
3408 ($rt:expr, $cmd:literal) => {{
3409 let flowey_sh = &$rt.sh;
3410 #[expect(clippy::disallowed_macros)]
3411 flowey_sh.wrap($crate::reexports::xshell::cmd!(flowey_sh.xshell(), $cmd))
3412 }};
3413}