1use std::{borrow::Cow, collections::BTreeMap};
2
3use crate::{
4 Array, Float, Map, SimpleValue, Value,
5 codec::{Head, Major},
6 view::{Payload, ValueView},
7};
8
9#[derive(Debug)]
51pub struct ValueKey<'a>(Inner<'a>);
52
53#[derive(Debug)]
54enum Inner<'a> {
55 Bytes(&'a [u8]),
56 Text(&'a str),
57 Array(&'a [Value]),
58 Map(&'a BTreeMap<Value, Value>),
59 Other(Cow<'a, Value>),
60}
61
62impl<'a> ValueKey<'a> {
63 pub(crate) fn to_usize(&self) -> Option<usize> {
64 if let Inner::Other(value) = &self.0 {
65 value.to_usize().ok()
66 } else {
67 None
68 }
69 }
70}
71
72impl<'a> From<&'a Value> for ValueKey<'a> {
73 fn from(value: &'a Value) -> Self {
74 Self(Inner::Other(Cow::Borrowed(value)))
75 }
76}
77
78impl<'a> From<&'a [Value]> for ValueKey<'a> {
79 fn from(value: &'a [Value]) -> Self {
80 Self(Inner::Array(value))
81 }
82}
83
84impl<'a> From<&'a Array> for ValueKey<'a> {
85 fn from(value: &'a Array) -> Self {
86 Self(Inner::Array(&value.0))
87 }
88}
89
90impl<'a> From<&'a Map> for ValueKey<'a> {
91 fn from(value: &'a Map) -> Self {
92 Self(Inner::Map(&value.0))
93 }
94}
95
96impl<'a> From<&'a BTreeMap<Value, Value>> for ValueKey<'a> {
97 fn from(value: &'a BTreeMap<Value, Value>) -> Self {
98 Self(Inner::Map(value))
99 }
100}
101
102impl<'a> From<&'a str> for ValueKey<'a> {
103 fn from(value: &'a str) -> Self {
104 Self(Inner::Text(value))
105 }
106}
107
108impl<'a> From<&'a String> for ValueKey<'a> {
109 fn from(value: &'a String) -> Self {
110 Self(Inner::Text(value))
111 }
112}
113
114impl<'a> From<&'a [u8]> for ValueKey<'a> {
115 fn from(value: &'a [u8]) -> Self {
116 Self(Inner::Bytes(value))
117 }
118}
119
120impl<'a, T> From<&'a Vec<T>> for ValueKey<'a>
121where
122 ValueKey<'a>: From<&'a [T]>,
123{
124 fn from(value: &'a Vec<T>) -> Self {
125 value.as_slice().into()
126 }
127}
128
129impl<'a, T, const N: usize> From<&'a [T; N]> for ValueKey<'a>
130where
131 ValueKey<'a>: From<&'a [T]>,
132{
133 fn from(value: &'a [T; N]) -> Self {
134 value.as_slice().into()
135 }
136}
137
138macro_rules! impl_from_copy {
139 ($($type:ty),* $(,)?) => { $(
140 impl<'a> From<$type> for ValueKey<'a> {
141 fn from(value: $type) -> ValueKey<'a> {
142 Self(Inner::Other(Cow::Owned(Value::from(value))))
143 }
144 }
145 )* }
146}
147
148impl_from_copy!(bool, SimpleValue, ());
149
150impl_from_copy!(u8, u16, u32, u64, u128, usize);
151impl_from_copy!(i8, i16, i32, i64, i128, isize);
152
153impl_from_copy!(f32, f64, Float);
154
155impl ValueView for ValueKey<'_> {
156 fn head(&self) -> Head {
157 match &self.0 {
158 Inner::Bytes(bytes) => Head::from_usize(Major::ByteString, bytes.len()),
159 Inner::Text(text) => Head::from_usize(Major::TextString, text.len()),
160 Inner::Array(arr) => Head::from_usize(Major::Array, arr.len()),
161 Inner::Map(map) => Head::from_usize(Major::Map, map.len()),
162 Inner::Other(value) => value.head(),
163 }
164 }
165
166 fn payload(&self) -> Payload<'_> {
167 match &self.0 {
168 Inner::Bytes(bytes) => Payload::Bytes(bytes),
169 Inner::Text(text) => Payload::Text(text),
170 Inner::Array(arr) => Payload::Array(arr),
171 Inner::Map(map) => Payload::Map(map),
172 Inner::Other(value) => value.payload(),
173 }
174 }
175}