Skip to main content

ara_com/
field.rs

1use crate::error::AraComError;
2use crate::event::EventStream;
3
4/// Configuration for field access
5#[derive(Debug, Clone)]
6pub struct FieldConfig {
7    pub has_getter: bool,
8    pub has_setter: bool,
9    pub has_notifier: bool,
10}
11
12impl Default for FieldConfig {
13    fn default() -> Self {
14        Self {
15            has_getter: true,
16            has_setter: true,
17            has_notifier: true,
18        }
19    }
20}
21
22/// Trait for a readable field value
23pub trait FieldGetter<T>: Send + Sync {
24    /// Retrieve the current field value from the service.
25    fn get(&self) -> impl std::future::Future<Output = Result<T, AraComError>> + Send;
26}
27
28/// Trait for a writable field value
29pub trait FieldSetter<T>: Send + Sync {
30    /// Set the field value on the service.
31    fn set(&self, value: T) -> impl std::future::Future<Output = Result<(), AraComError>> + Send;
32}
33
34/// Trait for a field that emits change notifications
35pub trait FieldNotifier<T>: Send + Sync {
36    /// Subscribe to notifications for this field's value changes.
37    fn subscribe(&self) -> EventStream<T>;
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_field_config_default() {
46        let cfg = FieldConfig::default();
47        assert!(cfg.has_getter);
48        assert!(cfg.has_setter);
49        assert!(cfg.has_notifier);
50    }
51
52    #[test]
53    fn test_field_config_read_only() {
54        let cfg = FieldConfig {
55            has_getter: true,
56            has_setter: false,
57            has_notifier: false,
58        };
59        assert!(cfg.has_getter);
60        assert!(!cfg.has_setter);
61        assert!(!cfg.has_notifier);
62    }
63}