optative
Simple generic traits for building reconciler systems.
Reconciliation, in this context, means that you manage certain items (such as processes, cloud resources, or UI components), and control the desired state.
You define lifecycle events:
- How to create an item
- How to update an item
- How to delete an item
You also decide what the desired state looks like, and when the reconciliation needs to happen. Optative takes care of calling the lifecycle events to achieve yor desired state.
Extracted from my in-progress project tauler (a data-driven widgeting system) because the pattern turned out to be useful well beyond that project: process pools, connection pools, file watchers, subscriptions, etc.
The walkthrough below builds everything against a tiny REST API that stores one personal greeting per person. The Api client itself is plumbing - its full source lives in crates/optative/tests/common/mod.rs - but for the tutorial, all you need to know is:
api.create(&greeting)doesPOST /greetings/<name>with the message as the bodyapi.update(&greeting)doesPUT /greetings/<name>api.remove(&greeting)doesDELETE /greetings/<name>
Tutorial — declarative state with OptativeSet
Step 1. Define a data type that models our resource
use ;
Step 2. Implement Lifecycle to teach optative how to manage one
We implement the Lifecycle trait by delegating the work to our API client.
use Lifecycle;
Step 3. Initialize a store and the API client
use ;
let mut api = Api ;
let mut store: = new;
Step 4. Declare your initial desired set
The remote state will converge to it automatically.
store.reconcile;
-> POST /greetings/ada, POST /greetings/grace.
Step 5. Change your mind
store.reconcile;
-> PUT /greetings/ada (message differs), DELETE /greetings/grace (no longer in the desired set). optative diffed the two passes and called the right hook for each item.
License
MIT OR Apache-2.0