Skip to main content

rorm_db/row/
index.rs

1/// The index used by [`Row::get`](crate::Row::get)
2#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
3pub enum RowIndex<'i> {
4    /// Index using the columns' positions
5    Position(usize),
6    /// Index using the columns' names
7    Name(&'i str),
8}
9
10impl RowIndex<'_> {
11    /// Converts the index into its owned version
12    pub fn into_owned(self) -> OwnedRowIndex {
13        match self {
14            RowIndex::Position(index) => index.into(),
15            RowIndex::Name(index) => index.into(),
16        }
17    }
18}
19
20/// Owned version of the [`RowIndex`]
21#[derive(Clone, Debug, Eq, PartialEq, Hash)]
22pub enum OwnedRowIndex {
23    /// Index using the columns' positions
24    Position(usize),
25    /// Index using the columns' names
26    Name(Box<str>),
27}
28
29impl OwnedRowIndex {
30    /// Gets index's borrowed version
31    pub fn as_borrowed(&self) -> RowIndex<'_> {
32        match self {
33            OwnedRowIndex::Position(index) => (*index).into(),
34            OwnedRowIndex::Name(index) => index.as_ref().into(),
35        }
36    }
37}
38
39impl From<usize> for RowIndex<'_> {
40    fn from(value: usize) -> Self {
41        Self::Position(value)
42    }
43}
44
45impl<'i> From<&'i str> for RowIndex<'i> {
46    fn from(value: &'i str) -> Self {
47        Self::Name(value)
48    }
49}
50
51impl From<usize> for OwnedRowIndex {
52    fn from(value: usize) -> Self {
53        Self::Position(value)
54    }
55}
56
57impl From<&str> for OwnedRowIndex {
58    fn from(value: &str) -> Self {
59        value.to_string().into()
60    }
61}
62
63impl From<String> for OwnedRowIndex {
64    fn from(value: String) -> Self {
65        Self::Name(value.into_boxed_str())
66    }
67}