[go: up one dir, main page]

sval/
value.rs

1use crate::{Index, Label, Result, Stream, Tag};
2
3/**
4A producer of structured data.
5
6Each call to [`Value::stream`] is expected to produce a single complete value.
7*/
8pub trait Value {
9    /**
10    Stream this value through a [`Stream`].
11    */
12    fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result;
13
14    /**
15    Get the tag of this value, if there is one.
16    */
17    #[inline]
18    fn tag(&self) -> Option<Tag> {
19        default_value::tag(self)
20    }
21
22    /**
23    Try convert this value into a boolean.
24    */
25    #[inline]
26    fn to_bool(&self) -> Option<bool> {
27        default_value::to_bool(self)
28    }
29
30    /**
31    Try convert this value into a 32bit binary floating point number.
32    */
33    #[inline]
34    fn to_f32(&self) -> Option<f32> {
35        default_value::to_f32(self)
36    }
37
38    /**
39    Try convert this value into a 64bit binary floating point number.
40    */
41    #[inline]
42    fn to_f64(&self) -> Option<f64> {
43        default_value::to_f64(self)
44    }
45
46    /**
47    Try convert this value into a signed 8bit integer.
48    */
49    #[inline]
50    fn to_i8(&self) -> Option<i8> {
51        default_value::to_i8(self)
52    }
53
54    /**
55    Try convert this value into a signed 16bit integer.
56    */
57    #[inline]
58    fn to_i16(&self) -> Option<i16> {
59        default_value::to_i16(self)
60    }
61
62    /**
63    Try convert this value into a signed 32bit integer.
64    */
65    #[inline]
66    fn to_i32(&self) -> Option<i32> {
67        default_value::to_i32(self)
68    }
69
70    /**
71    Try convert this value into a signed 64bit integer.
72    */
73    #[inline]
74    fn to_i64(&self) -> Option<i64> {
75        default_value::to_i64(self)
76    }
77
78    /**
79    Try convert this value into a signed 128bit integer.
80    */
81    #[inline]
82    fn to_i128(&self) -> Option<i128> {
83        default_value::to_i128(self)
84    }
85
86    /**
87    Try convert this value into an unsigned 8bit integer.
88    */
89    #[inline]
90    fn to_u8(&self) -> Option<u8> {
91        default_value::to_u8(self)
92    }
93
94    /**
95    Try convert this value into an unsigned 16bit integer.
96    */
97    #[inline]
98    fn to_u16(&self) -> Option<u16> {
99        default_value::to_u16(self)
100    }
101
102    /**
103    Try convert this value into an unsigned 32bit integer.
104    */
105    #[inline]
106    fn to_u32(&self) -> Option<u32> {
107        default_value::to_u32(self)
108    }
109
110    /**
111    Try convert this value into an unsigned 64bit integer.
112    */
113    #[inline]
114    fn to_u64(&self) -> Option<u64> {
115        default_value::to_u64(self)
116    }
117
118    /**
119    Try convert this value into an unsigned 128bit integer.
120    */
121    #[inline]
122    fn to_u128(&self) -> Option<u128> {
123        default_value::to_u128(self)
124    }
125
126    /**
127    Try convert this value into a text string.
128    */
129    #[inline]
130    fn to_text(&self) -> Option<&str> {
131        default_value::to_text(self)
132    }
133
134    /**
135    Try convert this value into a bitstring.
136    */
137    #[inline]
138    fn to_binary(&self) -> Option<&[u8]> {
139        default_value::to_binary(self)
140    }
141}
142
143macro_rules! impl_value_forward {
144    ({ $($r:tt)* } => $bind:ident => { $($forward:tt)* }) => {
145        $($r)* {
146            fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result {
147                let $bind = self;
148                ($($forward)*).stream(stream)
149            }
150
151            #[inline]
152            fn tag(&self) -> Option<Tag> {
153                let $bind = self;
154                ($($forward)*).tag()
155            }
156
157            #[inline]
158            fn to_bool(&self) -> Option<bool> {
159                let $bind = self;
160                ($($forward)*).to_bool()
161            }
162
163            #[inline]
164            fn to_f32(&self) -> Option<f32> {
165                let $bind = self;
166                ($($forward)*).to_f32()
167            }
168
169            #[inline]
170            fn to_f64(&self) -> Option<f64> {
171                let $bind = self;
172                ($($forward)*).to_f64()
173            }
174
175            #[inline]
176            fn to_i8(&self) -> Option<i8> {
177                let $bind = self;
178                ($($forward)*).to_i8()
179            }
180
181            #[inline]
182            fn to_i16(&self) -> Option<i16> {
183                let $bind = self;
184                ($($forward)*).to_i16()
185            }
186
187            #[inline]
188            fn to_i32(&self) -> Option<i32> {
189                let $bind = self;
190                ($($forward)*).to_i32()
191            }
192
193            #[inline]
194            fn to_i64(&self) -> Option<i64> {
195                let $bind = self;
196                ($($forward)*).to_i64()
197            }
198
199            #[inline]
200            fn to_i128(&self) -> Option<i128> {
201                let $bind = self;
202                ($($forward)*).to_i128()
203            }
204
205            #[inline]
206            fn to_u8(&self) -> Option<u8> {
207                let $bind = self;
208                ($($forward)*).to_u8()
209            }
210
211            #[inline]
212            fn to_u16(&self) -> Option<u16> {
213                let $bind = self;
214                ($($forward)*).to_u16()
215            }
216
217            #[inline]
218            fn to_u32(&self) -> Option<u32> {
219                let $bind = self;
220                ($($forward)*).to_u32()
221            }
222
223            #[inline]
224            fn to_u64(&self) -> Option<u64> {
225                let $bind = self;
226                ($($forward)*).to_u64()
227            }
228
229            #[inline]
230            fn to_u128(&self) -> Option<u128> {
231                let $bind = self;
232                ($($forward)*).to_u128()
233            }
234
235            #[inline]
236            fn to_text(&self) -> Option<&str> {
237                let $bind = self;
238                ($($forward)*).to_text()
239            }
240
241            #[inline]
242            fn to_binary(&self) -> Option<&[u8]> {
243                let $bind = self;
244                ($($forward)*).to_binary()
245            }
246        }
247    };
248}
249
250impl_value_forward!({impl<'a, T: Value + ?Sized> Value for &'a T} => x => { **x });
251
252#[cfg(feature = "alloc")]
253mod alloc_support {
254    use super::*;
255
256    use crate::std::boxed::Box;
257
258    impl_value_forward!({impl<T: Value + ?Sized> Value for Box<T>} => x => { **x });
259}
260
261pub mod default_value {
262    /*!
263    Default method implementations for [`Value`]s.
264    */
265
266    use super::*;
267
268    /**
269    Get the tag of this value, if there is one.
270    */
271    pub fn tag(value: &(impl Value + ?Sized)) -> Option<Tag> {
272        struct Extract {
273            value: Option<Tag>,
274            set: bool,
275        }
276
277        impl Extract {
278            fn set(&mut self, tag: Option<&Tag>) -> Result {
279                if self.set {
280                    return crate::error();
281                }
282
283                self.set = true;
284                self.value = tag.cloned();
285
286                Ok(())
287            }
288        }
289
290        impl<'sval> Stream<'sval> for Extract {
291            fn tag(&mut self, tag: Option<&Tag>, _: Option<&Label>, _: Option<&Index>) -> Result {
292                self.set(tag)
293            }
294
295            fn tagged_begin(
296                &mut self,
297                tag: Option<&Tag>,
298                _: Option<&Label>,
299                _: Option<&Index>,
300            ) -> Result {
301                self.set(tag)
302            }
303
304            fn enum_begin(
305                &mut self,
306                tag: Option<&Tag>,
307                _: Option<&Label>,
308                _: Option<&Index>,
309            ) -> Result {
310                self.set(tag)
311            }
312
313            fn record_begin(
314                &mut self,
315                tag: Option<&Tag>,
316                _: Option<&Label>,
317                _: Option<&Index>,
318                _: Option<usize>,
319            ) -> Result {
320                self.set(tag)
321            }
322
323            fn tuple_begin(
324                &mut self,
325                tag: Option<&Tag>,
326                _: Option<&Label>,
327                _: Option<&Index>,
328                _: Option<usize>,
329            ) -> Result {
330                self.set(tag)
331            }
332
333            fn bool(&mut self, _: bool) -> Result {
334                crate::error()
335            }
336
337            fn null(&mut self) -> Result {
338                crate::error()
339            }
340
341            fn text_begin(&mut self, _: Option<usize>) -> Result {
342                crate::error()
343            }
344
345            fn text_fragment_computed(&mut self, _: &str) -> Result {
346                crate::error()
347            }
348
349            fn text_end(&mut self) -> Result {
350                crate::error()
351            }
352
353            fn i64(&mut self, _: i64) -> Result {
354                self.set(Some(&crate::tags::NUMBER))
355            }
356
357            fn f64(&mut self, _: f64) -> Result {
358                self.set(Some(&crate::tags::NUMBER))
359            }
360
361            fn seq_begin(&mut self, _: Option<usize>) -> Result {
362                crate::error()
363            }
364
365            fn seq_value_begin(&mut self) -> Result {
366                crate::error()
367            }
368
369            fn seq_value_end(&mut self) -> Result {
370                crate::error()
371            }
372
373            fn seq_end(&mut self) -> Result {
374                crate::error()
375            }
376        }
377
378        let mut extract = Extract {
379            value: None,
380            set: false,
381        };
382
383        let _ = value.stream(&mut extract);
384
385        extract.value
386    }
387
388    /**
389    Try convert this value into a boolean.
390    */
391    pub fn to_bool(value: &(impl Value + ?Sized)) -> Option<bool> {
392        struct Extract(Option<bool>);
393
394        impl<'sval> Stream<'sval> for Extract {
395            fn bool(&mut self, value: bool) -> Result {
396                self.0 = Some(value);
397                Ok(())
398            }
399
400            fn null(&mut self) -> Result {
401                crate::error()
402            }
403
404            fn text_begin(&mut self, _: Option<usize>) -> Result {
405                crate::error()
406            }
407
408            fn text_fragment_computed(&mut self, _: &str) -> Result {
409                crate::error()
410            }
411
412            fn text_end(&mut self) -> Result {
413                crate::error()
414            }
415
416            fn i64(&mut self, _: i64) -> Result {
417                crate::error()
418            }
419
420            fn f64(&mut self, _: f64) -> Result {
421                crate::error()
422            }
423
424            fn seq_begin(&mut self, _: Option<usize>) -> Result {
425                crate::error()
426            }
427
428            fn seq_value_begin(&mut self) -> Result {
429                crate::error()
430            }
431
432            fn seq_value_end(&mut self) -> Result {
433                crate::error()
434            }
435
436            fn seq_end(&mut self) -> Result {
437                crate::error()
438            }
439        }
440
441        let mut extract = Extract(None);
442        value.stream(&mut extract).ok()?;
443        extract.0
444    }
445
446    /**
447    Try convert this value into a 32bit binary floating point number.
448    */
449    pub fn to_f32(value: &(impl Value + ?Sized)) -> Option<f32> {
450        struct Extract(Option<f32>);
451
452        impl<'sval> Stream<'sval> for Extract {
453            fn f32(&mut self, value: f32) -> Result {
454                self.0 = Some(value);
455                Ok(())
456            }
457
458            fn null(&mut self) -> Result {
459                crate::error()
460            }
461
462            fn bool(&mut self, _: bool) -> Result {
463                crate::error()
464            }
465
466            fn text_begin(&mut self, _: Option<usize>) -> Result {
467                crate::error()
468            }
469
470            fn text_fragment_computed(&mut self, _: &str) -> Result {
471                crate::error()
472            }
473
474            fn text_end(&mut self) -> Result {
475                crate::error()
476            }
477
478            fn i64(&mut self, _: i64) -> Result {
479                crate::error()
480            }
481
482            fn f64(&mut self, _: f64) -> Result {
483                crate::error()
484            }
485
486            fn seq_begin(&mut self, _: Option<usize>) -> Result {
487                crate::error()
488            }
489
490            fn seq_value_begin(&mut self) -> Result {
491                crate::error()
492            }
493
494            fn seq_value_end(&mut self) -> Result {
495                crate::error()
496            }
497
498            fn seq_end(&mut self) -> Result {
499                crate::error()
500            }
501        }
502
503        let mut extract = Extract(None);
504        value.stream(&mut extract).ok()?;
505        extract.0
506    }
507
508    /**
509    Try convert this value into a 64bit binary floating point number.
510    */
511    pub fn to_f64(value: &(impl Value + ?Sized)) -> Option<f64> {
512        struct Extract(Option<f64>);
513
514        impl<'sval> Stream<'sval> for Extract {
515            fn f64(&mut self, value: f64) -> Result {
516                self.0 = Some(value);
517                Ok(())
518            }
519
520            fn null(&mut self) -> Result {
521                crate::error()
522            }
523
524            fn bool(&mut self, _: bool) -> Result {
525                crate::error()
526            }
527
528            fn text_begin(&mut self, _: Option<usize>) -> Result {
529                crate::error()
530            }
531
532            fn text_fragment_computed(&mut self, _: &str) -> Result {
533                crate::error()
534            }
535
536            fn text_end(&mut self) -> Result {
537                crate::error()
538            }
539
540            fn i64(&mut self, _: i64) -> Result {
541                crate::error()
542            }
543
544            fn seq_begin(&mut self, _: Option<usize>) -> Result {
545                crate::error()
546            }
547
548            fn seq_value_begin(&mut self) -> Result {
549                crate::error()
550            }
551
552            fn seq_value_end(&mut self) -> Result {
553                crate::error()
554            }
555
556            fn seq_end(&mut self) -> Result {
557                crate::error()
558            }
559        }
560
561        let mut extract = Extract(None);
562        value.stream(&mut extract).ok()?;
563        extract.0
564    }
565
566    /**
567    Try convert this value into a signed 8bit integer.
568    */
569    pub fn to_i8(value: &(impl Value + ?Sized)) -> Option<i8> {
570        value.to_i128().and_then(|value| value.try_into().ok())
571    }
572
573    /**
574    Try convert this value into a signed 16bit integer.
575    */
576    pub fn to_i16(value: &(impl Value + ?Sized)) -> Option<i16> {
577        value.to_i128().and_then(|value| value.try_into().ok())
578    }
579
580    /**
581    Try convert this value into a signed 32bit integer.
582    */
583    pub fn to_i32(value: &(impl Value + ?Sized)) -> Option<i32> {
584        value.to_i128().and_then(|value| value.try_into().ok())
585    }
586
587    /**
588    Try convert this value into a signed 64bit integer.
589    */
590    pub fn to_i64(value: &(impl Value + ?Sized)) -> Option<i64> {
591        value.to_i128().and_then(|value| value.try_into().ok())
592    }
593
594    /**
595    Try convert this value into a signed 128bit integer.
596    */
597    pub fn to_i128(value: &(impl Value + ?Sized)) -> Option<i128> {
598        struct Extract(Option<i128>);
599
600        impl<'sval> Stream<'sval> for Extract {
601            fn i128(&mut self, value: i128) -> Result {
602                self.0 = Some(value);
603                Ok(())
604            }
605
606            fn null(&mut self) -> Result {
607                crate::error()
608            }
609
610            fn bool(&mut self, _: bool) -> Result {
611                crate::error()
612            }
613
614            fn text_begin(&mut self, _: Option<usize>) -> Result {
615                crate::error()
616            }
617
618            fn text_fragment_computed(&mut self, _: &str) -> Result {
619                crate::error()
620            }
621
622            fn text_end(&mut self) -> Result {
623                crate::error()
624            }
625
626            fn i64(&mut self, _: i64) -> Result {
627                crate::error()
628            }
629
630            fn f64(&mut self, _: f64) -> Result {
631                crate::error()
632            }
633
634            fn seq_begin(&mut self, _: Option<usize>) -> Result {
635                crate::error()
636            }
637
638            fn seq_value_begin(&mut self) -> Result {
639                crate::error()
640            }
641
642            fn seq_value_end(&mut self) -> Result {
643                crate::error()
644            }
645
646            fn seq_end(&mut self) -> Result {
647                crate::error()
648            }
649        }
650
651        let mut extract = Extract(None);
652        value.stream(&mut extract).ok()?;
653        extract.0
654    }
655
656    /**
657    Try convert this value into an unsigned 8bit integer.
658    */
659    pub fn to_u8(value: &(impl Value + ?Sized)) -> Option<u8> {
660        value.to_u128().and_then(|value| value.try_into().ok())
661    }
662
663    /**
664    Try convert this value into an unsigned 16bit integer.
665    */
666    pub fn to_u16(value: &(impl Value + ?Sized)) -> Option<u16> {
667        value.to_u128().and_then(|value| value.try_into().ok())
668    }
669
670    /**
671    Try convert this value into an unsigned 32bit integer.
672    */
673    pub fn to_u32(value: &(impl Value + ?Sized)) -> Option<u32> {
674        value.to_u128().and_then(|value| value.try_into().ok())
675    }
676
677    /**
678    Try convert this value into an unsigned 64bit integer.
679    */
680    pub fn to_u64(value: &(impl Value + ?Sized)) -> Option<u64> {
681        value.to_u128().and_then(|value| value.try_into().ok())
682    }
683
684    /**
685    Try convert this value into an unsigned 128bit integer.
686    */
687    pub fn to_u128(value: &(impl Value + ?Sized)) -> Option<u128> {
688        struct Extract(Option<u128>);
689
690        impl<'sval> Stream<'sval> for Extract {
691            fn u128(&mut self, value: u128) -> Result {
692                self.0 = Some(value);
693                Ok(())
694            }
695
696            fn null(&mut self) -> Result {
697                crate::error()
698            }
699
700            fn bool(&mut self, _: bool) -> Result {
701                crate::error()
702            }
703
704            fn text_begin(&mut self, _: Option<usize>) -> Result {
705                crate::error()
706            }
707
708            fn text_fragment_computed(&mut self, _: &str) -> Result {
709                crate::error()
710            }
711
712            fn text_end(&mut self) -> Result {
713                crate::error()
714            }
715
716            fn i64(&mut self, _: i64) -> Result {
717                crate::error()
718            }
719
720            fn f64(&mut self, _: f64) -> Result {
721                crate::error()
722            }
723
724            fn seq_begin(&mut self, _: Option<usize>) -> Result {
725                crate::error()
726            }
727
728            fn seq_value_begin(&mut self) -> Result {
729                crate::error()
730            }
731
732            fn seq_value_end(&mut self) -> Result {
733                crate::error()
734            }
735
736            fn seq_end(&mut self) -> Result {
737                crate::error()
738            }
739        }
740
741        let mut extract = Extract(None);
742        value.stream(&mut extract).ok()?;
743        extract.0
744    }
745
746    /**
747    Try convert this value into a text string.
748    */
749    pub fn to_text(value: &(impl Value + ?Sized)) -> Option<&str> {
750        struct Extract<'sval> {
751            extracted: Option<&'sval str>,
752            seen_fragment: bool,
753        }
754
755        impl<'sval> Stream<'sval> for Extract<'sval> {
756            fn text_begin(&mut self, _: Option<usize>) -> Result {
757                Ok(())
758            }
759
760            fn text_fragment(&mut self, fragment: &'sval str) -> Result {
761                // Allow either independent strings, or fragments of a single borrowed string
762                if !self.seen_fragment {
763                    self.extracted = Some(fragment);
764                    self.seen_fragment = true;
765                } else {
766                    self.extracted = None;
767                }
768
769                Ok(())
770            }
771
772            fn text_fragment_computed(&mut self, _: &str) -> Result {
773                self.extracted = None;
774                self.seen_fragment = true;
775
776                crate::error()
777            }
778
779            fn text_end(&mut self) -> Result {
780                Ok(())
781            }
782
783            fn null(&mut self) -> Result {
784                crate::error()
785            }
786
787            fn bool(&mut self, _: bool) -> Result {
788                crate::error()
789            }
790
791            fn i64(&mut self, _: i64) -> Result {
792                crate::error()
793            }
794
795            fn f64(&mut self, _: f64) -> Result {
796                crate::error()
797            }
798
799            fn seq_begin(&mut self, _: Option<usize>) -> Result {
800                crate::error()
801            }
802
803            fn seq_value_begin(&mut self) -> Result {
804                crate::error()
805            }
806
807            fn seq_value_end(&mut self) -> Result {
808                crate::error()
809            }
810
811            fn seq_end(&mut self) -> Result {
812                crate::error()
813            }
814        }
815
816        let mut extract = Extract {
817            extracted: None,
818            seen_fragment: false,
819        };
820
821        value.stream(&mut extract).ok()?;
822        extract.extracted
823    }
824
825    /**
826    Try convert this value into a bitstring.
827    */
828    pub fn to_binary(value: &(impl Value + ?Sized)) -> Option<&[u8]> {
829        struct Extract<'sval> {
830            extracted: Option<&'sval [u8]>,
831            seen_fragment: bool,
832        }
833
834        impl<'sval> Stream<'sval> for Extract<'sval> {
835            fn binary_begin(&mut self, _: Option<usize>) -> Result {
836                Ok(())
837            }
838
839            fn binary_fragment(&mut self, fragment: &'sval [u8]) -> Result {
840                // Allow either independent bytes, or fragments of a single borrowed byte stream
841                if !self.seen_fragment {
842                    self.extracted = Some(fragment);
843                    self.seen_fragment = true;
844                } else {
845                    self.extracted = None;
846                }
847
848                Ok(())
849            }
850
851            fn binary_fragment_computed(&mut self, _: &[u8]) -> Result {
852                self.extracted = None;
853                self.seen_fragment = true;
854
855                crate::error()
856            }
857
858            fn binary_end(&mut self) -> Result {
859                Ok(())
860            }
861
862            fn null(&mut self) -> Result {
863                crate::error()
864            }
865
866            fn bool(&mut self, _: bool) -> Result {
867                crate::error()
868            }
869
870            fn text_begin(&mut self, _: Option<usize>) -> Result {
871                crate::error()
872            }
873
874            fn text_fragment_computed(&mut self, _: &str) -> Result {
875                crate::error()
876            }
877
878            fn text_end(&mut self) -> Result {
879                crate::error()
880            }
881
882            fn i64(&mut self, _: i64) -> Result {
883                crate::error()
884            }
885
886            fn f64(&mut self, _: f64) -> Result {
887                crate::error()
888            }
889
890            fn seq_begin(&mut self, _: Option<usize>) -> Result {
891                crate::error()
892            }
893
894            fn seq_value_begin(&mut self) -> Result {
895                crate::error()
896            }
897
898            fn seq_value_end(&mut self) -> Result {
899                crate::error()
900            }
901
902            fn seq_end(&mut self) -> Result {
903                crate::error()
904            }
905        }
906
907        let mut extract = Extract {
908            extracted: None,
909            seen_fragment: false,
910        };
911
912        value.stream(&mut extract).ok()?;
913        extract.extracted
914    }
915}