1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::sync::{RwLock, RwLockWriteGuard};
use stepflow_base::{ObjectStore, IdError, ObjectStoreFiltered};
use stepflow_data::{StateDataFiltered, var::{Var, VarId}};
use stepflow_step::Step;
use stepflow_action::{ActionResult, Action, ActionId};
use super::{Error};
#[derive(Debug)]
pub struct ActionObjectStore {
object_store: RwLock<ObjectStore<Box<dyn Action + Sync + Send>, ActionId>>,
}
impl ActionObjectStore {
pub fn with_capacity(capacity: usize) -> ActionObjectStore {
ActionObjectStore {
object_store: RwLock::new(ObjectStore::with_capacity(capacity)),
}
}
fn action_store_write(&self)
-> Result<RwLockWriteGuard<ObjectStore<Box<dyn Action + Sync + Send>, ActionId>>, Error>
{
let store = self
.object_store
.try_write()
.map_err(|_e| Error::Other)?;
Ok(store)
}
pub fn reserve_id(&self) -> Result<ActionId, Error> {
self.action_store_write()
.map(|mut store| store.reserve_id())
}
pub fn insert_new<CB>(&self, name: Option<String>, cb: CB) -> Result<ActionId, Error>
where CB: FnOnce(ActionId) -> Result<Box<dyn Action + Sync + Send>, IdError<ActionId>>
{
self.action_store_write()
.and_then(|mut store| {
store.insert_new(name, cb).map_err(|e| Error::ActionId(e))
})
}
pub fn register(&self, name: Option<String>, object: Box<dyn Action + Sync + Send>) -> Result<ActionId, Error> {
self.action_store_write()
.and_then(|mut store| {
store.register(name, object).map_err(|e| Error::ActionId(e))
})
}
pub fn id_from_name(&self, name: &str) -> Result<ActionId, Error> {
self.action_store_write()
.and_then(|store| {
store.id_from_name(name)
.map(|id| id.clone())
.ok_or_else(|| Error::ActionId(IdError::NoSuchName(name.to_owned())))
})
}
pub fn name_from_id(&self, action_id: &ActionId) -> Result<String, Error> {
self.action_store_write()
.and_then(|store| {
store.name_from_id(action_id)
.map(|name| name.clone())
.ok_or_else(|| Error::ActionId(IdError::IdMissing(action_id.clone())))
})
}
pub fn start_action(&self, id: &ActionId, step: &Step, step_name: Option<&String>, step_data: &StateDataFiltered, vars: &ObjectStoreFiltered<Box<dyn Var + Send + Sync>, VarId>)
-> Result<ActionResult, Error>
{
self.action_store_write()
.and_then(|mut store| {
let action = store.get_mut(id).ok_or_else(|| Error::ActionId(IdError::IdMissing(id.clone())))?;
action.start(&step, step_name, &step_data, &vars)
.map_err(|e| e.into())
})
}
}