melodium_engine/building/
contextual_environment.rs1use melodium_common::executive::{Context, TrackId, Value};
2use std::collections::HashMap;
3use std::sync::Arc;
4
5#[derive(Debug, Clone)]
6pub struct ContextualEnvironment {
7 track_id: TrackId,
8 contexts: HashMap<String, Arc<dyn Context>>,
9 variables: HashMap<String, Value>,
11}
12
13impl ContextualEnvironment {
14 pub fn new(track_id: TrackId) -> Self {
15 Self {
16 track_id,
17 variables: HashMap::new(),
18 contexts: HashMap::new(),
19 }
20 }
21
22 pub fn base_on(&self) -> Self {
23 Self {
24 track_id: self.track_id,
25 variables: HashMap::new(),
26 contexts: self.contexts.clone(),
27 }
28 }
29
30 pub fn track_id(&self) -> TrackId {
31 self.track_id
32 }
33
34 pub fn add_variable(&mut self, name: &str, value: Value) {
35 self.variables.insert(name.to_string(), value);
36 }
37
38 pub fn get_variable(&self, name: &str) -> Option<&Value> {
39 self.variables.get(name)
40 }
41
42 pub fn variables(&self) -> &HashMap<String, Value> {
43 &self.variables
44 }
45
46 pub fn add_context(&mut self, name: &str, context: Arc<dyn Context>) {
47 self.contexts.insert(name.to_string(), context);
48 }
49
50 pub fn get_context(&self, name: &str) -> Option<&Arc<dyn Context>> {
51 self.contexts.get(name)
52 }
53
54 pub fn contexts(&self) -> &HashMap<String, Arc<dyn Context>> {
55 &self.contexts
56 }
57}