microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
flowey/flowey_lib_common/src/cfg_cargo_common_flags.rs
83lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //! Centralized configuration for setting "global" cargo command flags, such as |
| 5 | //! `--locked`, `--verbose`, etc... |
| 6 | //! |
| 7 | //! This node can then be depended on by nodes which do fine-grained ops with |
| 8 | //! cargo (e.g: `cargo build`, `cargo doc`, `cargo test`, etc...) to avoid |
| 9 | //! duping the same flag config all over the place. |
| 10 | |
| 11 | use flowey::node::prelude::*; |
| 12 | |
| 13 | #[derive(Serialize, Deserialize)] |
| 14 | pub struct Flags { |
| 15 | pub locked: bool, |
| 16 | pub verbose: bool, |
| 17 | } |
| 18 | |
| 19 | flowey_request! { |
| 20 | pub enum Request { |
| 21 | SetLocked(bool), |
| 22 | SetVerbose(ReadVar<bool>), |
| 23 | GetFlags(WriteVar<Flags>), |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | new_flow_node!(struct Node); |
| 28 | |
| 29 | impl FlowNode for Node { |
| 30 | type Request = Request; |
| 31 | |
| 32 | fn imports(ctx: &mut ImportCtx<'_>) { |
| 33 | ctx.import::<crate::install_rust::Node>(); |
| 34 | } |
| 35 | |
| 36 | fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> { |
| 37 | let mut set_locked = None; |
| 38 | let mut set_verbose = None; |
| 39 | let mut get_flags = Vec::new(); |
| 40 | |
| 41 | for req in requests { |
| 42 | match req { |
| 43 | Request::SetLocked(v) => same_across_all_reqs("SetLocked", &mut set_locked, v)?, |
| 44 | Request::SetVerbose(v) => { |
| 45 | same_across_all_reqs_backing_var("SetVerbose", &mut set_verbose, v)? |
| 46 | } |
| 47 | Request::GetFlags(v) => get_flags.push(v), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | let set_locked = |
| 52 | set_locked.ok_or(anyhow::anyhow!("Missing essential request: SetLocked"))?; |
| 53 | let set_verbose = |
| 54 | set_verbose.ok_or(anyhow::anyhow!("Missing essential request: SetVerbose"))?; |
| 55 | let get_flags = get_flags; |
| 56 | |
| 57 | // -- end of req processing -- // |
| 58 | |
| 59 | if get_flags.is_empty() { |
| 60 | return Ok(()); |
| 61 | } |
| 62 | |
| 63 | ctx.emit_minor_rust_step("report common cargo flags", |ctx| { |
| 64 | let get_flags = get_flags.claim(ctx); |
| 65 | let set_verbose = set_verbose.claim(ctx); |
| 66 | |
| 67 | move |rt| { |
| 68 | let set_verbose = rt.read(set_verbose); |
| 69 | for var in get_flags { |
| 70 | rt.write( |
| 71 | var, |
| 72 | &Flags { |
| 73 | locked: set_locked, |
| 74 | verbose: set_verbose, |
| 75 | }, |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | Ok(()) |
| 82 | } |
| 83 | } |
| 84 | |