#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
pub enum LocatorPrecision {
Block,
Restart,
Entry,
}
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
pub enum LocatorPolicyEntry {
None,
Enabled {
precision: LocatorPrecision,
block_id_bits: Option<u8>,
slot_bits: Option<u8>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocatorPolicy(Vec<LocatorPolicyEntry>);
impl core::ops::Deref for LocatorPolicy {
type Target = [LocatorPolicyEntry];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl LocatorPolicy {
#[must_use]
pub(crate) fn get(&self, level: usize) -> LocatorPolicyEntry {
#[expect(clippy::expect_used, reason = "policy is expected not to be empty")]
self.0
.get(level)
.copied()
.unwrap_or_else(|| self.last().copied().expect("policy should not be empty"))
}
#[must_use]
pub fn disabled() -> Self {
Self::all(LocatorPolicyEntry::None)
}
#[must_use]
pub fn block_level() -> Self {
Self::all(LocatorPolicyEntry::Enabled {
precision: LocatorPrecision::Block,
block_id_bits: None,
slot_bits: None,
})
}
#[must_use]
pub fn all(entry: LocatorPolicyEntry) -> Self {
Self(vec![entry])
}
#[must_use]
pub fn new(policy: impl Into<Vec<LocatorPolicyEntry>>) -> Self {
let policy = policy.into();
assert!(!policy.is_empty(), "locator policy may not be empty");
assert!(policy.len() <= 255, "locator policy is too large");
Self(policy)
}
}
#[cfg(test)]
mod tests;