From c04b84f2e42f6d27d3984a2654e0e35204e41247 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Sun, 17 Jul 2022 16:04:23 +0800 Subject: [PATCH] Make Parameters easier to populate --- crates/fj-host/src/lib.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index c8ddc6463c..4644e44d99 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -21,6 +21,7 @@ use std::{ collections::{HashMap, HashSet}, ffi::OsStr, io, + ops::{Deref, DerefMut}, path::PathBuf, process::Command, sync::mpsc, @@ -254,7 +255,8 @@ impl Watcher { } } -/// Parameters that are passed to a model +/// Parameters that are passed to a model. +#[derive(Debug, Clone, PartialEq)] pub struct Parameters(pub HashMap); impl Parameters { @@ -262,6 +264,31 @@ impl Parameters { pub fn empty() -> Self { Self(HashMap::new()) } + + /// Insert a value into the [`Parameters`] dictionary, implicitly converting + /// the arguments to strings and returning `&mut self` to enable chaining. + pub fn insert( + &mut self, + key: impl Into, + value: impl Into, + ) -> &mut Self { + self.0.insert(key.into(), value.into()); + self + } +} + +impl Deref for Parameters { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Parameters { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } } /// An error that can occur when loading or reloading a model