1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use super::{ResultRow, ResultRowRef};
use crate::ast::Value;
use std::ops;

pub trait ValueIndex<RowType, ReturnValue>: private::Sealed {
    #[doc(hidden)]
    fn index_into(self, row: &RowType) -> &ReturnValue;
}

mod private {
    pub trait Sealed {}
    impl Sealed for usize {}
    impl Sealed for &str {}
}

impl ValueIndex<ResultRowRef<'_>, Value<'static>> for usize {
    fn index_into<'v>(self, row: &'v ResultRowRef) -> &'v Value<'static> {
        row.at(self).unwrap()
    }
}

impl ValueIndex<ResultRowRef<'_>, Value<'static>> for &str {
    fn index_into<'v>(self, row: &'v ResultRowRef) -> &'v Value<'static> {
        row.get(self).unwrap()
    }
}

impl ValueIndex<ResultRow, Value<'static>> for usize {
    fn index_into(self, row: &ResultRow) -> &Value<'static> {
        row.at(self).unwrap()
    }
}

impl ValueIndex<ResultRow, Value<'static>> for &str {
    fn index_into(self, row: &ResultRow) -> &Value<'static> {
        row.get(self).unwrap()
    }
}

impl<'a, I: ValueIndex<ResultRowRef<'a>, Value<'static>> + 'static> ops::Index<I> for ResultRowRef<'a> {
    type Output = Value<'static>;

    fn index(&self, index: I) -> &Value<'static> {
        index.index_into(self)
    }
}

impl<I: ValueIndex<ResultRow, Value<'static>> + 'static> ops::Index<I> for ResultRow {
    type Output = Value<'static>;

    fn index(&self, index: I) -> &Value<'static> {
        index.index_into(self)
    }
}