microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
flowey/flowey_cli/src/var_db/in_memory.rs
50lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //! A dead simple runtime variable db, backed by a single JSON file. |
| 5 | |
| 6 | use std::collections::BTreeMap; |
| 7 | |
| 8 | /// Implements [`flowey_core::node::RuntimeVarDb`] in memory. |
| 9 | pub struct InMemoryVarDb { |
| 10 | vars: BTreeMap<String, (bool, Vec<u8>)>, |
| 11 | } |
| 12 | |
| 13 | impl InMemoryVarDb { |
| 14 | pub fn new() -> Self { |
| 15 | Self { |
| 16 | vars: BTreeMap::new(), |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl flowey_core::node::RuntimeVarDb for InMemoryVarDb { |
| 22 | fn try_get_var(&mut self, var_name: &str) -> Option<(Vec<u8>, bool)> { |
| 23 | let (is_secret, ref val) = *self.vars.get(var_name)?; |
| 24 | if is_secret { |
| 25 | log::debug!("[db] read var: {} = <secret>", var_name); |
| 26 | } else { |
| 27 | log::debug!( |
| 28 | "[db] read var: {} = {}", |
| 29 | var_name, |
| 30 | String::from_utf8_lossy(val) |
| 31 | ); |
| 32 | } |
| 33 | Some((val.clone(), is_secret)) |
| 34 | } |
| 35 | |
| 36 | fn set_var(&mut self, var_name: &str, is_secret: bool, value: Vec<u8>) { |
| 37 | if is_secret { |
| 38 | log::debug!("[db] set var: {} = <secret>", var_name,); |
| 39 | } else { |
| 40 | log::debug!( |
| 41 | "[db] set var: {} = {}", |
| 42 | var_name, |
| 43 | String::from_utf8_lossy(&value) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | let existing = self.vars.insert(var_name.into(), (is_secret, value)); |
| 48 | assert!(existing.is_none()); // all vars are one-time-write |
| 49 | } |
| 50 | } |
| 51 | |