Skip to main content

bern_protocol/
message.rs

1use crate::{CommandError, Value};
2use std::sync::Arc;
3use std::sync::atomic::{AtomicBool, Ordering};
4
5/// A validated command call addressed by catalog index.
6#[derive(Clone, Debug, PartialEq)]
7pub struct Invocation {
8    pub command: usize,
9    pub params: Vec<Value>,
10}
11
12/// Lifecycle of a command request. The first version runs commands to
13/// completion before answering; the enum keeps room for long operations
14/// that answer `Accepted` with a handle first.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum CommandStatus {
17    Accepted,
18    Completed,
19}
20
21#[derive(Clone, Debug, PartialEq)]
22pub struct CommandOutcome {
23    pub status: CommandStatus,
24    /// State revision after the command finished, so callers can relate the
25    /// result to subscription updates.
26    pub revision: u64,
27    pub result: Result<Value, CommandError>,
28}
29
30/// Changed state fields within one consistent revision.
31#[derive(Clone, Debug, PartialEq)]
32pub struct StateUpdate {
33    pub revision: u64,
34    pub changes: Vec<(usize, Value)>,
35}
36
37/// Signals a query provider that its answer is no longer wanted: the
38/// subscription moved on to newer parameters or ended. Clones share the
39/// same flag, so the requester and the provider see one request.
40#[derive(Clone, Debug, Default)]
41pub struct Cancellation(Arc<AtomicBool>);
42
43impl Cancellation {
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    pub fn is_cancelled(&self) -> bool {
49        self.0.load(Ordering::Relaxed)
50    }
51
52    pub fn cancel(&self) {
53        self.0.store(true, Ordering::Relaxed);
54    }
55}