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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![cfg(feature = "iterator")]

use cosmwasm_std::Addr;
use std::marker::PhantomData;

use crate::de::KeyDeserialize;
use crate::{Prefixer, PrimaryKey};

/// `RawBound` is used to define the two ends of a range, more explicit than `Option<u8>`.
/// `None` means that we don't limit that side of the range at all.
/// `Inclusive` means we use the given bytes as a limit and *include* anything at that exact key.
/// `Exclusive` means we use the given bytes as a limit and *exclude* anything at that exact key.
/// See `Bound` for a type safe way to build these bounds.
#[derive(Clone, Debug)]
pub enum RawBound {
    Inclusive(Vec<u8>),
    Exclusive(Vec<u8>),
}

/// `Bound` is used to define the two ends of a range.
/// `None` means that we don't limit that side of the range at all.
/// `Inclusive` means we use the given value as a limit and *include* anything at that exact key.
/// `Exclusive` means we use the given value as a limit and *exclude* anything at that exact key.
#[derive(Clone, Debug)]
pub enum Bound<'a, K: PrimaryKey<'a>> {
    Inclusive((K, PhantomData<&'a bool>)),
    Exclusive((K, PhantomData<&'a bool>)),
    InclusiveRaw(Vec<u8>),
    ExclusiveRaw(Vec<u8>),
}

impl<'a, K: PrimaryKey<'a>> Bound<'a, K> {
    pub fn inclusive<T: Into<K>>(k: T) -> Self {
        Self::Inclusive((k.into(), PhantomData))
    }

    pub fn exclusive<T: Into<K>>(k: T) -> Self {
        Self::Exclusive((k.into(), PhantomData))
    }

    pub fn to_raw_bound(&self) -> RawBound {
        match self {
            Bound::Inclusive((k, _)) => RawBound::Inclusive(k.joined_key()),
            Bound::Exclusive((k, _)) => RawBound::Exclusive(k.joined_key()),
            Bound::ExclusiveRaw(raw_k) => RawBound::Exclusive(raw_k.clone()),
            Bound::InclusiveRaw(raw_k) => RawBound::Inclusive(raw_k.clone()),
        }
    }
}

#[derive(Clone, Debug)]
pub enum PrefixBound<'a, K: Prefixer<'a>> {
    Inclusive((K, PhantomData<&'a bool>)),
    Exclusive((K, PhantomData<&'a bool>)),
}

impl<'a, K: Prefixer<'a>> PrefixBound<'a, K> {
    pub fn inclusive<T: Into<K>>(k: T) -> Self {
        Self::Inclusive((k.into(), PhantomData))
    }

    pub fn exclusive<T: Into<K>>(k: T) -> Self {
        Self::Exclusive((k.into(), PhantomData))
    }

    pub fn to_raw_bound(&self) -> RawBound {
        match self {
            PrefixBound::Exclusive((k, _)) => RawBound::Exclusive(k.joined_prefix()),
            PrefixBound::Inclusive((k, _)) => RawBound::Inclusive(k.joined_prefix()),
        }
    }
}

pub trait Bounder<'a>: PrimaryKey<'a> + Sized {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>>;
    fn exclusive_bound(self) -> Option<Bound<'a, Self>>;
}

impl<'a> Bounder<'a> for () {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        None
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        None
    }
}

impl<'a> Bounder<'a> for &'a [u8] {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<
        'a,
        T: PrimaryKey<'a> + KeyDeserialize + Prefixer<'a> + Clone,
        U: PrimaryKey<'a> + KeyDeserialize + Clone,
    > Bounder<'a> for (T, U)
{
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<
        'a,
        T: PrimaryKey<'a> + Prefixer<'a> + Clone,
        U: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize + Clone,
        V: PrimaryKey<'a> + KeyDeserialize + Clone,
    > Bounder<'a> for (T, U, V)
{
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<'a> Bounder<'a> for &'a str {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<'a> Bounder<'a> for String {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<'a> Bounder<'a> for Vec<u8> {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<'a> Bounder<'a> for &'a Addr {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

impl<'a> Bounder<'a> for Addr {
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }
    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

macro_rules! integer_bound {
    (for $($t:ty),+) => {
        $(impl<'a> Bounder<'a> for $t {
            fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
                Some(Bound::inclusive(self))
            }
            fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
                Some(Bound::exclusive(self))
            }
        })*
    }
}

integer_bound!(for i8, u8, i16, u16, i32, u32, i64, u64);