eryon_actors/ctx/
context.rs

1/*
2    Appellation: context <module>
3    Contrib: @FL03
4*/
5use crate::types::MetricPosition;
6use rstmt::HarmonicFunction;
7
8/// The context for node operators; the context provides information about the current state of
9/// the operator and its environment.
10#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
11#[cfg_attr(
12    feature = "serde",
13    derive(serde::Deserialize, serde::Serialize),
14    serde(rename_all = "snake_case")
15)]
16pub struct ActorContext<T = f32> {
17    /// Harmonic function (tonic, dominant, etc.)
18    pub(crate) harmonic_function: Option<HarmonicFunction>,
19    /// Metric position in a phrase
20    pub(crate) metric_position: Option<MetricPosition>,
21    /// Emotional/semantic tagging
22    pub(crate) semantic_tags: Vec<String>,
23    /// Stability rating (0.0-1.0)
24    pub(crate) stability: T,
25}
26
27impl<T> ActorContext<T> {
28    /// Create a new context with default values
29    pub fn new() -> Self
30    where
31        T: num_traits::Zero,
32    {
33        Self {
34            harmonic_function: None,
35            metric_position: None,
36            semantic_tags: Vec::new(),
37            stability: T::zero(),
38        }
39    }
40    /// returns a copy of the harmonic function
41    pub const fn harmonic_function(&self) -> Option<HarmonicFunction> {
42        self.harmonic_function
43    }
44    /// returns a copy of the metric position
45    pub const fn metric_position(&self) -> Option<MetricPosition> {
46        self.metric_position
47    }
48    /// returns an immutable reference to the semantic tags
49    pub const fn semantic_tags(&self) -> &Vec<String> {
50        &self.semantic_tags
51    }
52    /// returns a mutable reference to the semantic tags
53    pub fn semantic_tags_mut(&mut self) -> &mut Vec<String> {
54        &mut self.semantic_tags
55    }
56    /// returns an immutable reference to the stability rating
57    pub const fn stability(&self) -> &T {
58        &self.stability
59    }
60    /// Set the harmonic function
61    pub fn set_harmonic_function(&mut self, function: Option<HarmonicFunction>) {
62        self.harmonic_function = function;
63    }
64    /// Set the metric position
65    pub fn set_metric_position(&mut self, position: Option<MetricPosition>) {
66        self.metric_position = position;
67    }
68    /// Add a semantic tag
69    pub fn push_semantic_tag(&mut self, tag: String) {
70        self.semantic_tags.push(tag);
71    }
72    /// Set the stability rating
73    pub fn set_stability(&mut self, stability: T) {
74        self.stability = stability;
75    }
76    /// consumes the current instance to create another with the given harmonic function
77    pub fn with_harmonic_function(self, function: HarmonicFunction) -> Self {
78        Self {
79            harmonic_function: Some(function),
80            ..self
81        }
82    }
83    /// consumes the current instance to create another with the given metric position
84    pub fn with_metric_position(self, position: MetricPosition) -> Self {
85        Self {
86            metric_position: Some(position),
87            ..self
88        }
89    }
90    /// consumes the current instance to create another with the given semantic tags
91    pub fn with_semantic_tags<I, U>(self, tags: I) -> Self
92    where
93        I: IntoIterator<Item = U>,
94        U: ToString,
95    {
96        Self {
97            semantic_tags: Vec::from_iter(tags.into_iter().map(|t| t.to_string())),
98            ..self
99        }
100    }
101    /// consumes the current instance to create another with the given stability rating
102    pub fn with_stability(self, stability: T) -> Self {
103        Self { stability, ..self }
104    }
105}
106
107impl Default for ActorContext {
108    fn default() -> Self {
109        Self::new()
110    }
111}