Skip to main content

sofie_db/
action.rs

1//! The route or job a commit is made for, which history records beside the actor.
2
3tokio::task_local! {
4    static ACTION: &'static str;
5}
6
7pub async fn acting_for<F: Future>(action: &'static str, work: F) -> F::Output {
8    ACTION.scope(action, work).await
9}
10
11#[must_use]
12pub fn current() -> Option<&'static str> {
13    ACTION.try_with(|action| *action).ok()
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[tokio::test]
21    async fn the_action_is_known_only_inside_its_work() {
22        let inside = acting_for("shop.web.place_order", async { current() }).await;
23
24        assert_eq!(inside, Some("shop.web.place_order"));
25        assert_eq!(current(), None);
26    }
27}