fj_kernel/services/
mod.rs

1//! Service API that promotes monitoring and interactivity
2//!
3//! See [`Service`].
4
5mod objects;
6mod service;
7mod validation;
8
9use crate::objects::{Object, ObjectSet, Objects, WithHandle};
10
11pub use self::{
12    objects::{InsertObject, Operation},
13    service::{Service, State},
14    validation::{Validation, ValidationCommand, ValidationEvent},
15};
16
17/// The kernel services
18pub struct Services {
19    /// The objects service
20    ///
21    /// Allows for inserting objects into a store after they were created.
22    pub objects: Service<Objects>,
23
24    /// The validation service
25    ///
26    /// Validates objects that are inserted using the objects service.
27    pub validation: Service<Validation>,
28}
29
30impl Services {
31    /// Construct an instance of `Services`
32    pub fn new() -> Self {
33        let objects = Service::<Objects>::default();
34        let validation = Service::default();
35
36        Self {
37            objects,
38            validation,
39        }
40    }
41
42    /// Insert an object into the stores
43    pub fn insert_object(&mut self, object: Object<WithHandle>) {
44        let mut object_events = Vec::new();
45        self.objects
46            .execute(Operation::InsertObject { object }, &mut object_events);
47
48        for object_event in object_events {
49            let command = ValidationCommand::ValidateObject {
50                object: object_event.object.into(),
51            };
52            self.validation.execute(command, &mut Vec::new());
53        }
54    }
55
56    /// Validate the provided objects and forget all other validation errors
57    pub fn only_validate(&mut self, objects: impl Into<ObjectSet>) {
58        let objects = objects.into();
59
60        let mut events = Vec::new();
61        self.validation
62            .execute(ValidationCommand::OnlyValidate { objects }, &mut events);
63    }
64}
65
66impl Default for Services {
67    fn default() -> Self {
68        Self::new()
69    }
70}