Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up services #2208

Merged
merged 15 commits into from
Feb 13, 2024
26 changes: 7 additions & 19 deletions crates/fj-core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use self::{
};

/// The kernel services
#[derive(Default)]
pub struct Services {
/// The objects service
///
Expand All @@ -33,20 +34,15 @@ pub struct Services {
impl Services {
/// Construct an instance of `Services`
pub fn new() -> Self {
let objects = Service::<Objects>::default();
let validation = Service::default();

Self {
objects,
validation,
}
Self::default()
}

/// Construct an instance of `Services`, using the provided configuration
pub fn with_validation_config(config: ValidationConfig) -> Self {
let objects = Service::<Objects>::default();
let objects = Service::default();
let validation =
Service::new(Validation::with_validation_config(config));

Self {
objects,
validation,
Expand All @@ -57,21 +53,19 @@ impl Services {
pub fn insert_object(&mut self, object: AnyObject<AboutToBeStored>) {
let mut object_events = Vec::new();
self.objects
.execute(Operation::InsertObject { object }, &mut object_events);
.process(Operation::InsertObject { object }, &mut object_events);

for object_event in object_events {
let command = ValidationCommand::ValidateObject {
object: object_event.object.into(),
};
self.validation.execute(command, &mut Vec::new());
self.validation.process(command, &mut Vec::new());
}
}

/// Drop `Services`; return any unhandled validation error
pub fn drop_and_validate(self) -> Result<(), ValidationErrors> {
let errors = ValidationErrors(
self.validation.errors.values().cloned().collect(),
);
let errors = self.validation.into_state().into_errors();

if errors.0.is_empty() {
Ok(())
Expand All @@ -80,9 +74,3 @@ impl Services {
}
}
}

impl Default for Services {
fn default() -> Self {
Self::new()
}
}
22 changes: 4 additions & 18 deletions crates/fj-core/src/services/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,17 @@ impl<S: State> Service<S> {
///
/// The command is executed synchronously. When this method returns, the
/// state has been updated and any events have been logged.
pub fn execute(&mut self, command: S::Command, events: &mut Vec<S::Event>) {
pub fn process(&mut self, command: S::Command, events: &mut Vec<S::Event>) {
self.state.decide(command, events);

for event in events {
self.state.evolve(event);
}
}

/// Replay the provided events on the given state
pub fn replay<'event>(
state: &mut S,
events: impl IntoIterator<Item = &'event S::Event>,
) where
<S as State>::Event: 'event,
{
for event in events {
state.evolve(event);
}
/// Drop this instance, returning the wrapped state
pub fn into_state(self) -> S {
self.state
}
}

Expand All @@ -62,13 +55,6 @@ impl<S: State> Deref for Service<S> {
}
}

#[cfg(test)]
impl<S: State> std::ops::DerefMut for Service<S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.state
}
}

impl<S: State> Default for Service<S>
where
S: Default,
Expand Down
19 changes: 9 additions & 10 deletions crates/fj-core/src/services/validation.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
use std::{collections::BTreeMap, error::Error, thread};
use std::{collections::HashMap, error::Error, thread};

use crate::{
objects::{AnyObject, Stored},
storage::ObjectId,
validate::{ValidationConfig, ValidationError},
validate::{ValidationConfig, ValidationError, ValidationErrors},
};

use super::State;

/// Errors that occurred while validating the objects inserted into the stores
#[derive(Default)]
pub struct Validation {
/// All unhandled validation errors
pub errors: BTreeMap<ObjectId, ValidationError>,
errors: HashMap<ObjectId, ValidationError>,

/// Validation configuration for the validation service
config: ValidationConfig,
}

impl Validation {
/// A constructor for the validation service that allows a validation configuration to be set for the service
pub fn with_validation_config(config: ValidationConfig) -> Self {
let errors = BTreeMap::new();
let errors = HashMap::new();
Self { errors, config }
}
}

impl Default for Validation {
fn default() -> Self {
let errors = BTreeMap::new();
let config: ValidationConfig = ValidationConfig::default();
Self { errors, config }
/// Drop this instance, returning the errors it contained
pub fn into_errors(mut self) -> ValidationErrors {
ValidationErrors(self.errors.drain().map(|(_, error)| error).collect())
}
}

Expand Down
8 changes: 0 additions & 8 deletions crates/fj-core/src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@
//! [`Validate::validate_with_config`] and [`ValidationConfig`].
//!
//!
//! ## Implementation Note
//!
//! It is currently not possible to define this configuration for the background
//! validation done by the [`Services`] API. If this is a problem for you,
//! please comment on this issue:
//! <https://github.com/hannobraun/fornjot/issues/2060>
//!
//!
//! [`fj-export`]: https://crates.io/crates/fj-export
//! [issue tracker]: https://github.com/hannobraun/fornjot/issues
//! [`Services`]: crate::services::Services
Expand Down
12 changes: 8 additions & 4 deletions crates/fj-core/src/validate/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ mod tests {
let valid_solid = Solid::new(vec![]).insert(&mut core);
valid_solid.validate_and_return_first_error()?;

core.services.validation.errors.clear();
// Ignore remaining validation errors.
let _ = core.services.drop_and_validate();

Ok(())
}
Expand Down Expand Up @@ -287,7 +288,8 @@ mod tests {
let valid_solid = Solid::new(vec![]).insert(&mut core);
valid_solid.validate_and_return_first_error()?;

core.services.validation.errors.clear();
// Ignore remaining validation errors.
let _ = core.services.drop_and_validate();

Ok(())
}
Expand Down Expand Up @@ -336,7 +338,8 @@ mod tests {
let valid_solid = Solid::new(vec![]).insert(&mut core);
valid_solid.validate_and_return_first_error()?;

core.services.validation.errors.clear();
// Ignore remaining validation errors.
let _ = core.services.drop_and_validate();

Ok(())
}
Expand Down Expand Up @@ -375,7 +378,8 @@ mod tests {
let valid_solid = Solid::new(vec![]).insert(&mut core);
valid_solid.validate_and_return_first_error()?;

core.services.validation.errors.clear();
// Ignore remaining validation errors.
let _ = core.services.drop_and_validate();

Ok(())
}
Expand Down