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
//! Structured keys.

use std::fmt;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::borrow::Borrow;

/// A type that can be converted into a key.
pub trait ToKey {
    /// Perform the conversion.
    fn to_key(&self) -> Key;
}

impl<'a, T: ?Sized> ToKey for &'a T
where
    T: ToKey,
{
    fn to_key(&self) -> Key {
        (**self).to_key()
    }
}

impl ToKey for str {
    fn to_key(&self) -> Key {
        Key::from_str(self, None)
    }
}

impl<'k> ToKey for Key<'k> {
    fn to_key(&self) -> Key {
        Key::from_str(self, self.index)
    }
}

/// The key in a key-value pair.
/// 
/// The `Key` type abstracts over owned or borrowed keys.
pub struct Key<'k> {
    inner: Str<'k>,
    index: Option<usize>,
}

impl<'k> Key<'k> {
    /// Create a key from a borrowed string and optional index.
    pub fn from_str(key: &'k (impl Borrow<str> + ?Sized), index: Option<usize>) -> Self {
        Key {
            inner: Str::Borrowed(key.borrow()),
            index,
        }
    }

    pub fn index(&self) -> Option<usize> {
        self.index
    }

    pub fn as_str(&self) -> &str {
        match self.inner {
            Str::Borrowed(k) => k,
            #[cfg(feature = "std")]
            Str::Owned(ref k) => &*k,
        }
    }
}

impl<'k> AsRef<str> for Key<'k> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'k> Borrow<str> for Key<'k> {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl<'k> From<&'k str> for Key<'k> {
    fn from(k: &'k str) -> Self {
        Key::from_str(k, None)
    }
}

impl<'k> PartialEq for Key<'k> {
    fn eq(&self, other: &Self) -> bool {
        self.as_str().eq(other.as_str())
    }
}

impl<'k> Eq for Key<'k> {}

impl<'k> PartialOrd for Key<'k> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.as_str().partial_cmp(other.as_str())
    }
}

impl<'k> Ord for Key<'k> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl<'k> Hash for Key<'k> {
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.as_str().hash(state)
    }
}

impl<'k> fmt::Debug for Key<'k> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl<'k> fmt::Display for Key<'k> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

enum Str<'k> {
    Borrowed(&'k str),
    #[cfg(feature = "std")]
    Owned(String),
}

#[cfg(feature = "std")]
mod std_support {
    use super::*;

    impl<'k> Key<'k> {
        /// Create a key from an owned string and optional index.
        pub fn from_owned(key: impl Into<String>, index: Option<usize>) -> Self {
            Key {
                inner: Str::Owned(key.into()),
                index,
            }
        }
    }

    impl ToKey for String {
        fn to_key(&self) -> Key {
            Key::from_str(self, None)
        }
    }

    impl<'k> From<String> for Key<'k> {
        fn from(k: String) -> Self {
            Key::from_owned(k, None)
        }
    }
}

#[cfg(feature = "kv_sval")]
mod sval_support {
    use super::*;

    use sval::value::{self, Value};

    impl<'k> Value for Key<'k> {
        fn stream(&self, stream: &mut value::Stream) -> Result<(), value::Error> {
            self.as_str().stream(stream)
        }
    }
}

#[cfg(feature = "kv_serde")]
mod serde_support {
    use super::*;

    use serde::{Serialize, Serializer};

    impl<'k> Serialize for Key<'k> {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            self.as_str().serialize(serializer)
        }
    }
}