fog_pack/
value.rs

1use fog_crypto::identity::BareIdKey;
2
3use crate::value_ref::ValueRef;
4use crate::*;
5use std::borrow::Cow;
6use std::ops::Index;
7use std::{collections::BTreeMap, fmt::Debug};
8
9/// A single, self-contained fog-pack value.
10///
11/// This enum covers all value types that fog-pack specifically encodes.
12#[allow(missing_docs)]
13#[derive(Clone, Debug, Default, PartialEq)]
14pub enum Value {
15    #[default]
16    Null,
17    Bool(bool),
18    Int(Integer),
19    Str(String),
20    F32(f32),
21    F64(f64),
22    Bin(Vec<u8>),
23    Array(Vec<Value>),
24    Map(BTreeMap<String, Value>),
25    Timestamp(Timestamp),
26    Hash(Hash),
27    Identity(Identity),
28    LockId(LockId),
29    StreamId(StreamId),
30    DataLockbox(DataLockbox),
31    IdentityLockbox(IdentityLockbox),
32    StreamLockbox(StreamLockbox),
33    LockLockbox(LockLockbox),
34    BareIdKey(Box<BareIdKey>),
35}
36
37#[allow(missing_docs)]
38impl Value {
39    /// Transform the value into a [`ValueRef`].
40    pub fn as_ref(&self) -> ValueRef {
41        use std::ops::Deref;
42        match *self {
43            Value::Null => ValueRef::Null,
44            Value::Bool(v) => ValueRef::Bool(v),
45            Value::Int(v) => ValueRef::Int(v),
46            Value::Str(ref v) => ValueRef::Str(v.as_ref()),
47            Value::F32(v) => ValueRef::F32(v),
48            Value::F64(v) => ValueRef::F64(v),
49            Value::Bin(ref v) => ValueRef::Bin(v.as_slice()),
50            Value::Array(ref v) => ValueRef::Array(v.iter().map(|i| i.as_ref()).collect()),
51            Value::Map(ref v) => {
52                ValueRef::Map(v.iter().map(|(f, i)| (f.as_ref(), i.as_ref())).collect())
53            }
54            Value::Timestamp(v) => ValueRef::Timestamp(v),
55            Value::Hash(ref v) => ValueRef::Hash(v.clone()),
56            Value::Identity(ref v) => ValueRef::Identity(v.clone()),
57            Value::StreamId(ref v) => ValueRef::StreamId(v.clone()),
58            Value::LockId(ref v) => ValueRef::LockId(v.clone()),
59            Value::DataLockbox(ref v) => ValueRef::DataLockbox(v.deref()),
60            Value::IdentityLockbox(ref v) => ValueRef::IdentityLockbox(v.deref()),
61            Value::StreamLockbox(ref v) => ValueRef::StreamLockbox(v.deref()),
62            Value::LockLockbox(ref v) => ValueRef::LockLockbox(v.deref()),
63            Value::BareIdKey(ref v) => ValueRef::BareIdKey(v.clone()),
64        }
65    }
66
67    pub fn is_null(&self) -> bool {
68        matches!(self, Value::Null)
69    }
70
71    pub fn is_bool(&self) -> bool {
72        matches!(self, Value::Bool(_))
73    }
74
75    pub fn is_int(&self) -> bool {
76        matches!(self, Value::Int(_))
77    }
78
79    pub fn is_i64(&self) -> bool {
80        if let Value::Int(ref v) = *self {
81            v.is_i64()
82        } else {
83            false
84        }
85    }
86
87    pub fn is_u64(&self) -> bool {
88        if let Value::Int(ref v) = *self {
89            v.is_u64()
90        } else {
91            false
92        }
93    }
94
95    pub fn is_f32(&self) -> bool {
96        matches!(self, Value::F32(_))
97    }
98
99    pub fn is_f64(&self) -> bool {
100        matches!(self, Value::F64(_))
101    }
102
103    pub fn is_str(&self) -> bool {
104        matches!(self, Value::Str(_))
105    }
106
107    pub fn is_bin(&self) -> bool {
108        matches!(self, Value::Bin(_))
109    }
110
111    pub fn is_array(&self) -> bool {
112        matches!(self, Value::Array(_))
113    }
114
115    pub fn is_map(&self) -> bool {
116        matches!(self, Value::Map(_))
117    }
118
119    pub fn is_timestamp(&self) -> bool {
120        matches!(self, Value::Timestamp(_))
121    }
122
123    pub fn is_hash(&self) -> bool {
124        matches!(self, Value::Hash(_))
125    }
126
127    pub fn is_identity(&self) -> bool {
128        matches!(self, Value::Identity(_))
129    }
130
131    pub fn is_stream_id(&self) -> bool {
132        matches!(self, Value::StreamId(_))
133    }
134
135    pub fn is_lock_id(&self) -> bool {
136        matches!(self, Value::LockId(_))
137    }
138
139    pub fn is_lockbox(&self) -> bool {
140        matches!(
141            self,
142            Value::DataLockbox(_)
143                | Value::IdentityLockbox(_)
144                | Value::StreamLockbox(_)
145                | Value::LockLockbox(_)
146        )
147    }
148
149    pub fn is_data_lockbox(&self) -> bool {
150        matches!(self, Value::DataLockbox(_))
151    }
152
153    pub fn is_identity_lockbox(&self) -> bool {
154        matches!(self, Value::IdentityLockbox(_))
155    }
156
157    pub fn is_stream_lockbox(&self) -> bool {
158        matches!(self, Value::StreamLockbox(_))
159    }
160
161    pub fn is_lock_lockbox(&self) -> bool {
162        matches!(self, Value::LockLockbox(_))
163    }
164
165    pub fn is_bare_id_key(&self) -> bool {
166        matches!(self, Value::BareIdKey(_))
167    }
168
169    pub fn as_bool(&self) -> Option<bool> {
170        if let Value::Bool(val) = *self {
171            Some(val)
172        } else {
173            None
174        }
175    }
176
177    pub fn as_int(&self) -> Option<Integer> {
178        if let Value::Int(val) = *self {
179            Some(val)
180        } else {
181            None
182        }
183    }
184
185    pub fn as_i64(&self) -> Option<i64> {
186        match *self {
187            Value::Int(ref n) => n.as_i64(),
188            _ => None,
189        }
190    }
191
192    pub fn as_u64(&self) -> Option<u64> {
193        match *self {
194            Value::Int(ref n) => n.as_u64(),
195            _ => None,
196        }
197    }
198
199    pub fn as_f32(&self) -> Option<f32> {
200        match *self {
201            Value::F32(n) => Some(n),
202            _ => None,
203        }
204    }
205
206    pub fn as_f64(&self) -> Option<f64> {
207        match *self {
208            Value::F64(n) => Some(n),
209            _ => None,
210        }
211    }
212
213    pub fn as_floating(&self) -> Option<f64> {
214        match *self {
215            Value::F32(n) => Some(n.into()),
216            Value::F64(n) => Some(n),
217            _ => None,
218        }
219    }
220
221    pub fn as_str(&self) -> Option<&str> {
222        if let Value::Str(ref val) = *self {
223            Some(val.as_str())
224        } else {
225            None
226        }
227    }
228
229    pub fn as_string(&self) -> Option<&String> {
230        if let Value::Str(ref val) = *self {
231            Some(val)
232        } else {
233            None
234        }
235    }
236
237    pub fn as_slice(&self) -> Option<&[u8]> {
238        if let Value::Bin(ref val) = *self {
239            Some(val)
240        } else {
241            None
242        }
243    }
244
245    pub fn as_array(&self) -> Option<&[Value]> {
246        if let Value::Array(ref array) = *self {
247            Some(array)
248        } else {
249            None
250        }
251    }
252
253    pub fn as_array_mut(&mut self) -> Option<&mut [Value]> {
254        match *self {
255            Value::Array(ref mut array) => Some(array),
256            _ => None,
257        }
258    }
259
260    pub fn as_map(&self) -> Option<&BTreeMap<String, Value>> {
261        if let Value::Map(ref map) = *self {
262            Some(map)
263        } else {
264            None
265        }
266    }
267
268    pub fn as_map_mut(&mut self) -> Option<&mut BTreeMap<String, Value>> {
269        match *self {
270            Value::Map(ref mut map) => Some(map),
271            _ => None,
272        }
273    }
274
275    pub fn as_timestamp(&self) -> Option<Timestamp> {
276        if let Value::Timestamp(time) = *self {
277            Some(time)
278        } else {
279            None
280        }
281    }
282
283    pub fn as_hash(&self) -> Option<&Hash> {
284        if let Value::Hash(ref hash) = *self {
285            Some(hash)
286        } else {
287            None
288        }
289    }
290
291    pub fn as_identity(&self) -> Option<&Identity> {
292        if let Value::Identity(ref id) = *self {
293            Some(id)
294        } else {
295            None
296        }
297    }
298
299    pub fn as_stream_id(&self) -> Option<&StreamId> {
300        if let Value::StreamId(ref id) = *self {
301            Some(id)
302        } else {
303            None
304        }
305    }
306
307    pub fn as_lock_id(&self) -> Option<&LockId> {
308        if let Value::LockId(ref id) = *self {
309            Some(id)
310        } else {
311            None
312        }
313    }
314
315    pub fn as_data_lockbox(&self) -> Option<&DataLockboxRef> {
316        if let Value::DataLockbox(ref lockbox) = *self {
317            Some(lockbox)
318        } else {
319            None
320        }
321    }
322
323    pub fn as_identity_lockbox(&self) -> Option<&IdentityLockboxRef> {
324        if let Value::IdentityLockbox(ref lockbox) = *self {
325            Some(lockbox)
326        } else {
327            None
328        }
329    }
330
331    pub fn as_stream_lockbox(&self) -> Option<&StreamLockboxRef> {
332        if let Value::StreamLockbox(ref lockbox) = *self {
333            Some(lockbox)
334        } else {
335            None
336        }
337    }
338
339    pub fn as_lock_lockbox(&self) -> Option<&LockLockboxRef> {
340        if let Value::LockLockbox(ref lockbox) = *self {
341            Some(lockbox)
342        } else {
343            None
344        }
345    }
346
347    pub fn as_bare_id_key(&self) -> Option<&BareIdKey> {
348        if let Value::BareIdKey(ref key) = *self {
349            Some(key)
350        } else {
351            None
352        }
353    }
354}
355
356static NULL: Value = Value::Null;
357
358/// Support indexing into arrays. If the index is out of range or the value isn't an array, this
359/// returns a [`Value::Null`].
360impl Index<usize> for Value {
361    type Output = Value;
362
363    fn index(&self, index: usize) -> &Self::Output {
364        self.as_array().and_then(|v| v.get(index)).unwrap_or(&NULL)
365    }
366}
367
368/// Support indexing into maps. If the index string is not in the map, this returns a
369/// [`Value::Null`].
370impl Index<&str> for Value {
371    type Output = Value;
372
373    fn index(&self, index: &str) -> &Self::Output {
374        self.as_map().and_then(|v| v.get(index)).unwrap_or(&NULL)
375    }
376}
377
378impl<'a> PartialEq<ValueRef<'a>> for Value {
379    fn eq(&self, other: &ValueRef) -> bool {
380        use std::ops::Deref;
381        match self {
382            Value::Null => other == &ValueRef::Null,
383            Value::Bool(s) => {
384                if let ValueRef::Bool(o) = other {
385                    s == o
386                } else {
387                    false
388                }
389            }
390            Value::Int(s) => {
391                if let ValueRef::Int(o) = other {
392                    s == o
393                } else {
394                    false
395                }
396            }
397            Value::Str(s) => {
398                if let ValueRef::Str(o) = other {
399                    s == o
400                } else {
401                    false
402                }
403            }
404            Value::F32(s) => {
405                if let ValueRef::F32(o) = other {
406                    s == o
407                } else {
408                    false
409                }
410            }
411            Value::F64(s) => {
412                if let ValueRef::F64(o) = other {
413                    s == o
414                } else {
415                    false
416                }
417            }
418            Value::Bin(s) => {
419                if let ValueRef::Bin(o) = other {
420                    s == o
421                } else {
422                    false
423                }
424            }
425            Value::Array(s) => {
426                if let ValueRef::Array(o) = other {
427                    s == o
428                } else {
429                    false
430                }
431            }
432            Value::Map(s) => {
433                if let ValueRef::Map(o) = other {
434                    s.len() == o.len()
435                        && s.iter()
436                            .zip(o)
437                            .all(|((ks, vs), (ko, vo))| (ks == ko) && (vs == vo))
438                } else {
439                    false
440                }
441            }
442            Value::Hash(s) => {
443                if let ValueRef::Hash(o) = other {
444                    s == o
445                } else {
446                    false
447                }
448            }
449            Value::Identity(s) => {
450                if let ValueRef::Identity(o) = other {
451                    s == o
452                } else {
453                    false
454                }
455            }
456            Value::StreamId(s) => {
457                if let ValueRef::StreamId(o) = other {
458                    s == o
459                } else {
460                    false
461                }
462            }
463            Value::LockId(s) => {
464                if let ValueRef::LockId(o) = other {
465                    s == o
466                } else {
467                    false
468                }
469            }
470            Value::Timestamp(s) => {
471                if let ValueRef::Timestamp(o) = other {
472                    s == o
473                } else {
474                    false
475                }
476            }
477            Value::DataLockbox(s) => {
478                if let ValueRef::DataLockbox(o) = other {
479                    o == &s.deref()
480                } else {
481                    false
482                }
483            }
484            Value::IdentityLockbox(s) => {
485                if let ValueRef::IdentityLockbox(o) = other {
486                    o == &s.deref()
487                } else {
488                    false
489                }
490            }
491            Value::StreamLockbox(s) => {
492                if let ValueRef::StreamLockbox(o) = other {
493                    o == &s.deref()
494                } else {
495                    false
496                }
497            }
498            Value::LockLockbox(s) => {
499                if let ValueRef::LockLockbox(o) = other {
500                    o == &s.deref()
501                } else {
502                    false
503                }
504            }
505            Value::BareIdKey(s) => {
506                if let ValueRef::BareIdKey(o) = other {
507                    s == o
508                } else {
509                    false
510                }
511            }
512        }
513    }
514}
515
516macro_rules! impl_value_from_integer {
517    ($t: ty) => {
518        impl From<$t> for Value {
519            fn from(v: $t) -> Self {
520                Value::Int(From::from(v))
521            }
522        }
523    };
524}
525
526macro_rules! impl_value_from {
527    ($t: ty, $p: ident) => {
528        impl From<$t> for Value {
529            fn from(v: $t) -> Self {
530                Value::$p(v)
531            }
532        }
533    };
534}
535
536impl_value_from!(bool, Bool);
537impl_value_from!(Integer, Int);
538impl_value_from!(f32, F32);
539impl_value_from!(f64, F64);
540impl_value_from!(String, Str);
541impl_value_from!(Vec<u8>, Bin);
542impl_value_from!(Vec<Value>, Array);
543impl_value_from!(BTreeMap<String, Value>, Map);
544impl_value_from!(Timestamp, Timestamp);
545impl_value_from!(Hash, Hash);
546impl_value_from!(Identity, Identity);
547impl_value_from!(StreamId, StreamId);
548impl_value_from!(LockId, LockId);
549impl_value_from!(DataLockbox, DataLockbox);
550impl_value_from!(IdentityLockbox, IdentityLockbox);
551impl_value_from!(StreamLockbox, StreamLockbox);
552impl_value_from!(LockLockbox, LockLockbox);
553impl_value_from_integer!(u8);
554impl_value_from_integer!(u16);
555impl_value_from_integer!(u32);
556impl_value_from_integer!(u64);
557impl_value_from_integer!(usize);
558impl_value_from_integer!(i8);
559impl_value_from_integer!(i16);
560impl_value_from_integer!(i32);
561impl_value_from_integer!(i64);
562impl_value_from_integer!(isize);
563
564impl From<()> for Value {
565    fn from((): ()) -> Self {
566        Value::Null
567    }
568}
569
570impl From<BareIdKey> for Value {
571    fn from(value: BareIdKey) -> Self {
572        Value::BareIdKey(Box::new(value))
573    }
574}
575
576impl<'a> From<&'a str> for Value {
577    fn from(v: &str) -> Self {
578        Value::Str(v.to_string())
579    }
580}
581
582impl<'a> From<Cow<'a, str>> for Value {
583    fn from(v: Cow<'a, str>) -> Self {
584        Value::Str(v.to_string())
585    }
586}
587
588impl<'a> From<&'a [u8]> for Value {
589    fn from(v: &[u8]) -> Self {
590        Value::Bin(v.into())
591    }
592}
593
594impl<'a> From<Cow<'a, [u8]>> for Value {
595    fn from(v: Cow<'a, [u8]>) -> Self {
596        Value::Bin(v.into_owned())
597    }
598}
599
600impl<V: Into<Value>> std::iter::FromIterator<V> for Value {
601    fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
602        let v: Vec<Value> = iter.into_iter().map(Into::into).collect();
603        Value::Array(v)
604    }
605}
606
607use std::convert::TryFrom;
608
609macro_rules! impl_try_from_value {
610    ($t: ty, $p: ident) => {
611        impl TryFrom<Value> for $t {
612            type Error = Value;
613            fn try_from(v: Value) -> Result<Self, Self::Error> {
614                match v {
615                    Value::$p(v) => Ok(v),
616                    _ => Err(v),
617                }
618            }
619        }
620    };
621}
622
623macro_rules! impl_try_from_value_integer {
624    ($t: ty) => {
625        impl TryFrom<Value> for $t {
626            type Error = Value;
627            fn try_from(v: Value) -> Result<Self, Self::Error> {
628                match v {
629                    Value::Int(i) => TryFrom::try_from(i).map_err(|_| v),
630                    _ => Err(v),
631                }
632            }
633        }
634    };
635}
636
637impl_try_from_value!(bool, Bool);
638impl_try_from_value!(String, Str);
639impl_try_from_value!(f32, F32);
640impl_try_from_value!(f64, F64);
641impl_try_from_value!(Vec<u8>, Bin);
642impl_try_from_value!(Vec<Value>, Array);
643impl_try_from_value!(BTreeMap<String, Value>, Map);
644impl_try_from_value!(Timestamp, Timestamp);
645impl_try_from_value!(Hash, Hash);
646impl_try_from_value!(Identity, Identity);
647impl_try_from_value!(StreamId, StreamId);
648impl_try_from_value!(LockId, LockId);
649impl_try_from_value!(DataLockbox, DataLockbox);
650impl_try_from_value!(IdentityLockbox, IdentityLockbox);
651impl_try_from_value!(StreamLockbox, StreamLockbox);
652impl_try_from_value!(LockLockbox, LockLockbox);
653impl_try_from_value_integer!(u8);
654impl_try_from_value_integer!(u16);
655impl_try_from_value_integer!(u32);
656impl_try_from_value_integer!(u64);
657impl_try_from_value_integer!(usize);
658impl_try_from_value_integer!(i8);
659impl_try_from_value_integer!(i16);
660impl_try_from_value_integer!(i32);
661impl_try_from_value_integer!(i64);
662impl_try_from_value_integer!(isize);
663
664impl TryFrom<Value> for BareIdKey {
665    type Error = Value;
666    fn try_from(v: Value) -> Result<Self, Self::Error> {
667        match v {
668            Value::BareIdKey(v) => Ok(*v),
669            _ => Err(v),
670        }
671    }
672}
673
674impl serde::Serialize for Value {
675    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
676        match self {
677            Value::Null => serializer.serialize_unit(),
678            Value::Bool(v) => serializer.serialize_bool(*v),
679            Value::Int(v) => v.serialize(serializer),
680            Value::Str(v) => serializer.serialize_str(v),
681            Value::F32(v) => serializer.serialize_f32(*v),
682            Value::F64(v) => serializer.serialize_f64(*v),
683            Value::Bin(v) => serializer.serialize_bytes(v),
684            Value::Array(v) => v.serialize(serializer),
685            Value::Map(v) => v.serialize(serializer),
686            Value::Timestamp(v) => v.serialize(serializer),
687            Value::Hash(v) => v.serialize(serializer),
688            Value::Identity(v) => v.serialize(serializer),
689            Value::LockId(v) => v.serialize(serializer),
690            Value::StreamId(v) => v.serialize(serializer),
691            Value::DataLockbox(v) => v.serialize(serializer),
692            Value::IdentityLockbox(v) => v.serialize(serializer),
693            Value::StreamLockbox(v) => v.serialize(serializer),
694            Value::LockLockbox(v) => v.serialize(serializer),
695            Value::BareIdKey(v) => v.serialize(serializer),
696        }
697    }
698}
699
700impl<'de> serde::Deserialize<'de> for Value {
701    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
702        use serde::de::*;
703        use std::fmt;
704
705        struct ValueVisitor;
706        impl<'de> Visitor<'de> for ValueVisitor {
707            type Value = Value;
708
709            fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
710                fmt.write_str("any valid fogpack Value")
711            }
712
713            fn visit_bool<E: Error>(self, v: bool) -> Result<Self::Value, E> {
714                Ok(Value::Bool(v))
715            }
716
717            fn visit_i8<E: Error>(self, v: i8) -> Result<Self::Value, E> {
718                Ok(Value::Int(Integer::from(v)))
719            }
720
721            fn visit_i16<E: Error>(self, v: i16) -> Result<Self::Value, E> {
722                Ok(Value::Int(Integer::from(v)))
723            }
724
725            fn visit_i32<E: Error>(self, v: i32) -> Result<Self::Value, E> {
726                Ok(Value::Int(Integer::from(v)))
727            }
728
729            fn visit_i64<E: Error>(self, v: i64) -> Result<Self::Value, E> {
730                Ok(Value::Int(Integer::from(v)))
731            }
732
733            fn visit_u8<E: Error>(self, v: u8) -> Result<Self::Value, E> {
734                Ok(Value::Int(Integer::from(v)))
735            }
736
737            fn visit_u16<E: Error>(self, v: u16) -> Result<Self::Value, E> {
738                Ok(Value::Int(Integer::from(v)))
739            }
740
741            fn visit_u32<E: Error>(self, v: u32) -> Result<Self::Value, E> {
742                Ok(Value::Int(Integer::from(v)))
743            }
744
745            fn visit_u64<E: Error>(self, v: u64) -> Result<Self::Value, E> {
746                Ok(Value::Int(Integer::from(v)))
747            }
748
749            fn visit_f32<E: Error>(self, v: f32) -> Result<Self::Value, E> {
750                Ok(Value::F32(v))
751            }
752
753            fn visit_f64<E: Error>(self, v: f64) -> Result<Self::Value, E> {
754                Ok(Value::F64(v))
755            }
756
757            fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
758                Ok(Value::Str(v.into()))
759            }
760
761            fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
762                Ok(Value::Str(v))
763            }
764
765            fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
766                Ok(Value::Bin(v.into()))
767            }
768
769            fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
770                Ok(Value::Bin(v))
771            }
772
773            fn visit_unit<E: Error>(self) -> Result<Self::Value, E> {
774                Ok(Value::Null)
775            }
776
777            fn visit_seq<A: SeqAccess<'de>>(self, mut access: A) -> Result<Self::Value, A::Error> {
778                // Allocate with the size hint, but be conservative. 4096 is what serde uses
779                // internally for collections, so we'll do likewise.
780                let mut seq = match access.size_hint() {
781                    Some(size) => Vec::with_capacity(size.min(4096)),
782                    None => Vec::new(),
783                };
784                while let Some(elem) = access.next_element()? {
785                    seq.push(elem);
786                }
787                Ok(Value::Array(seq))
788            }
789
790            fn visit_map<A: MapAccess<'de>>(self, mut access: A) -> Result<Self::Value, A::Error> {
791                let mut map = BTreeMap::new();
792                while let Some((key, val)) = access.next_entry()? {
793                    map.insert(key, val);
794                }
795                Ok(Value::Map(map))
796            }
797
798            /// Should only be called when deserializing our special types.
799            /// Fogpack's deserializer will always turn the variant into a u64
800            fn visit_enum<A: EnumAccess<'de>>(self, access: A) -> Result<Self::Value, A::Error> {
801                let (variant, access) = access.variant()?;
802                use fog_crypto::serde::*;
803                use serde_bytes::{ByteBuf, Bytes};
804                match variant {
805                    FOG_TYPE_ENUM_TIME_INDEX => {
806                        let bytes: ByteBuf = access.newtype_variant()?;
807                        let val = Timestamp::try_from(bytes.as_ref()).map_err(A::Error::custom)?;
808                        Ok(Value::Timestamp(val))
809                    }
810                    FOG_TYPE_ENUM_HASH_INDEX => {
811                        let bytes: ByteBuf = access.newtype_variant()?;
812                        let val = Hash::try_from(bytes.as_ref())
813                            .map_err(|e| A::Error::custom(e.serde_err()))?;
814                        Ok(Value::Hash(val))
815                    }
816                    FOG_TYPE_ENUM_IDENTITY_INDEX => {
817                        let bytes: ByteBuf = access.newtype_variant()?;
818                        let val = Identity::try_from(bytes.as_ref())
819                            .map_err(|e| A::Error::custom(e.serde_err()))?;
820                        Ok(Value::Identity(val))
821                    }
822                    FOG_TYPE_ENUM_LOCK_ID_INDEX => {
823                        let bytes: ByteBuf = access.newtype_variant()?;
824                        let val = LockId::try_from(bytes.as_ref())
825                            .map_err(|e| A::Error::custom(e.serde_err()))?;
826                        Ok(Value::LockId(val))
827                    }
828                    FOG_TYPE_ENUM_STREAM_ID_INDEX => {
829                        let bytes: ByteBuf = access.newtype_variant()?;
830                        let val = StreamId::try_from(bytes.as_ref())
831                            .map_err(|e| A::Error::custom(e.serde_err()))?;
832                        Ok(Value::StreamId(val))
833                    }
834                    FOG_TYPE_ENUM_DATA_LOCKBOX_INDEX => {
835                        let bytes: &Bytes = access.newtype_variant()?;
836                        let val = DataLockboxRef::from_bytes(bytes)
837                            .map_err(|e| A::Error::custom(e.serde_err()))?
838                            .to_owned();
839                        Ok(Value::DataLockbox(val))
840                    }
841                    FOG_TYPE_ENUM_IDENTITY_LOCKBOX_INDEX => {
842                        let bytes: &Bytes = access.newtype_variant()?;
843                        let val = IdentityLockboxRef::from_bytes(bytes)
844                            .map_err(|e| A::Error::custom(e.serde_err()))?
845                            .to_owned();
846                        Ok(Value::IdentityLockbox(val))
847                    }
848                    FOG_TYPE_ENUM_STREAM_LOCKBOX_INDEX => {
849                        let bytes: &Bytes = access.newtype_variant()?;
850                        let val = StreamLockboxRef::from_bytes(bytes)
851                            .map_err(|e| A::Error::custom(e.serde_err()))?
852                            .to_owned();
853                        Ok(Value::StreamLockbox(val))
854                    }
855                    FOG_TYPE_ENUM_LOCK_LOCKBOX_INDEX => {
856                        let bytes: &Bytes = access.newtype_variant()?;
857                        let val = LockLockboxRef::from_bytes(bytes)
858                            .map_err(|e| A::Error::custom(e.serde_err()))?
859                            .to_owned();
860                        Ok(Value::LockLockbox(val))
861                    }
862                    FOG_TYPE_ENUM_BARE_ID_KEY_INDEX => {
863                        let bytes: ByteBuf = access.newtype_variant()?;
864                        let val = BareIdKey::try_from(bytes.as_ref())
865                            .map_err(|e| A::Error::custom(e.serde_err()))?;
866                        Ok(Value::BareIdKey(Box::new(val)))
867                    }
868                    _ => Err(A::Error::custom("unrecognized fogpack extension type")),
869                }
870            }
871        }
872
873        deserializer.deserialize_any(ValueVisitor)
874    }
875}