Skip to main content

fp_runtime/asupersync/
config.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub enum CxCapability {
6    Spawn,
7    Time,
8    Random,
9    Io,
10    Remote,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14pub struct CapabilitySet {
15    pub spawn: bool,
16    pub time: bool,
17    pub random: bool,
18    pub io: bool,
19    pub remote: bool,
20}
21
22impl CapabilitySet {
23    #[must_use]
24    pub const fn none() -> Self {
25        Self {
26            spawn: false,
27            time: false,
28            random: false,
29            io: false,
30            remote: false,
31        }
32    }
33
34    #[must_use]
35    pub const fn all() -> Self {
36        Self {
37            spawn: true,
38            time: true,
39            random: true,
40            io: true,
41            remote: true,
42        }
43    }
44
45    #[must_use]
46    pub const fn for_capability(capability: CxCapability) -> Self {
47        match capability {
48            CxCapability::Spawn => Self {
49                spawn: true,
50                ..Self::none()
51            },
52            CxCapability::Time => Self {
53                time: true,
54                ..Self::none()
55            },
56            CxCapability::Random => Self {
57                random: true,
58                ..Self::none()
59            },
60            CxCapability::Io => Self {
61                io: true,
62                ..Self::none()
63            },
64            CxCapability::Remote => Self {
65                remote: true,
66                ..Self::none()
67            },
68        }
69    }
70
71    #[must_use]
72    pub const fn union(self, other: Self) -> Self {
73        Self {
74            spawn: self.spawn || other.spawn,
75            time: self.time || other.time,
76            random: self.random || other.random,
77            io: self.io || other.io,
78            remote: self.remote || other.remote,
79        }
80    }
81
82    #[must_use]
83    pub const fn satisfies(self, required: Self) -> bool {
84        (!required.spawn || self.spawn)
85            && (!required.time || self.time)
86            && (!required.random || self.random)
87            && (!required.io || self.io)
88            && (!required.remote || self.remote)
89    }
90}
91
92impl Default for CapabilitySet {
93    fn default() -> Self {
94        Self::none()
95    }
96}
97
98pub trait RequiresCapabilities {
99    fn required_capabilities(&self) -> CapabilitySet;
100}
101
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103pub struct AsupersyncConfig {
104    pub max_decode_attempts: u32,
105    pub max_repair_symbols: u32,
106    pub max_transfer_ms: u64,
107    pub capabilities: CapabilitySet,
108}
109
110impl Default for AsupersyncConfig {
111    fn default() -> Self {
112        Self {
113            max_decode_attempts: 3,
114            max_repair_symbols: 32,
115            max_transfer_ms: 5_000,
116            capabilities: CapabilitySet::all(),
117        }
118    }
119}
120
121impl AsupersyncConfig {
122    #[must_use]
123    pub fn with_capabilities(mut self, capabilities: CapabilitySet) -> Self {
124        self.capabilities = capabilities;
125        self
126    }
127}