Crates.io | sod |
lib.rs | sod |
version | 0.3.4 |
source | src |
created_at | 2023-04-04 18:13:33.572892 |
updated_at | 2024-06-26 13:50:12.817275 |
description | Service Oriented Design |
homepage | |
repository | https://github.com/thill/sod |
max_upload_size | |
id | 830371 |
size | 53,345 |
This crate provides Service
, MutService
, and AsyncService
traits and associated utilities to facilitiate service-oriented design.
These traits and tools in this library provide concrete guidelines to help make a service-oriented design successful.
In the context of this crate, a service is simply a trait that accepts an input and produces a result.
Traits can be composed or chained together using the ServiceChain
found in this crate.
This crate in and of itself does not provide mechanisms to expose services on a network or facilitiate service discovery.
Those implementation details are to be provided in sod-*
crates, which will often simply encapsulate other open source libraries to expose them as services.
Instead, this crate provides the core mechanisms to define services and in a way that helps guarantee they will be interoperable with one another at a library level.
use sod::{Service, ServiceChain};
// define a service, which adds a constant number to the input, producing the result as output
struct AddService {
n: usize,
}
impl AddService {
pub fn new(n: usize) -> Self {
Self { n }
}
}
impl Service for AddService {
type Input = usize;
type Output = usize;
type Error = ();
fn process(&self, input: usize) -> Result<usize, ()> {
Ok(input + self.n)
}
}
// chain together multiple add services, where each service's output is processed as the next service's input
let chain = ServiceChain::start(AddService::new(1))
.next(AddService::new(2))
.next(AddService::new(4))
.end();
// pass 100 to the service chain, which should result in `100 + 1 + 2 + 4 = 107`
let result = chain.process(100).unwrap();
assert_eq!(107, result);