Skip to main content

sa17/
row_id.rs

1//! The name of a row in its table.
2
3use std::fmt;
4
5use crate::row_address::RowAddress;
6
7/// A row's identity within its table.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[non_exhaustive]
10pub enum RowId {
11    /// A row of the file, by where it is stored.
12    Stored(RowAddress),
13    /// A row that was laid over a file, by the number it was given.
14    Added(u32),
15}
16
17impl RowId {
18    /// Where the row is stored, for a row of the file.
19    #[must_use]
20    pub const fn address(self) -> Option<RowAddress> {
21        match self {
22            Self::Stored(address) => Some(address),
23            Self::Added(_) => None,
24        }
25    }
26}
27
28impl From<RowAddress> for RowId {
29    fn from(address: RowAddress) -> Self {
30        Self::Stored(address)
31    }
32}
33
34impl fmt::Display for RowId {
35    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::Stored(address) => address.fmt(formatter),
38            Self::Added(number) => write!(formatter, "added row {number}"),
39        }
40    }
41}