1use std::borrow::Borrow;
18use std::borrow::Cow;
19use std::collections::HashMap;
20use std::collections::hash_map;
21use std::fmt;
22use std::slice;
23
24use crate::Error;
25use crate::str::RefStr;
26
27pub trait Visitor {
29 fn visit(&mut self, key: KeyView, value: ValueView) -> Result<(), Error>;
31}
32
33#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
35pub struct Key<'a>(RefStr<'a>);
36
37impl fmt::Display for Key<'_> {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 fmt::Display::fmt(&self.0, f)
40 }
41}
42
43#[cfg(feature = "serde")]
44impl serde::Serialize for Key<'_> {
45 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
46 serializer.collect_str(self)
47 }
48}
49
50impl Borrow<str> for Key<'_> {
51 fn borrow(&self) -> &str {
52 &self.0
53 }
54}
55
56impl Key<'static> {
57 pub const fn new(k: &'static str) -> Key<'static> {
59 Key(RefStr::Static(k))
60 }
61}
62
63impl<'a> Key<'a> {
64 pub const fn borrowed(k: &'a str) -> Key<'a> {
68 Key(RefStr::Borrowed(k))
69 }
70}
71
72impl Key<'_> {
73 pub fn view(&self) -> KeyView<'_> {
75 KeyView(self.0)
76 }
77}
78
79#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
81pub struct KeyOwned(Cow<'static, str>);
82
83impl Borrow<str> for KeyOwned {
84 fn borrow(&self) -> &str {
85 &self.0
86 }
87}
88
89impl fmt::Display for KeyOwned {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 fmt::Display::fmt(&self.0, f)
92 }
93}
94
95#[cfg(feature = "serde")]
96impl serde::Serialize for KeyOwned {
97 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
98 serializer.collect_str(self)
99 }
100}
101
102impl KeyOwned {
103 pub fn new(k: impl Into<Cow<'static, str>>) -> KeyOwned {
105 KeyOwned(k.into())
106 }
107}
108
109impl KeyOwned {
110 pub fn view(&self) -> KeyView<'_> {
112 KeyView(match &self.0 {
113 Cow::Borrowed(s) => RefStr::Static(s),
114 Cow::Owned(s) => RefStr::Borrowed(s),
115 })
116 }
117}
118
119#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
121pub struct KeyView<'a>(RefStr<'a>);
122
123impl Borrow<str> for KeyView<'_> {
124 fn borrow(&self) -> &str {
125 &self.0
126 }
127}
128
129impl fmt::Display for KeyView<'_> {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 fmt::Display::fmt(&self.0, f)
132 }
133}
134
135#[cfg(feature = "serde")]
136impl serde::Serialize for KeyView<'_> {
137 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
138 serializer.collect_str(self)
139 }
140}
141
142impl KeyView<'_> {
143 pub fn to_owned(&self) -> KeyOwned {
145 KeyOwned(self.0.into_cow_static())
146 }
147
148 pub fn to_cow(&self) -> Cow<'static, str> {
150 self.0.into_cow_static()
151 }
152
153 pub fn as_str(&self) -> &str {
155 self.0.get()
156 }
157}
158
159#[derive(Clone, Copy)]
161pub struct DebugValue<'a>(&'a dyn fmt::Debug);
162
163impl fmt::Debug for DebugValue<'_> {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 fmt::Debug::fmt(&self.0, f)
166 }
167}
168
169impl fmt::Display for DebugValue<'_> {
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 fmt::Debug::fmt(&self.0, f)
172 }
173}
174
175#[derive(Clone, Copy)]
177pub struct DisplayValue<'a>(&'a dyn fmt::Display);
178
179impl fmt::Debug for DisplayValue<'_> {
180 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181 fmt::Display::fmt(&self.0, f)
182 }
183}
184
185impl fmt::Display for DisplayValue<'_> {
186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187 fmt::Display::fmt(&self.0, f)
188 }
189}
190
191#[derive(Debug, Clone, Copy)]
193pub struct ListValue<'a>(ListValueState<'a>);
194
195#[derive(Debug, Clone, Copy)]
196enum ListValueState<'a> {
197 Borrowed(&'a [Value<'a>]),
198 Owned(&'a [ValueOwned]),
199}
200
201#[cfg(feature = "serde")]
202impl serde::Serialize for ListValue<'_> {
203 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
204 serializer.collect_seq(self.iter())
205 }
206}
207
208impl<'a> ListValue<'a> {
209 pub fn len(&self) -> usize {
211 match self.0 {
212 ListValueState::Borrowed(p) => p.len(),
213 ListValueState::Owned(p) => p.len(),
214 }
215 }
216
217 pub fn is_empty(&self) -> bool {
219 match self.0 {
220 ListValueState::Borrowed(p) => p.is_empty(),
221 ListValueState::Owned(p) => p.is_empty(),
222 }
223 }
224
225 pub fn iter(&self) -> ListValueIter<'a> {
227 match self.0 {
228 ListValueState::Borrowed(v) => ListValueIter(ListValueIterState::Borrowed(v.iter())),
229 ListValueState::Owned(v) => ListValueIter(ListValueIterState::Owned(v.iter())),
230 }
231 }
232}
233
234#[derive(Debug, Clone)]
236pub struct ListValueIter<'a>(ListValueIterState<'a>);
237
238#[derive(Debug, Clone)]
239enum ListValueIterState<'a> {
240 Borrowed(slice::Iter<'a, Value<'a>>),
241 Owned(slice::Iter<'a, ValueOwned>),
242}
243
244impl<'a> Iterator for ListValueIter<'a> {
245 type Item = ValueView<'a>;
246
247 fn next(&mut self) -> Option<Self::Item> {
248 match &mut self.0 {
249 ListValueIterState::Borrowed(v) => v.next().map(|v| v.view()),
250 ListValueIterState::Owned(v) => v.next().map(|v| v.view()),
251 }
252 }
253
254 fn size_hint(&self) -> (usize, Option<usize>) {
255 match &self.0 {
256 ListValueIterState::Borrowed(v) => v.size_hint(),
257 ListValueIterState::Owned(v) => v.size_hint(),
258 }
259 }
260}
261
262#[derive(Debug, Clone, Copy)]
264pub struct MapValue<'a>(MapValueState<'a>);
265
266#[derive(Debug, Clone, Copy)]
267enum MapValueState<'a> {
268 Borrowed(&'a [(Key<'a>, Value<'a>)]),
269 Owned(&'a HashMap<KeyOwned, ValueOwned>),
270}
271
272#[cfg(feature = "serde")]
273impl serde::Serialize for MapValue<'_> {
274 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
275 serializer.collect_map(self.iter())
276 }
277}
278
279impl<'a> MapValue<'a> {
280 pub fn len(&self) -> usize {
282 match self.0 {
283 MapValueState::Borrowed(p) => p.len(),
284 MapValueState::Owned(p) => p.len(),
285 }
286 }
287
288 pub fn is_empty(&self) -> bool {
290 match self.0 {
291 MapValueState::Borrowed(p) => p.is_empty(),
292 MapValueState::Owned(p) => p.is_empty(),
293 }
294 }
295
296 pub fn iter(&self) -> MapValueIter<'a> {
298 match self.0 {
299 MapValueState::Borrowed(v) => MapValueIter(MapValueIterState::Borrowed(v.iter())),
300 MapValueState::Owned(v) => MapValueIter(MapValueIterState::Owned(v.iter())),
301 }
302 }
303
304 pub fn get(&self, key: &str) -> Option<ValueView<'a>> {
306 match &self.0 {
307 MapValueState::Borrowed(p) => p
308 .iter()
309 .find_map(|(k, v)| if (&*k.0) != key { None } else { Some(v.view()) }),
310 MapValueState::Owned(p) => p.get(key).map(|v| v.view()),
311 }
312 }
313}
314
315#[derive(Debug, Clone)]
317pub struct MapValueIter<'a>(MapValueIterState<'a>);
318
319#[derive(Debug, Clone)]
320enum MapValueIterState<'a> {
321 Borrowed(slice::Iter<'a, (Key<'a>, Value<'a>)>),
322 Owned(hash_map::Iter<'a, KeyOwned, ValueOwned>),
323}
324
325impl<'a> Iterator for MapValueIter<'a> {
326 type Item = (KeyView<'a>, ValueView<'a>);
327
328 fn next(&mut self) -> Option<Self::Item> {
329 match &mut self.0 {
330 MapValueIterState::Borrowed(v) => v.next().map(|(k, v)| (k.view(), v.view())),
331 MapValueIterState::Owned(v) => v.next().map(|(k, v)| (k.view(), v.view())),
332 }
333 }
334
335 fn size_hint(&self) -> (usize, Option<usize>) {
336 match &self.0 {
337 MapValueIterState::Borrowed(v) => v.size_hint(),
338 MapValueIterState::Owned(v) => v.size_hint(),
339 }
340 }
341}
342
343#[derive(Debug, Clone)]
345pub struct Value<'a>(ValueState<'a>);
346
347#[derive(Clone)]
348enum ValueState<'a> {
349 None,
350 Bool(bool),
351 I64(i64),
352 U64(u64),
353 F64(f64),
354 I128(i128),
355 U128(u128),
356 Char(char),
357 Str(RefStr<'a>),
358 Bytes(&'a [u8]),
359 List(&'a [Value<'a>]),
360 Map(&'a [(Key<'a>, Value<'a>)]),
361 Debug(&'a dyn fmt::Debug),
362 Display(&'a dyn fmt::Display),
363}
364
365impl fmt::Debug for ValueState<'_> {
366 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
367 match self {
368 ValueState::None => write!(f, "<none>"),
369 ValueState::Bool(v) => v.fmt(f),
370 ValueState::I64(v) => v.fmt(f),
371 ValueState::U64(v) => v.fmt(f),
372 ValueState::F64(v) => v.fmt(f),
373 ValueState::I128(v) => v.fmt(f),
374 ValueState::U128(v) => v.fmt(f),
375 ValueState::Char(v) => v.fmt(f),
376 ValueState::Str(v) => v.fmt(f),
377 ValueState::Bytes(v) => v.fmt(f),
378 ValueState::List(v) => v.fmt(f),
379 ValueState::Map(v) => v.fmt(f),
380 ValueState::Debug(v) => fmt::Debug::fmt(v, f),
381 ValueState::Display(v) => fmt::Display::fmt(v, f),
382 }
383 }
384}
385
386#[cfg(feature = "serde")]
387impl serde::Serialize for Value<'_> {
388 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
389 self.view().serialize(serializer)
390 }
391}
392
393impl Value<'_> {
394 pub fn view(&self) -> ValueView<'_> {
396 match self.0 {
397 ValueState::None => ValueView::None,
398 ValueState::Bool(b) => ValueView::Bool(b),
399 ValueState::I64(i) => ValueView::I64(i),
400 ValueState::U64(u) => ValueView::U64(u),
401 ValueState::F64(f) => ValueView::F64(f),
402 ValueState::I128(i) => ValueView::I128(i),
403 ValueState::U128(u) => ValueView::U128(u),
404 ValueState::Char(c) => ValueView::Char(c),
405 ValueState::Str(RefStr::Static(s)) => ValueView::StaticStr(s),
406 ValueState::Str(RefStr::Borrowed(s)) => ValueView::BorrowedStr(s),
407 ValueState::Bytes(b) => ValueView::Bytes(b),
408 ValueState::List(l) => ValueView::List(ListValue(ListValueState::Borrowed(l))),
409 ValueState::Map(m) => ValueView::Map(MapValue(MapValueState::Borrowed(m))),
410 ValueState::Debug(d) => ValueView::Debug(DebugValue(d)),
411 ValueState::Display(d) => ValueView::Display(DisplayValue(d)),
412 }
413 }
414}
415
416impl<'a> Value<'a> {
417 pub fn none() -> Value<'a> {
419 Value(ValueState::None)
420 }
421
422 pub fn str(s: &'a str) -> Self {
424 Value(ValueState::Str(RefStr::Borrowed(s)))
425 }
426
427 pub fn static_str(s: &'static str) -> Self {
429 Value(ValueState::Str(RefStr::Static(s)))
430 }
431
432 pub fn bytes(b: &'a [u8]) -> Self {
434 Value(ValueState::Bytes(b))
435 }
436
437 pub fn bool(b: bool) -> Self {
439 Value(ValueState::Bool(b))
440 }
441
442 pub fn i64(i: i64) -> Self {
444 Value(ValueState::I64(i))
445 }
446
447 pub fn u64(u: u64) -> Self {
449 Value(ValueState::U64(u))
450 }
451
452 pub fn f64(f: f64) -> Self {
454 Value(ValueState::F64(f))
455 }
456
457 pub fn i128(i: i128) -> Self {
459 Value(ValueState::I128(i))
460 }
461
462 pub fn u128(u: u128) -> Self {
464 Value(ValueState::U128(u))
465 }
466
467 pub fn char(c: char) -> Self {
469 Value(ValueState::Char(c))
470 }
471
472 pub fn list(l: &'a [Value<'a>]) -> Self {
474 Value(ValueState::List(l))
475 }
476
477 pub fn map(m: &'a [(Key<'a>, Value<'a>)]) -> Self {
479 Value(ValueState::Map(m))
480 }
481
482 pub fn debug(d: &'a dyn fmt::Debug) -> Self {
484 Value(ValueState::Debug(d))
485 }
486
487 pub fn display(d: &'a dyn fmt::Display) -> Self {
489 Value(ValueState::Display(d))
490 }
491}
492
493#[derive(Debug, Clone)]
495pub struct ValueOwned(ValueOwnedState);
496
497#[derive(Debug, Clone)]
498enum ValueOwnedState {
499 None,
500 Bool(bool),
501 I64(i64),
502 U64(u64),
503 F64(f64),
504 I128(i128),
505 U128(u128),
506 Char(char),
507 Str(Cow<'static, str>),
508 #[expect(clippy::box_collection)]
509 Bytes(Box<Vec<u8>>),
510 #[expect(clippy::box_collection)]
511 List(Box<Vec<ValueOwned>>),
512 #[expect(clippy::box_collection)]
513 Map(Box<HashMap<KeyOwned, ValueOwned>>),
514}
515
516#[cfg(feature = "serde")]
517impl serde::Serialize for ValueOwned {
518 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
519 self.view().serialize(serializer)
520 }
521}
522
523impl ValueOwned {
524 pub fn view(&self) -> ValueView<'_> {
526 match &self.0 {
527 ValueOwnedState::None => ValueView::None,
528 ValueOwnedState::Bool(b) => ValueView::Bool(*b),
529 ValueOwnedState::I64(i) => ValueView::I64(*i),
530 ValueOwnedState::U64(u) => ValueView::U64(*u),
531 ValueOwnedState::F64(f) => ValueView::F64(*f),
532 ValueOwnedState::I128(i) => ValueView::I128(*i),
533 ValueOwnedState::U128(u) => ValueView::U128(*u),
534 ValueOwnedState::Char(c) => ValueView::Char(*c),
535 ValueOwnedState::Str(Cow::Borrowed(s)) => ValueView::StaticStr(s),
536 ValueOwnedState::Str(Cow::Owned(s)) => ValueView::BorrowedStr(s.as_str()),
537 ValueOwnedState::Bytes(b) => ValueView::Bytes(b.as_slice()),
538 ValueOwnedState::List(l) => ValueView::List(ListValue(ListValueState::Owned(l))),
539 ValueOwnedState::Map(m) => ValueView::Map(MapValue(MapValueState::Owned(m))),
540 }
541 }
542}
543
544impl ValueOwned {
545 pub fn none() -> ValueOwned {
547 ValueOwned(ValueOwnedState::None)
548 }
549
550 pub fn bool(b: bool) -> ValueOwned {
552 ValueOwned(ValueOwnedState::Bool(b))
553 }
554
555 pub fn i64(i: i64) -> ValueOwned {
557 ValueOwned(ValueOwnedState::I64(i))
558 }
559
560 pub fn u64(u: u64) -> ValueOwned {
562 ValueOwned(ValueOwnedState::U64(u))
563 }
564
565 pub fn f64(f: f64) -> ValueOwned {
567 ValueOwned(ValueOwnedState::F64(f))
568 }
569
570 pub fn i128(i: i128) -> ValueOwned {
572 ValueOwned(ValueOwnedState::I128(i))
573 }
574
575 pub fn u128(u: u128) -> ValueOwned {
577 ValueOwned(ValueOwnedState::U128(u))
578 }
579
580 pub fn char(c: char) -> ValueOwned {
582 ValueOwned(ValueOwnedState::Char(c))
583 }
584
585 pub fn str(s: impl Into<Cow<'static, str>>) -> ValueOwned {
587 ValueOwned(ValueOwnedState::Str(s.into()))
588 }
589
590 pub fn bytes(b: impl Into<Vec<u8>>) -> ValueOwned {
592 ValueOwned(ValueOwnedState::Bytes(Box::new(b.into())))
593 }
594
595 pub fn list(l: impl IntoIterator<Item = ValueOwned>) -> ValueOwned {
597 ValueOwned(ValueOwnedState::List(Box::new(l.into_iter().collect())))
598 }
599
600 pub fn map(m: impl IntoIterator<Item = (KeyOwned, ValueOwned)>) -> ValueOwned {
602 ValueOwned(ValueOwnedState::Map(Box::new(m.into_iter().collect())))
603 }
604
605 pub fn from_vec(v: Vec<ValueOwned>) -> ValueOwned {
607 ValueOwned(ValueOwnedState::List(Box::new(v)))
608 }
609
610 pub fn from_hash_map(m: HashMap<KeyOwned, ValueOwned>) -> ValueOwned {
612 ValueOwned(ValueOwnedState::Map(Box::new(m)))
613 }
614}
615
616#[derive(Debug, Clone)]
618#[non_exhaustive]
619pub enum ValueView<'a> {
620 None,
622 BorrowedStr(&'a str),
624 StaticStr(&'static str),
626 Bytes(&'a [u8]),
628 Bool(bool),
630 I64(i64),
632 U64(u64),
634 F64(f64),
636 I128(i128),
638 U128(u128),
640 Char(char),
642 List(ListValue<'a>),
644 Map(MapValue<'a>),
646 Debug(DebugValue<'a>),
648 Display(DisplayValue<'a>),
650}
651
652impl fmt::Display for ValueView<'_> {
653 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
654 match self {
655 ValueView::None => write!(f, "<none>"),
656 ValueView::BorrowedStr(v) => v.fmt(f),
657 ValueView::StaticStr(v) => v.fmt(f),
658 ValueView::Bytes(v) => {
659 write!(f, "b\"")?;
662 for &b in v.iter() {
663 if b == b'\n' {
665 write!(f, "\\n")?;
666 } else if b == b'\r' {
667 write!(f, "\\r")?;
668 } else if b == b'\t' {
669 write!(f, "\\t")?;
670 } else if b == b'\\' || b == b'"' {
671 write!(f, "\\{}", b as char)?;
672 } else if b == b'\0' {
673 write!(f, "\\0")?;
674 } else if (0x20..0x7f).contains(&b) {
676 write!(f, "{}", b as char)?;
677 } else {
678 write!(f, "\\x{:02x}", b)?;
679 }
680 }
681 write!(f, "\"")?;
682 Ok(())
683 }
684 ValueView::Bool(v) => v.fmt(f),
685 ValueView::I64(v) => v.fmt(f),
686 ValueView::U64(v) => v.fmt(f),
687 ValueView::F64(v) => v.fmt(f),
688 ValueView::I128(v) => v.fmt(f),
689 ValueView::U128(v) => v.fmt(f),
690 ValueView::Char(v) => v.fmt(f),
691 ValueView::List(v) => {
692 let mut dbg = f.debug_list();
693 for item in v.iter() {
694 dbg.entry(&item);
695 }
696 dbg.finish()
697 }
698 ValueView::Map(v) => {
699 let mut dbg = f.debug_map();
700 for (k, v) in v.iter() {
701 dbg.entry(&k, &v);
702 }
703 dbg.finish()
704 }
705 ValueView::Debug(v) => fmt::Debug::fmt(v, f),
706 ValueView::Display(v) => fmt::Display::fmt(v, f),
707 }
708 }
709}
710
711#[cfg(feature = "serde")]
712impl serde::Serialize for ValueView<'_> {
713 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
714 match &self {
715 ValueView::None => serializer.serialize_none(),
716 ValueView::BorrowedStr(v) => serializer.serialize_str(v),
717 ValueView::StaticStr(v) => serializer.serialize_str(v),
718 ValueView::Bytes(v) => serializer.serialize_bytes(v),
719 ValueView::Bool(v) => serializer.serialize_bool(*v),
720 ValueView::I64(v) => serializer.serialize_i64(*v),
721 ValueView::U64(v) => serializer.serialize_u64(*v),
722 ValueView::F64(v) => serializer.serialize_f64(*v),
723 ValueView::I128(v) => serializer.serialize_i128(*v),
724 ValueView::U128(v) => serializer.serialize_u128(*v),
725 ValueView::Char(v) => serializer.serialize_char(*v),
726 ValueView::List(v) => v.serialize(serializer),
727 ValueView::Map(v) => v.serialize(serializer),
728 ValueView::Debug(v) => serializer.collect_str(v),
729 ValueView::Display(v) => serializer.collect_str(v),
730 }
731 }
732}
733
734impl ValueView<'_> {
735 pub fn to_owned(&self) -> ValueOwned {
737 match &self {
738 ValueView::None => ValueOwned(ValueOwnedState::None),
739 ValueView::BorrowedStr(s) => {
740 ValueOwned(ValueOwnedState::Str(Cow::Owned(s.to_string())))
741 }
742 ValueView::StaticStr(s) => ValueOwned(ValueOwnedState::Str(Cow::Borrowed(s))),
743 ValueView::Bytes(b) => ValueOwned(ValueOwnedState::Bytes(Box::new(b.to_vec()))),
744 ValueView::Bool(b) => ValueOwned(ValueOwnedState::Bool(*b)),
745 ValueView::I64(i) => ValueOwned(ValueOwnedState::I64(*i)),
746 ValueView::U64(u) => ValueOwned(ValueOwnedState::U64(*u)),
747 ValueView::F64(f) => ValueOwned(ValueOwnedState::F64(*f)),
748 ValueView::I128(i) => ValueOwned(ValueOwnedState::I128(*i)),
749 ValueView::U128(u) => ValueOwned(ValueOwnedState::U128(*u)),
750 ValueView::Char(c) => ValueOwned(ValueOwnedState::Char(*c)),
751 ValueView::List(l) => ValueOwned(ValueOwnedState::List(Box::new(
752 l.iter().map(|v| v.to_owned()).collect(),
753 ))),
754 ValueView::Map(m) => ValueOwned(ValueOwnedState::Map(Box::new(
755 m.iter()
756 .map(|(k, v)| (k.to_owned(), v.to_owned()))
757 .collect(),
758 ))),
759 ValueView::Debug(d) => ValueOwned(ValueOwnedState::Str(Cow::Owned(format!("{d:?}")))),
760 ValueView::Display(d) => ValueOwned(ValueOwnedState::Str(Cow::Owned(format!("{d}")))),
761 }
762 }
763}
764
765impl<'a> ValueView<'a> {
766 pub fn to_bool(&self) -> Option<bool> {
768 if let ValueView::Bool(b) = self {
769 Some(*b)
770 } else {
771 None
772 }
773 }
774
775 pub fn to_i64(&self) -> Option<i64> {
777 if let ValueView::I64(i) = self {
778 Some(*i)
779 } else {
780 None
781 }
782 }
783
784 pub fn to_u64(&self) -> Option<u64> {
786 if let ValueView::U64(u) = self {
787 Some(*u)
788 } else {
789 None
790 }
791 }
792
793 pub fn to_f64(&self) -> Option<f64> {
795 if let ValueView::F64(f) = self {
796 Some(*f)
797 } else {
798 None
799 }
800 }
801
802 pub fn to_i128(&self) -> Option<i128> {
804 if let ValueView::I128(i) = self {
805 Some(*i)
806 } else {
807 None
808 }
809 }
810
811 pub fn to_u128(&self) -> Option<u128> {
813 if let ValueView::U128(u) = self {
814 Some(*u)
815 } else {
816 None
817 }
818 }
819
820 pub fn to_char(&self) -> Option<char> {
822 if let ValueView::Char(c) = self {
823 Some(*c)
824 } else {
825 None
826 }
827 }
828
829 pub fn to_str(&self) -> Option<&'a str> {
831 if let ValueView::BorrowedStr(s) = self {
832 Some(*s)
833 } else if let ValueView::StaticStr(s) = self {
834 Some(*s)
835 } else {
836 None
837 }
838 }
839
840 pub fn to_static_str(&self) -> Option<&'static str> {
842 if let ValueView::StaticStr(s) = self {
843 Some(*s)
844 } else {
845 None
846 }
847 }
848
849 pub fn to_display(&self) -> Option<DisplayValue<'a>> {
851 if let ValueView::Display(d) = self {
852 Some(*d)
853 } else {
854 None
855 }
856 }
857
858 pub fn to_list(&self) -> Option<ListValue<'a>> {
860 if let ValueView::List(l) = self {
861 Some(*l)
862 } else {
863 None
864 }
865 }
866
867 pub fn to_map(&self) -> Option<MapValue<'a>> {
869 if let ValueView::Map(m) = self {
870 Some(*m)
871 } else {
872 None
873 }
874 }
875
876 pub fn to_debug(&self) -> Option<DebugValue<'a>> {
878 if let ValueView::Debug(d) = self {
879 Some(*d)
880 } else {
881 None
882 }
883 }
884}
885
886#[derive(Debug, Clone, Copy)]
888pub struct KeyValues<'a>(KeyValuesState<'a>);
889
890#[derive(Debug, Clone, Copy)]
891enum KeyValuesState<'a> {
892 Borrowed(&'a [(Key<'a>, Value<'a>)]),
893 Owned(&'a [(KeyOwned, ValueOwned)]),
894}
895
896impl KeyValues<'_> {
897 pub fn empty() -> Self {
899 KeyValues(KeyValuesState::Borrowed(&[]))
900 }
901}
902
903impl<'a> KeyValues<'a> {
904 pub fn len(&self) -> usize {
906 match self.0 {
907 KeyValuesState::Borrowed(p) => p.len(),
908 KeyValuesState::Owned(p) => p.len(),
909 }
910 }
911
912 pub fn is_empty(&self) -> bool {
914 match self.0 {
915 KeyValuesState::Borrowed(p) => p.is_empty(),
916 KeyValuesState::Owned(p) => p.is_empty(),
917 }
918 }
919
920 pub fn iter(&self) -> KeyValuesIter<'a> {
922 match &self.0 {
923 KeyValuesState::Borrowed(p) => KeyValuesIter(KeyValuesIterState::Borrowed(p.iter())),
924 KeyValuesState::Owned(p) => KeyValuesIter(KeyValuesIterState::Owned(p.iter())),
925 }
926 }
927
928 pub fn get(&self, key: &str) -> Option<ValueView<'a>> {
930 match &self.0 {
931 KeyValuesState::Borrowed(p) => p
932 .iter()
933 .find_map(|(k, v)| if (&*k.0) != key { None } else { Some(v.view()) }),
934 KeyValuesState::Owned(p) => p
935 .iter()
936 .find_map(|(k, v)| if (&*k.0) != key { None } else { Some(v.view()) }),
937 }
938 }
939
940 pub fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> {
942 for (k, v) in self.iter() {
943 visitor.visit(k, v)?;
944 }
945 Ok(())
946 }
947}
948
949impl<'a> From<&'a [(Key<'a>, Value<'a>)]> for KeyValues<'a> {
950 fn from(kvs: &'a [(Key<'a>, Value<'a>)]) -> Self {
951 Self(KeyValuesState::Borrowed(kvs))
952 }
953}
954
955impl<'a> From<&'a [(KeyOwned, ValueOwned)]> for KeyValues<'a> {
956 fn from(kvs: &'a [(KeyOwned, ValueOwned)]) -> Self {
957 Self(KeyValuesState::Owned(kvs))
958 }
959}
960
961pub struct KeyValuesIter<'a>(KeyValuesIterState<'a>);
963
964enum KeyValuesIterState<'a> {
965 Borrowed(slice::Iter<'a, (Key<'a>, Value<'a>)>),
966 Owned(slice::Iter<'a, (KeyOwned, ValueOwned)>),
967}
968
969impl<'a> Iterator for KeyValuesIter<'a> {
970 type Item = (KeyView<'a>, ValueView<'a>);
971
972 fn next(&mut self) -> Option<Self::Item> {
973 match &mut self.0 {
974 KeyValuesIterState::Borrowed(iter) => iter.next().map(|(k, v)| (k.view(), v.view())),
975 KeyValuesIterState::Owned(iter) => iter.next().map(|(k, v)| (k.view(), v.view())),
976 }
977 }
978
979 fn size_hint(&self) -> (usize, Option<usize>) {
980 match &self.0 {
981 KeyValuesIterState::Borrowed(iter) => iter.size_hint(),
982 KeyValuesIterState::Owned(iter) => iter.size_hint(),
983 }
984 }
985}