peace_rt_model_core/params/
workspace_params.rs

1use std::{
2    hash::Hash,
3    marker::PhantomData,
4    ops::{Deref, DerefMut},
5};
6
7use serde::Serialize;
8use type_reg::untagged::{BoxDt, TypeMap};
9
10/// Information that is shared across all profiles and flows in a workspace.
11/// `TypeMap<K>` newtype.
12///
13/// Shared information are the ones that will not change when switching to
14/// different profiles. For example, a user working on a project for a
15/// particular customer may use the following information across profiles:
16///
17/// * User ID
18/// * Customer ID
19///
20/// # Type Parameters
21///
22/// * `K`: Type of key for the `WorkspaceParams` map.
23#[derive(Clone, Debug, Serialize)]
24#[serde(transparent)] // Needed to serialize as a map instead of a list.
25pub struct WorkspaceParams<K>(TypeMap<K, BoxDt>, PhantomData<K>)
26where
27    K: Eq + Hash;
28
29impl<K> WorkspaceParams<K>
30where
31    K: Eq + Hash,
32{
33    /// Returns a new `WorkspaceParams` map.
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Creates an empty `WorkspaceParams` map with the specified capacity.
39    ///
40    /// The `WorkspaceParams` will be able to hold at least capacity elements
41    /// without reallocating. If capacity is 0, the map will not allocate.
42    pub fn with_capacity(capacity: usize) -> Self {
43        Self(TypeMap::with_capacity_typed(capacity), PhantomData)
44    }
45
46    /// Returns the inner map.
47    pub fn into_inner(self) -> TypeMap<K, BoxDt> {
48        self.0
49    }
50}
51
52impl<K> Default for WorkspaceParams<K>
53where
54    K: Eq + Hash,
55{
56    fn default() -> Self {
57        Self(TypeMap::default(), PhantomData)
58    }
59}
60
61impl<K> Deref for WorkspaceParams<K>
62where
63    K: Eq + Hash,
64{
65    type Target = TypeMap<K, BoxDt>;
66
67    fn deref(&self) -> &Self::Target {
68        &self.0
69    }
70}
71
72impl<K> DerefMut for WorkspaceParams<K>
73where
74    K: Eq + Hash,
75{
76    fn deref_mut(&mut self) -> &mut Self::Target {
77        &mut self.0
78    }
79}
80
81impl<K> From<TypeMap<K, BoxDt>> for WorkspaceParams<K>
82where
83    K: Eq + Hash,
84{
85    fn from(type_map: TypeMap<K, BoxDt>) -> Self {
86        Self(type_map, PhantomData)
87    }
88}