Skip to main content

qualia_core_db/net/qdnf/harness/
partition.rs

1//! Partition gate. Delivery is WouldBlock until healed.
2
3use crate::net::qdnf::errors::QdnfError;
4
5/// Independent partition flag for the harness pipe.
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7pub struct PartitionGate {
8    partitioned: bool,
9}
10
11impl PartitionGate {
12    /// Opens the resource.
13    pub const fn open() -> Self {
14        Self { partitioned: false }
15    }
16
17    /// `true` when `partitioned`.
18    pub const fn is_partitioned(self) -> bool {
19        self.partitioned
20    }
21
22    /// `PartitionGate::partition`.
23    pub fn partition(&mut self) {
24        self.partitioned = true;
25    }
26
27    /// Heals the partition.
28    pub fn heal(&mut self) {
29        self.partitioned = false;
30    }
31
32    /// Admits under the partition.
33    pub fn admit(self) -> Result<(), QdnfError> {
34        if self.partitioned {
35            Err(QdnfError::WouldBlock)
36        } else {
37            Ok(())
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn partition_heal() {
48        let mut gate = PartitionGate::open();
49        assert_eq!(gate.admit(), Ok(()));
50        gate.partition();
51        assert_eq!(gate.admit(), Err(QdnfError::WouldBlock));
52        gate.heal();
53        assert_eq!(gate.admit(), Ok(()));
54        assert!(!gate.is_partitioned());
55    }
56}