microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6176f0798485454ba12bbbb23bed1104de308a29

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
6use std::collections::BTreeMap;
7
8/// Implements [`flowey_core::node::RuntimeVarDb`] in memory.
9pub struct InMemoryVarDb {
10 vars: BTreeMap<String, (bool, Vec<u8>)>,
11}
12
13impl InMemoryVarDb {
14 pub fn new() -> Self {
15 Self {
16 vars: BTreeMap::new(),
17 }
18 }
19}
20
21impl 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