-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathvalue.rs
1394 lines (1208 loc) · 39.6 KB
/
value.rs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Structured values.
//!
//! This module defines the [`Value`] type and supporting APIs for
//! capturing and serializing them.
use std::fmt;
pub use crate::kv::Error;
/// A type that can be converted into a [`Value`](struct.Value.html).
pub trait ToValue {
/// Perform the conversion.
fn to_value(&self) -> Value;
}
impl<'a, T> ToValue for &'a T
where
T: ToValue + ?Sized,
{
fn to_value(&self) -> Value {
(**self).to_value()
}
}
impl<'v> ToValue for Value<'v> {
fn to_value(&self) -> Value {
Value {
inner: self.inner.clone(),
}
}
}
/// A value in a key-value.
///
/// Values are an anonymous bag containing some structured datum.
///
/// # Capturing values
///
/// There are a few ways to capture a value:
///
/// - Using the `Value::from_*` methods.
/// - Using the `ToValue` trait.
/// - Using the standard `From` trait.
///
/// ## Using the `Value::from_*` methods
///
/// `Value` offers a few constructor methods that capture values of different kinds.
///
/// ```
/// use log::kv::Value;
///
/// let value = Value::from_debug(&42i32);
///
/// assert_eq!(None, value.to_i64());
/// ```
///
/// ## Using the `ToValue` trait
///
/// The `ToValue` trait can be used to capture values generically.
/// It's the bound used by `Source`.
///
/// ```
/// # use log::kv::ToValue;
/// let value = 42i32.to_value();
///
/// assert_eq!(Some(42), value.to_i64());
/// ```
///
/// ## Using the standard `From` trait
///
/// Standard types that implement `ToValue` also implement `From`.
///
/// ```
/// use log::kv::Value;
///
/// let value = Value::from(42i32);
///
/// assert_eq!(Some(42), value.to_i64());
/// ```
///
/// # Data model
///
/// Values can hold one of a number of types:
///
/// - **Null:** The absence of any other meaningful value. Note that
/// `Some(Value::null())` is not the same as `None`. The former is
/// `null` while the latter is `undefined`. This is important to be
/// able to tell the difference between a key-value that was logged,
/// but its value was empty (`Some(Value::null())`) and a key-value
/// that was never logged at all (`None`).
/// - **Strings:** `str`, `char`.
/// - **Booleans:** `bool`.
/// - **Integers:** `u8`-`u128`, `i8`-`i128`, `NonZero*`.
/// - **Floating point numbers:** `f32`-`f64`.
/// - **Errors:** `dyn (Error + 'static)`.
/// - **`serde`:** Any type in `serde`'s data model.
/// - **`sval`:** Any type in `sval`'s data model.
///
/// # Serialization
///
/// Values provide a number of ways to be serialized.
///
/// For basic types the [`Value::visit`] method can be used to extract the
/// underlying typed value. However, this is limited in the amount of types
/// supported (see the [`VisitValue`] trait methods).
///
/// For more complex types one of the following traits can be used:
/// * `sval::Value`, requires the `kv_sval` feature.
/// * `serde::Serialize`, requires the `kv_serde` feature.
///
/// You don't need a visitor to serialize values through `serde` or `sval`.
///
/// A value can always be serialized using any supported framework, regardless
/// of how it was captured. If, for example, a value was captured using its
/// `Display` implementation, it will serialize through `serde` as a string. If it was
/// captured as a struct using `serde`, it will also serialize as a struct
/// through `sval`, or can be formatted using a `Debug`-compatible representation.
pub struct Value<'v> {
inner: inner::Inner<'v>,
}
impl<'v> Value<'v> {
/// Get a value from a type implementing `ToValue`.
pub fn from_any<T>(value: &'v T) -> Self
where
T: ToValue,
{
value.to_value()
}
/// Get a value from a type implementing `std::fmt::Debug`.
pub fn from_debug<T>(value: &'v T) -> Self
where
T: fmt::Debug,
{
Value {
inner: inner::Inner::from_debug(value),
}
}
/// Get a value from a type implementing `std::fmt::Display`.
pub fn from_display<T>(value: &'v T) -> Self
where
T: fmt::Display,
{
Value {
inner: inner::Inner::from_display(value),
}
}
/// Get a value from a type implementing `serde::Serialize`.
#[cfg(feature = "kv_serde")]
pub fn from_serde<T>(value: &'v T) -> Self
where
T: serde::Serialize,
{
Value {
inner: inner::Inner::from_serde1(value),
}
}
/// Get a value from a type implementing `sval::Value`.
#[cfg(feature = "kv_sval")]
pub fn from_sval<T>(value: &'v T) -> Self
where
T: sval::Value,
{
Value {
inner: inner::Inner::from_sval2(value),
}
}
/// Get a value from a dynamic `std::fmt::Debug`.
pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
Value {
inner: inner::Inner::from_dyn_debug(value),
}
}
/// Get a value from a dynamic `std::fmt::Display`.
pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
Value {
inner: inner::Inner::from_dyn_display(value),
}
}
/// Get a value from a dynamic error.
#[cfg(feature = "kv_std")]
pub fn from_dyn_error(err: &'v (dyn std::error::Error + 'static)) -> Self {
Value {
inner: inner::Inner::from_dyn_error(err),
}
}
/// Get a `null` value.
pub fn null() -> Self {
Value {
inner: inner::Inner::empty(),
}
}
/// Get a value from an internal primitive.
fn from_inner<T>(value: T) -> Self
where
T: Into<inner::Inner<'v>>,
{
Value {
inner: value.into(),
}
}
/// Inspect this value using a simple visitor.
///
/// When the `kv_serde` or `kv_sval` features are enabled, you can also
/// serialize a value using its `Serialize` or `Value` implementation.
pub fn visit(&self, visitor: impl VisitValue<'v>) -> Result<(), Error> {
inner::visit(&self.inner, visitor)
}
}
impl<'v> fmt::Debug for Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
impl<'v> fmt::Display for Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
#[cfg(feature = "kv_serde")]
impl<'v> serde::Serialize for Value<'v> {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.inner.serialize(s)
}
}
#[cfg(feature = "kv_sval")]
impl<'v> sval::Value for Value<'v> {
fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> sval::Result {
sval::Value::stream(&self.inner, stream)
}
}
#[cfg(feature = "kv_sval")]
impl<'v> sval_ref::ValueRef<'v> for Value<'v> {
fn stream_ref<S: sval::Stream<'v> + ?Sized>(&self, stream: &mut S) -> sval::Result {
sval_ref::ValueRef::stream_ref(&self.inner, stream)
}
}
impl ToValue for str {
fn to_value(&self) -> Value {
Value::from(self)
}
}
impl<'v> From<&'v str> for Value<'v> {
fn from(value: &'v str) -> Self {
Value::from_inner(value)
}
}
impl ToValue for () {
fn to_value(&self) -> Value {
Value::from_inner(())
}
}
impl<T> ToValue for Option<T>
where
T: ToValue,
{
fn to_value(&self) -> Value {
match *self {
Some(ref value) => value.to_value(),
None => Value::from_inner(()),
}
}
}
macro_rules! impl_to_value_primitive {
($($into_ty:ty,)*) => {
$(
impl ToValue for $into_ty {
fn to_value(&self) -> Value {
Value::from(*self)
}
}
impl<'v> From<$into_ty> for Value<'v> {
fn from(value: $into_ty) -> Self {
Value::from_inner(value)
}
}
impl<'v> From<&'v $into_ty> for Value<'v> {
fn from(value: &'v $into_ty) -> Self {
Value::from_inner(*value)
}
}
)*
};
}
macro_rules! impl_to_value_nonzero_primitive {
($($into_ty:ident,)*) => {
$(
impl ToValue for std::num::$into_ty {
fn to_value(&self) -> Value {
Value::from(self.get())
}
}
impl<'v> From<std::num::$into_ty> for Value<'v> {
fn from(value: std::num::$into_ty) -> Self {
Value::from(value.get())
}
}
impl<'v> From<&'v std::num::$into_ty> for Value<'v> {
fn from(value: &'v std::num::$into_ty) -> Self {
Value::from(value.get())
}
}
)*
};
}
macro_rules! impl_value_to_primitive {
($(#[doc = $doc:tt] $into_name:ident -> $into_ty:ty,)*) => {
impl<'v> Value<'v> {
$(
#[doc = $doc]
pub fn $into_name(&self) -> Option<$into_ty> {
self.inner.$into_name()
}
)*
}
}
}
impl_to_value_primitive![
usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
];
#[rustfmt::skip]
impl_to_value_nonzero_primitive![
NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
];
impl_value_to_primitive![
#[doc = "Try convert this value into a `u64`."]
to_u64 -> u64,
#[doc = "Try convert this value into a `i64`."]
to_i64 -> i64,
#[doc = "Try convert this value into a `u128`."]
to_u128 -> u128,
#[doc = "Try convert this value into a `i128`."]
to_i128 -> i128,
#[doc = "Try convert this value into a `f64`."]
to_f64 -> f64,
#[doc = "Try convert this value into a `char`."]
to_char -> char,
#[doc = "Try convert this value into a `bool`."]
to_bool -> bool,
];
impl<'v> Value<'v> {
/// Try to convert this value into an error.
#[cfg(feature = "kv_std")]
pub fn to_borrowed_error(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.inner.to_borrowed_error()
}
/// Try to convert this value into a borrowed string.
pub fn to_borrowed_str(&self) -> Option<&str> {
self.inner.to_borrowed_str()
}
}
#[cfg(feature = "kv_std")]
mod std_support {
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;
use super::*;
impl<T> ToValue for Box<T>
where
T: ToValue + ?Sized,
{
fn to_value(&self) -> Value {
(**self).to_value()
}
}
impl<T> ToValue for Arc<T>
where
T: ToValue + ?Sized,
{
fn to_value(&self) -> Value {
(**self).to_value()
}
}
impl<T> ToValue for Rc<T>
where
T: ToValue + ?Sized,
{
fn to_value(&self) -> Value {
(**self).to_value()
}
}
impl ToValue for String {
fn to_value(&self) -> Value {
Value::from(&**self)
}
}
impl<'v> ToValue for Cow<'v, str> {
fn to_value(&self) -> Value {
Value::from(&**self)
}
}
impl<'v> Value<'v> {
/// Try to convert this value into a string.
pub fn to_cow_str(&self) -> Option<Cow<'v, str>> {
self.inner.to_str()
}
}
impl<'v> From<&'v String> for Value<'v> {
fn from(v: &'v String) -> Self {
Value::from(&**v)
}
}
}
/// A visitor for a [`Value`].
///
/// Also see [`Value`'s documentation on seralization]. Value visitors are a simple alternative
/// to a more fully-featured serialization framework like `serde` or `sval`. A value visitor
/// can differentiate primitive types through methods like [`VisitValue::visit_bool`] and
/// [`VisitValue::visit_str`], but more complex types like maps and sequences
/// will fallthrough to [`VisitValue::visit_any`].
///
/// If you're trying to serialize a value to a format like JSON, you can use either `serde`
/// or `sval` directly with the value. You don't need a visitor.
///
/// [`Value`'s documentation on seralization]: Value#serialization
pub trait VisitValue<'v> {
/// Visit a `Value`.
///
/// This is the only required method on `VisitValue` and acts as a fallback for any
/// more specific methods that aren't overridden.
/// The `Value` may be formatted using its `fmt::Debug` or `fmt::Display` implementation,
/// or serialized using its `sval::Value` or `serde::Serialize` implementation.
fn visit_any(&mut self, value: Value) -> Result<(), Error>;
/// Visit an empty value.
fn visit_null(&mut self) -> Result<(), Error> {
self.visit_any(Value::null())
}
/// Visit an unsigned integer.
fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
self.visit_any(value.into())
}
/// Visit a signed integer.
fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
self.visit_any(value.into())
}
/// Visit a big unsigned integer.
fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
self.visit_any((value).into())
}
/// Visit a big signed integer.
fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
self.visit_any((value).into())
}
/// Visit a floating point.
fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
self.visit_any(value.into())
}
/// Visit a boolean.
fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
self.visit_any(value.into())
}
/// Visit a string.
fn visit_str(&mut self, value: &str) -> Result<(), Error> {
self.visit_any(value.into())
}
/// Visit a string.
fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
self.visit_str(value)
}
/// Visit a Unicode character.
fn visit_char(&mut self, value: char) -> Result<(), Error> {
let mut b = [0; 4];
self.visit_str(&*value.encode_utf8(&mut b))
}
/// Visit an error.
#[cfg(feature = "kv_std")]
fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
self.visit_any(Value::from_dyn_error(err))
}
/// Visit an error.
#[cfg(feature = "kv_std")]
fn visit_borrowed_error(
&mut self,
err: &'v (dyn std::error::Error + 'static),
) -> Result<(), Error> {
self.visit_any(Value::from_dyn_error(err))
}
}
impl<'a, 'v, T: ?Sized> VisitValue<'v> for &'a mut T
where
T: VisitValue<'v>,
{
fn visit_any(&mut self, value: Value) -> Result<(), Error> {
(**self).visit_any(value)
}
fn visit_null(&mut self) -> Result<(), Error> {
(**self).visit_null()
}
fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
(**self).visit_u64(value)
}
fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
(**self).visit_i64(value)
}
fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
(**self).visit_u128(value)
}
fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
(**self).visit_i128(value)
}
fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
(**self).visit_f64(value)
}
fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
(**self).visit_bool(value)
}
fn visit_str(&mut self, value: &str) -> Result<(), Error> {
(**self).visit_str(value)
}
fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
(**self).visit_borrowed_str(value)
}
fn visit_char(&mut self, value: char) -> Result<(), Error> {
(**self).visit_char(value)
}
#[cfg(feature = "kv_std")]
fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
(**self).visit_error(err)
}
#[cfg(feature = "kv_std")]
fn visit_borrowed_error(
&mut self,
err: &'v (dyn std::error::Error + 'static),
) -> Result<(), Error> {
(**self).visit_borrowed_error(err)
}
}
#[cfg(feature = "value-bag")]
pub(in crate::kv) mod inner {
/**
An implementation of `Value` based on a library called `value_bag`.
`value_bag` was written specifically for use in `log`'s value, but was split out when it outgrew
the codebase here. It's a general-purpose type-erasure library that handles mapping between
more fully-featured serialization frameworks.
*/
use super::*;
pub use value_bag::ValueBag as Inner;
pub use value_bag::Error;
#[cfg(test)]
pub use value_bag::test::TestToken as Token;
pub fn visit<'v>(
inner: &Inner<'v>,
visitor: impl VisitValue<'v>,
) -> Result<(), crate::kv::Error> {
struct InnerVisitValue<V>(V);
impl<'v, V> value_bag::visit::Visit<'v> for InnerVisitValue<V>
where
V: VisitValue<'v>,
{
fn visit_any(&mut self, value: value_bag::ValueBag) -> Result<(), Error> {
self.0
.visit_any(Value { inner: value })
.map_err(crate::kv::Error::into_value)
}
fn visit_empty(&mut self) -> Result<(), Error> {
self.0.visit_null().map_err(crate::kv::Error::into_value)
}
fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
self.0
.visit_u64(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
self.0
.visit_i64(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
self.0
.visit_u128(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
self.0
.visit_i128(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
self.0
.visit_f64(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
self.0
.visit_bool(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_str(&mut self, value: &str) -> Result<(), Error> {
self.0
.visit_str(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
self.0
.visit_borrowed_str(value)
.map_err(crate::kv::Error::into_value)
}
fn visit_char(&mut self, value: char) -> Result<(), Error> {
self.0
.visit_char(value)
.map_err(crate::kv::Error::into_value)
}
#[cfg(feature = "kv_std")]
fn visit_error(
&mut self,
err: &(dyn std::error::Error + 'static),
) -> Result<(), Error> {
self.0
.visit_error(err)
.map_err(crate::kv::Error::into_value)
}
#[cfg(feature = "kv_std")]
fn visit_borrowed_error(
&mut self,
err: &'v (dyn std::error::Error + 'static),
) -> Result<(), Error> {
self.0
.visit_borrowed_error(err)
.map_err(crate::kv::Error::into_value)
}
}
inner
.visit(&mut InnerVisitValue(visitor))
.map_err(crate::kv::Error::from_value)
}
}
#[cfg(not(feature = "value-bag"))]
pub(in crate::kv) mod inner {
/**
This is a dependency-free implementation of `Value` when there's no serialization frameworks involved.
In these simple cases a more fully featured solution like `value_bag` isn't needed, so we avoid pulling it in.
There are a few things here that need to remain consistent with the `value_bag`-based implementation:
1. Conversions should always produce the same results. If a conversion here returns `Some`, then
the same `value_bag`-based conversion must also. Of particular note here are floats to ints; they're
based on the standard library's `TryInto` conversions, which need to be converted to `i32` or `u32`,
and then to `f64`.
2. VisitValues should always be called in the same way. If a particular type of value calls `visit_i64`,
then the same `value_bag`-based visitor must also.
*/
use super::*;
#[derive(Clone)]
pub enum Inner<'v> {
None,
Bool(bool),
Str(&'v str),
Char(char),
I64(i64),
U64(u64),
F64(f64),
I128(i128),
U128(u128),
Debug(&'v dyn fmt::Debug),
Display(&'v dyn fmt::Display),
}
impl<'v> From<()> for Inner<'v> {
fn from(_: ()) -> Self {
Inner::None
}
}
impl<'v> From<bool> for Inner<'v> {
fn from(v: bool) -> Self {
Inner::Bool(v)
}
}
impl<'v> From<char> for Inner<'v> {
fn from(v: char) -> Self {
Inner::Char(v)
}
}
impl<'v> From<f32> for Inner<'v> {
fn from(v: f32) -> Self {
Inner::F64(v as f64)
}
}
impl<'v> From<f64> for Inner<'v> {
fn from(v: f64) -> Self {
Inner::F64(v)
}
}
impl<'v> From<i8> for Inner<'v> {
fn from(v: i8) -> Self {
Inner::I64(v as i64)
}
}
impl<'v> From<i16> for Inner<'v> {
fn from(v: i16) -> Self {
Inner::I64(v as i64)
}
}
impl<'v> From<i32> for Inner<'v> {
fn from(v: i32) -> Self {
Inner::I64(v as i64)
}
}
impl<'v> From<i64> for Inner<'v> {
fn from(v: i64) -> Self {
Inner::I64(v as i64)
}
}
impl<'v> From<isize> for Inner<'v> {
fn from(v: isize) -> Self {
Inner::I64(v as i64)
}
}
impl<'v> From<u8> for Inner<'v> {
fn from(v: u8) -> Self {
Inner::U64(v as u64)
}
}
impl<'v> From<u16> for Inner<'v> {
fn from(v: u16) -> Self {
Inner::U64(v as u64)
}
}
impl<'v> From<u32> for Inner<'v> {
fn from(v: u32) -> Self {
Inner::U64(v as u64)
}
}
impl<'v> From<u64> for Inner<'v> {
fn from(v: u64) -> Self {
Inner::U64(v as u64)
}
}
impl<'v> From<usize> for Inner<'v> {
fn from(v: usize) -> Self {
Inner::U64(v as u64)
}
}
impl<'v> From<i128> for Inner<'v> {
fn from(v: i128) -> Self {
Inner::I128(v)
}
}
impl<'v> From<u128> for Inner<'v> {
fn from(v: u128) -> Self {
Inner::U128(v)
}
}
impl<'v> From<&'v str> for Inner<'v> {
fn from(v: &'v str) -> Self {
Inner::Str(v)
}
}
impl<'v> fmt::Debug for Inner<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Inner::None => fmt::Debug::fmt(&None::<()>, f),
Inner::Bool(v) => fmt::Debug::fmt(v, f),
Inner::Str(v) => fmt::Debug::fmt(v, f),
Inner::Char(v) => fmt::Debug::fmt(v, f),
Inner::I64(v) => fmt::Debug::fmt(v, f),
Inner::U64(v) => fmt::Debug::fmt(v, f),
Inner::F64(v) => fmt::Debug::fmt(v, f),
Inner::I128(v) => fmt::Debug::fmt(v, f),
Inner::U128(v) => fmt::Debug::fmt(v, f),
Inner::Debug(v) => fmt::Debug::fmt(v, f),
Inner::Display(v) => fmt::Display::fmt(v, f),
}
}
}
impl<'v> fmt::Display for Inner<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Inner::None => fmt::Debug::fmt(&None::<()>, f),
Inner::Bool(v) => fmt::Display::fmt(v, f),
Inner::Str(v) => fmt::Display::fmt(v, f),
Inner::Char(v) => fmt::Display::fmt(v, f),
Inner::I64(v) => fmt::Display::fmt(v, f),
Inner::U64(v) => fmt::Display::fmt(v, f),
Inner::F64(v) => fmt::Display::fmt(v, f),
Inner::I128(v) => fmt::Display::fmt(v, f),
Inner::U128(v) => fmt::Display::fmt(v, f),
Inner::Debug(v) => fmt::Debug::fmt(v, f),
Inner::Display(v) => fmt::Display::fmt(v, f),
}
}
}
impl<'v> Inner<'v> {
pub fn from_debug<T: fmt::Debug>(value: &'v T) -> Self {
Inner::Debug(value)
}
pub fn from_display<T: fmt::Display>(value: &'v T) -> Self {
Inner::Display(value)
}
pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
Inner::Debug(value)
}
pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
Inner::Display(value)
}
pub fn empty() -> Self {
Inner::None
}
pub fn to_bool(&self) -> Option<bool> {
match self {
Inner::Bool(v) => Some(*v),
_ => None,
}
}
pub fn to_char(&self) -> Option<char> {
match self {
Inner::Char(v) => Some(*v),
_ => None,
}
}
pub fn to_f64(&self) -> Option<f64> {
match self {
Inner::F64(v) => Some(*v),
Inner::I64(v) => {
let v: i32 = (*v).try_into().ok()?;
v.try_into().ok()
}
Inner::U64(v) => {
let v: u32 = (*v).try_into().ok()?;
v.try_into().ok()
}
Inner::I128(v) => {
let v: i32 = (*v).try_into().ok()?;
v.try_into().ok()
}
Inner::U128(v) => {
let v: u32 = (*v).try_into().ok()?;
v.try_into().ok()
}
_ => None,
}
}
pub fn to_i64(&self) -> Option<i64> {
match self {
Inner::I64(v) => Some(*v),
Inner::U64(v) => (*v).try_into().ok(),
Inner::I128(v) => (*v).try_into().ok(),
Inner::U128(v) => (*v).try_into().ok(),
_ => None,
}
}
pub fn to_u64(&self) -> Option<u64> {
match self {
Inner::U64(v) => Some(*v),
Inner::I64(v) => (*v).try_into().ok(),
Inner::I128(v) => (*v).try_into().ok(),
Inner::U128(v) => (*v).try_into().ok(),
_ => None,
}
}
pub fn to_u128(&self) -> Option<u128> {
match self {
Inner::U128(v) => Some(*v),
Inner::I64(v) => (*v).try_into().ok(),
Inner::U64(v) => (*v).try_into().ok(),
Inner::I128(v) => (*v).try_into().ok(),
_ => None,
}
}
pub fn to_i128(&self) -> Option<i128> {
match self {
Inner::I128(v) => Some(*v),
Inner::I64(v) => (*v).try_into().ok(),
Inner::U64(v) => (*v).try_into().ok(),
Inner::U128(v) => (*v).try_into().ok(),
_ => None,
}
}
pub fn to_borrowed_str(&self) -> Option<&'v str> {
match self {
Inner::Str(v) => Some(v),
_ => None,
}
}
#[cfg(test)]
pub fn to_test_token(&self) -> Token {