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

Refactor services into layers #2212

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/fj-core/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ mod tests {
let (surface_path, boundary) =
SurfacePath::line_from_points([[1., 1.], [2., 1.]]);
let boundary = CurveBoundary::from(boundary);
let surface = core.services.objects.surfaces.xz_plane();
let surface = core.layers.objects.surfaces.xz_plane();

let tolerance = 1.;
let approx =
Expand Down Expand Up @@ -263,7 +263,7 @@ mod tests {
let surface_path =
SurfacePath::circle_from_center_and_radius([0., 0.], 1.);
let boundary = CurveBoundary::from([[0.], [TAU]]);
let surface = core.services.objects.surfaces.xz_plane();
let surface = core.layers.objects.surfaces.xz_plane();

let tolerance = 1.;
let approx =
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-core/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
];

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-core/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ mod tests {
[1., 2.],
];
let [a, b] = [
core.services.objects.surfaces.xy_plane(),
core.services.objects.surfaces.xz_plane(),
core.layers.objects.surfaces.xy_plane(),
core.layers.objects.surfaces.xz_plane(),
]
.map(|surface| {
Face::unbound(surface, &mut core).update_region(
Expand Down Expand Up @@ -116,8 +116,8 @@ mod tests {
[-1., 1.],
];
let surfaces = [
core.services.objects.surfaces.xy_plane(),
core.services.objects.surfaces.xz_plane(),
core.layers.objects.surfaces.xy_plane(),
core.layers.objects.surfaces.xz_plane(),
];
let [a, b] = surfaces.clone().map(|surface| {
Face::unbound(surface, &mut core).update_region(
Expand Down
16 changes: 8 additions & 8 deletions crates/fj-core/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand All @@ -174,7 +174,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -203,7 +203,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -232,7 +232,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -261,7 +261,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -290,7 +290,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -325,7 +325,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -362,7 +362,7 @@ mod tests {
let mut core = Instance::new();

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down
14 changes: 7 additions & 7 deletions crates/fj-core/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.yz_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.yz_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.yz_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.yz_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -231,7 +231,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.yz_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.yz_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -263,7 +263,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.yz_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.yz_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -305,7 +305,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.yz_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.yz_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -348,7 +348,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -382,7 +382,7 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/algorithms/intersect/surface_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ mod tests {
fn plane_plane() {
let mut core = Instance::new();

let xy = core.services.objects.surfaces.xy_plane();
let xz = core.services.objects.surfaces.xz_plane();
let xy = core.layers.objects.surfaces.xy_plane();
let xz = core.layers.objects.surfaces.xz_plane();

// Coincident and parallel planes don't have an intersection curve.
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-core/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {
let d = [0., 1.];

let face =
Face::unbound(core.services.objects.surfaces.xy_plane(), &mut core)
Face::unbound(core.layers.objects.surfaces.xy_plane(), &mut core)
.update_region(
|region, core| {
region.update_exterior(
Expand Down Expand Up @@ -139,7 +139,7 @@ mod tests {
let g = [3., 3.];
let h = [3., 1.];

let surface = core.services.objects.surfaces.xy_plane();
let surface = core.layers.objects.surfaces.xy_plane();

let face = Face::unbound(surface.clone(), &mut core).update_region(
|region, core| {
Expand Down Expand Up @@ -204,7 +204,7 @@ mod tests {
let d = [1., 1.];
let e = [0., 9.];

let surface = core.services.objects.surfaces.xy_plane();
let surface = core.layers.objects.surfaces.xy_plane();

let face = Face::unbound(surface.clone(), &mut core).update_region(
|region, core| {
Expand Down
14 changes: 7 additions & 7 deletions crates/fj-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
//!
//! See [`Instance`].

use crate::{services::Services, validate::ValidationConfig};
use crate::{layers::Layers, validate::ValidationConfig};

/// An instance of the Fornjot core
///
/// This is the main entry point to `fj-core`'s API.
pub struct Instance {
/// Event-sourced background services
pub services: Services,
/// The layers of data that make up the state of a core instance
pub layers: Layers,
}

impl Instance {
/// Construct an instance of `Instance`
pub fn new() -> Self {
let services = Services::new();
Self { services }
let layers = Layers::new();
Self { layers }
}

/// Construct an instance of `Instance`, using the provided configuration
pub fn with_validation_config(config: ValidationConfig) -> Self {
let services = Services::with_validation_config(config);
Self { services }
let layers = Layers::with_validation_config(config);
Self { layers }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
use std::ops::Deref;

/// A service that controls access to some state
/// A generic layer, which controls access to layer state
///
/// `Service` is a generic wrapper around some state, as well as code that knows
/// how to operate on that state. It processes commands, changes the state based
/// on those command, and produces events that capture these changes. These
/// events are stored, providing a log of all changes to the state, and can be
/// replayed later to re-create the state at any point in time.
/// `Layer` is a generic wrapper around some state and controls access to it. It
/// [`Deref`]s to the state it wraps, for easy read access, but prevents any
/// direct write access.
///
/// The wrapped state must implement [`State`], which defines the type of
/// command that this service processes, and the type of event that captures
/// state changes. It also defines methods that operate on the state, commands,
/// and events.
/// Instead, each write access to state is reified as a command, which are
/// processed by [`Layer::process`]. Processing a command can result in any
/// number of events, which can then be used as commands for other layers.
///
/// Implementations of [`State`] might also define an extension trait for a
/// specific `Service<MyState>`, to provide a convenient API to callers.
/// All of this is mediated through [`State`], which the wrapped state must
/// implement.
///
/// This design takes inspiration from, and uses the nomenclature of, this
/// article:
/// <https://thinkbeforecoding.com/post/2021/12/17/functional-event-sourcing-decider>
pub struct Service<S: State> {
pub struct Layer<S: State> {
state: S,
}

impl<S: State> Service<S> {
/// Create an instance of `Service`
impl<S: State> Layer<S> {
/// Create an instance of `Layer`
pub fn new(state: S) -> Self {
Self { state }
}

/// Execute a command
/// Process a command
///
/// The command is executed synchronously. When this method returns, the
/// state has been updated and any events have been logged.
/// The command is processed synchronously. When this method returns, the
/// state has been updated.
pub fn process(&mut self, command: S::Command, events: &mut Vec<S::Event>) {
self.state.decide(command, events);

Expand All @@ -47,15 +44,15 @@ impl<S: State> Service<S> {
}
}

impl<S: State> Deref for Service<S> {
impl<S: State> Deref for Layer<S> {
type Target = S;

fn deref(&self) -> &Self::Target {
&self.state
}
}

impl<S: State> Default for Service<S>
impl<S: State> Default for Layer<S>
where
S: Default,
{
Expand All @@ -64,16 +61,19 @@ where
}
}

/// Implemented for state that can be wrapped by a [`Service`]
/// The state of a specific layer
///
/// See [`Service`] for a detailed explanation.
/// Implementations of this trait are wrapped by the generic [`Layer`], which is
/// the consumer of this trait's API.
///
/// See [`Layer`] for a more detailed explanation.
pub trait State {
/// A command that relates to the state
/// A command that encodes a request to update the state
///
/// Commands are processed by [`State::decide`].
type Command;

/// An event that captures modifications to this state
/// An event that encodes a change to the state
///
/// Events are produced by [`State::decide`] and processed by
/// [`State::evolve`].
Expand All @@ -87,8 +87,8 @@ pub trait State {

/// Evolve the state according to the provided event
///
/// This is the only method gets mutable access to the state, making sure
/// that all changes to the state are captured as events.
/// This is the only method that gets mutable access to the state, making
/// sure that all changes to the state are captured as events.
///
/// Implementations of this method are supposed to be relatively dumb. Any
/// decisions that go into updating the state should be made in
Expand Down
Loading