mithril_client/cardano_database_client/
immutable_file_range.rsuse std::ops::RangeInclusive;
use anyhow::anyhow;
use mithril_common::{entities::ImmutableFileNumber, StdResult};
#[derive(Debug)]
pub enum ImmutableFileRange {
Full,
From(ImmutableFileNumber),
Range(ImmutableFileNumber, ImmutableFileNumber),
UpTo(ImmutableFileNumber),
}
impl ImmutableFileRange {
pub fn to_range_inclusive(
&self,
last_immutable_file_number: ImmutableFileNumber,
) -> StdResult<RangeInclusive<ImmutableFileNumber>> {
const FIRST_IMMUTABLE_FILE_NUMBER: ImmutableFileNumber = 1;
let full_range = FIRST_IMMUTABLE_FILE_NUMBER..=last_immutable_file_number;
match self {
ImmutableFileRange::Full => Ok(full_range),
ImmutableFileRange::From(from) if full_range.contains(from) => {
Ok(*from..=last_immutable_file_number)
}
ImmutableFileRange::Range(from, to)
if full_range.contains(from)
&& full_range.contains(to)
&& !(*from..=*to).is_empty() =>
{
Ok(*from..=*to)
}
ImmutableFileRange::UpTo(to) if full_range.contains(to) => {
Ok(FIRST_IMMUTABLE_FILE_NUMBER..=*to)
}
_ => Err(anyhow!("Invalid immutable file range: {self:?}")),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_range_inclusive_with_full() {
let immutable_file_range = ImmutableFileRange::Full;
let last_immutable_file_number = 10;
let result = immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.unwrap();
assert_eq!(1..=10, result);
}
#[test]
fn to_range_inclusive_with_from() {
let immutable_file_range = ImmutableFileRange::From(5);
let last_immutable_file_number = 10;
let result = immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.unwrap();
assert_eq!(5..=10, result);
let last_immutable_file_number = 3;
immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.expect_err("should fail: given last immutable should be greater than range start");
}
#[test]
fn to_range_inclusive_with_range() {
let immutable_file_range = ImmutableFileRange::Range(5, 8);
let last_immutable_file_number = 10;
let result = immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.unwrap();
assert_eq!(5..=8, result);
let last_immutable_file_number = 7;
immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.expect_err(
"should fail: given last immutable should be greater or equal range max bound",
);
let immutable_file_range = ImmutableFileRange::Range(10, 8);
immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.expect_err("should fail: range start should be lower than range end");
}
#[test]
fn to_range_inclusive_with_up_to() {
let immutable_file_range = ImmutableFileRange::UpTo(8);
let last_immutable_file_number = 10;
let result = immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.unwrap();
assert_eq!(1..=8, result);
let last_immutable_file_number = 7;
immutable_file_range
.to_range_inclusive(last_immutable_file_number)
.expect_err(
"should fail: given last immutable should be greater or equal range max bound",
);
}
}