Skip to content

Commit

Permalink
fix(zenoh-flow-commons): add vars.rs file
Browse files Browse the repository at this point in the history
The purpose for moving them in the commons is to allow for their manipulation
from the command line.

Signed-off-by: Julien Loudet <[email protected]>
  • Loading branch information
J-Loudet committed Oct 26, 2023
1 parent d920a0a commit 633eb3d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions zenoh-flow-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ pub use runtime::RuntimeContext;
mod shared_memory;
pub use shared_memory::{SharedMemoryConfiguration, SharedMemoryParameters};

mod vars;
pub use vars::Vars;

/// Zenoh-Flow's result type.
pub type Result<T> = std::result::Result<T, anyhow::Error>;
67 changes: 67 additions & 0 deletions zenoh-flow-commons/src/vars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Copyright (c) 2021 - 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

use crate::IMergeOverwrite;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::Deref;
use std::rc::Rc;

/// `Vars` is an internal structure that we use to expand the "mustache variables" in a descriptor
/// file.
///
/// Mustache variables take the form: "{{ var }}..." where the number of spaces after the '{{' and
/// before the '}}' do not matter.
///
/// We first parse the descriptor file to only extract the `vars` section and build a `HashMap` out of it.
///
/// We then load the descriptor file as a template and "render" it, substituting every "mustache
/// variable" with its corresponding value in the HashMap.
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Vars {
#[serde(default)]
vars: Rc<HashMap<Rc<str>, Rc<str>>>,
}

impl Deref for Vars {
type Target = HashMap<Rc<str>, Rc<str>>;

fn deref(&self) -> &Self::Target {
&self.vars
}
}

impl IMergeOverwrite for Vars {
fn merge_overwrite(self, other: Self) -> Self {
let mut merged = (*other.vars).clone();
merged.extend((*self.vars).clone().into_iter());

Self {
vars: Rc::new(merged),
}
}
}

impl<const N: usize> From<[(&str, &str); N]> for Vars {
fn from(value: [(&str, &str); N]) -> Self {
Self {
vars: Rc::new(
value
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect::<HashMap<Rc<str>, Rc<str>>>(),
),
}
}
}

0 comments on commit 633eb3d

Please sign in to comment.