cbor_core/value.rs
1mod array;
2mod bytes;
3mod default_eq_ord_hash;
4mod float;
5mod index;
6mod int;
7mod map;
8mod simple_value;
9mod string;
10
11use std::collections::BTreeMap;
12use std::hash::{Hash, Hasher};
13use std::ops::{Index, IndexMut};
14use std::time::{Duration, SystemTime};
15use std::{cmp, io};
16
17use crate::{
18 ArgLength, Array, CtrlByte, DataType, DateTime, EpochTime, Error, Float, IntegerBytes, Major, Map, Result,
19 SimpleValue, Tag,
20};
21
22/// A single CBOR data item.
23///
24/// `Value` covers all CBOR major types: integers, floats, byte and text
25/// strings, arrays, maps, tagged values, and simple values (null, booleans).
26/// It encodes deterministically and decodes only canonical input.
27///
28/// # Creating values
29///
30/// Rust primitives convert via [`From`]:
31///
32/// ```
33/// use cbor_core::Value;
34///
35/// let n = Value::from(42);
36/// let s = Value::from("hello");
37/// let b = Value::from(true);
38/// ```
39///
40/// For arrays and maps the `array!` and `map!` macros are convenient:
41///
42/// ```
43/// use cbor_core::{Value, array, map};
44///
45/// let a = array![1, 2, 3];
46/// let m = map! { "x" => 10, "y" => 20 };
47/// ```
48///
49/// Arrays and maps can also be built from standard Rust collections.
50/// Slices, `Vec`s, fixed-size arrays, `BTreeMap`s, `HashMap`s, and
51/// slices of key-value pairs all convert automatically:
52///
53/// ```
54/// use cbor_core::Value;
55/// use std::collections::HashMap;
56///
57/// // Array from a slice
58/// let a = Value::array([1, 2, 3].as_slice());
59///
60/// // Map from a HashMap
61/// let mut hm = HashMap::new();
62/// hm.insert(1, 2);
63/// let m = Value::map(&hm);
64///
65/// // Map from key-value pairs
66/// let m = Value::map([("x", 10), ("y", 20)]);
67/// ```
68///
69/// Use `()` to create empty arrays or maps without spelling out a type:
70///
71/// ```
72/// use cbor_core::Value;
73///
74/// let empty_array = Value::array(());
75/// let empty_map = Value::map(());
76///
77/// assert_eq!(empty_array.as_array().unwrap().len(), 0);
78/// assert_eq!(empty_map.as_map().unwrap().len(), 0);
79/// ```
80///
81/// Named constructors are available for cases where `From` is ambiguous:
82///
83/// | Constructor | Builds |
84/// |---|---|
85/// | [`Value::null()`] | Null simple value |
86/// | [`Value::simple_value(v)`](Value::simple_value) | Arbitrary simple value |
87/// | [`Value::float(v)`](Value::float) | Float in shortest CBOR form |
88/// | [`Value::array(v)`](Value::array) | Array from slice, `Vec`, or fixed-size array |
89/// | [`Value::map(v)`](Value::map) | Map from `BTreeMap`, `HashMap`, slice of pairs, etc. |
90/// | [`Value::tag(n, v)`](Value::tag) | Tagged value |
91///
92/// # Encoding and decoding
93///
94/// ```
95/// use cbor_core::Value;
96///
97/// let original = Value::from(-1000);
98/// let bytes = original.encode();
99/// let decoded = Value::decode(&bytes).unwrap();
100/// assert_eq!(original, decoded);
101/// ```
102///
103/// For streaming use, [`write_to`](Value::write_to) and
104/// [`read_from`](Value::read_from) operate on any `io::Write` / `io::Read`.
105///
106/// # Accessors
107///
108/// Accessor methods extract or borrow the inner data of each variant.
109/// All return `Result<T>`, yielding `Err(Error::IncompatibleType)` on a
110/// type mismatch. The naming follows Rust conventions:
111///
112/// | Prefix | Meaning | Returns |
113/// |---|---|---|
114/// | `as_*` | Borrow inner data | `&T` or `&mut T` (with `_mut`) |
115/// | `to_*` | Convert or narrow | Owned `Copy` type (`u8`, `f32`, ...) |
116/// | `into_*` | Consume self, extract | Owned `T` |
117/// | no prefix | Trivial property | `Copy` scalar |
118///
119/// ## Simple values
120///
121/// In CBOR, booleans and null are not distinct types but specific simple
122/// values: `false` is 20, `true` is 21, `null` is 22. This means a
123/// boolean value is always also a simple value. [`to_bool`](Self::to_bool)
124/// provides typed access to `true`/`false`, while
125/// [`to_simple_value`](Self::to_simple_value) works on any simple value
126/// including booleans and null.
127///
128/// | Method | Returns | Notes |
129/// |---|---|---|
130/// | [`to_simple_value`](Self::to_simple_value) | `Result<u8>` | Raw simple value number |
131/// | [`to_bool`](Self::to_bool) | `Result<bool>` | Only for `true`/`false` |
132///
133/// ```
134/// use cbor_core::Value;
135///
136/// let v = Value::from(true);
137/// assert_eq!(v.to_bool().unwrap(), true);
138/// assert_eq!(v.to_simple_value().unwrap(), 21); // CBOR true = simple(21)
139///
140/// // null is also a simple value
141/// let n = Value::null();
142/// assert!(n.to_bool().is_err()); // not a boolean
143/// assert_eq!(n.to_simple_value().unwrap(), 22); // but is simple(22)
144/// ```
145///
146/// ## Integers
147///
148/// CBOR has effectively four integer types (unsigned or negative, and
149/// normal or big integer) with different internal representations.
150/// This is handled transparently by the API.
151///
152/// The `to_*` accessors perform checked
153/// narrowing into any Rust integer type, returning `Err(Overflow)` if
154/// the value does not fit, or `Err(NegativeUnsigned)` when extracting a
155/// negative value into an unsigned type.
156///
157/// | Method | Returns |
158/// |---|---|
159/// | [`to_u8`](Self::to_u8) .. [`to_u128`](Self::to_u128), [`to_usize`](Self::to_usize) | `Result<uN>` |
160/// | [`to_i8`](Self::to_i8) .. [`to_i128`](Self::to_i128), [`to_isize`](Self::to_isize) | `Result<iN>` |
161///
162/// ```
163/// use cbor_core::Value;
164///
165/// let v = Value::from(1000);
166/// assert_eq!(v.to_u32().unwrap(), 1000);
167/// assert_eq!(v.to_i64().unwrap(), 1000);
168/// assert!(v.to_u8().is_err()); // overflow
169///
170/// let neg = Value::from(-5);
171/// assert_eq!(neg.to_i8().unwrap(), -5);
172/// assert!(neg.to_u32().is_err()); // negative unsigned
173/// ```
174///
175/// ## Floats
176///
177/// Floats are stored internally in their shortest CBOR encoding (`f16`,
178/// `f32`, or `f64`). [`to_f64`](Self::to_f64) always succeeds since every
179/// float can widen to `f64`. [`to_f32`](Self::to_f32) fails with
180/// `Err(Precision)` if the value is stored as `f64`.
181/// A float internally stored as `f16` can always be converted to either
182/// an `f32` or `f64` for obvious reasons.
183///
184/// | Method | Returns |
185/// |---|---|
186/// | [`to_f32`](Self::to_f32) | `Result<f32>` (fails for f64 values) |
187/// | [`to_f64`](Self::to_f64) | `Result<f64>` |
188///
189/// ```
190/// use cbor_core::Value;
191///
192/// let v = Value::from(2.5);
193/// assert_eq!(v.to_f64().unwrap(), 2.5);
194/// assert_eq!(v.to_f32().unwrap(), 2.5);
195/// ```
196///
197/// ## Byte strings
198///
199/// Byte strings are stored as `Vec<u8>`. Use [`as_bytes`](Self::as_bytes)
200/// for a borrowed slice, or [`into_bytes`](Self::into_bytes) to take
201/// ownership without copying.
202///
203/// | Method | Returns |
204/// |---|---|
205/// | [`as_bytes`](Self::as_bytes) | `Result<&[u8]>` |
206/// | [`as_bytes_mut`](Self::as_bytes_mut) | `Result<&mut Vec<u8>>` |
207/// | [`into_bytes`](Self::into_bytes) | `Result<Vec<u8>>` |
208///
209/// ```
210/// use cbor_core::Value;
211///
212/// let mut v = Value::from(vec![1, 2, 3]);
213/// v.as_bytes_mut().unwrap().push(4);
214/// assert_eq!(v.as_bytes().unwrap(), &[1, 2, 3, 4]);
215/// ```
216///
217/// ## Text strings
218///
219/// Text strings are stored as `String` (guaranteed valid UTF-8 by the
220/// decoder). Use [`as_str`](Self::as_str) for a borrowed `&str`, or
221/// [`into_string`](Self::into_string) to take ownership.
222///
223/// | Method | Returns |
224/// |---|---|
225/// | [`as_str`](Self::as_str) | `Result<&str>` |
226/// | [`as_string_mut`](Self::as_string_mut) | `Result<&mut String>` |
227/// | [`into_string`](Self::into_string) | `Result<String>` |
228///
229/// ```
230/// use cbor_core::Value;
231///
232/// let v = Value::from("hello");
233/// assert_eq!(v.as_str().unwrap(), "hello");
234///
235/// // Modify in place
236/// let mut v = Value::from("hello");
237/// v.as_string_mut().unwrap().push_str(" world");
238/// assert_eq!(v.as_str().unwrap(), "hello world");
239/// ```
240///
241/// ## Arrays
242///
243/// Arrays are stored as `Vec<Value>`. Use [`as_array`](Self::as_array)
244/// to borrow the elements as a slice, or [`as_array_mut`](Self::as_array_mut)
245/// to modify them in place.
246///
247/// | Method | Returns |
248/// |---|---|
249/// | [`as_array`](Self::as_array) | `Result<&[Value]>` |
250/// | [`as_array_mut`](Self::as_array_mut) | `Result<&mut Vec<Value>>` |
251/// | [`into_array`](Self::into_array) | `Result<Vec<Value>>` |
252///
253/// ```
254/// use cbor_core::{Value, array};
255///
256/// let v = array![10, 20, 30];
257/// let items = v.as_array().unwrap();
258/// assert_eq!(items[1].to_u32().unwrap(), 20);
259///
260/// // Modify in place
261/// let mut v = array![1, 2];
262/// v.as_array_mut().unwrap().push(3.into());
263/// assert_eq!(v.as_array().unwrap().len(), 3);
264/// ```
265///
266/// ## Maps
267///
268/// Maps are stored as `BTreeMap<Value, Value>`, giving canonical key
269/// order. Use standard `BTreeMap` methods on the result of
270/// [`as_map`](Self::as_map) to look up entries.
271///
272/// | Method | Returns |
273/// |---|---|
274/// | [`as_map`](Self::as_map) | `Result<&BTreeMap<Value, Value>>` |
275/// | [`as_map_mut`](Self::as_map_mut) | `Result<&mut BTreeMap<Value, Value>>` |
276/// | [`into_map`](Self::into_map) | `Result<BTreeMap<Value, Value>>` |
277///
278/// ```
279/// use cbor_core::{Value, map};
280///
281/// let v = map! { "name" => "Alice", "age" => 30 };
282/// assert_eq!(v["name"].as_str().unwrap(), "Alice");
283///
284/// // Modify in place
285/// let mut v = map! { "count" => 1 };
286/// v.as_map_mut().unwrap().insert("count".into(), 2.into());
287/// assert_eq!(v["count"].to_u32().unwrap(), 2);
288/// ```
289///
290/// ## Indexing
291///
292/// Arrays and maps support `Index` and `IndexMut` with any type that
293/// converts into `Value`. For arrays the index is converted to `usize`;
294/// for maps it is used as a key lookup. Panics on type mismatch or
295/// missing key, just like `Vec` and `BTreeMap`.
296///
297/// ```
298/// use cbor_core::{Value, array, map};
299///
300/// let a = array![10, 20, 30];
301/// assert_eq!(a[1].to_u32().unwrap(), 20);
302///
303/// let m = map! { "x" => 10, "y" => 20 };
304/// assert_eq!(m["x"].to_u32().unwrap(), 10);
305/// ```
306///
307/// ## Tags
308///
309/// A tag wraps another value with a numeric label (e.g. tag 1 for epoch
310/// timestamps, tag 32 for URIs). Tags can be nested.
311///
312/// | Method | Returns | Notes |
313/// |---|---|---|
314/// | [`tag_number`](Self::tag_number) | `Result<u64>` | Tag number |
315/// | [`tag_content`](Self::tag_content) | `Result<&Value>` | Borrowed content |
316/// | [`tag_content_mut`](Self::tag_content_mut) | `Result<&mut Value>` | Mutable content |
317/// | [`as_tag`](Self::as_tag) | `Result<(u64, &Value)>` | Both parts |
318/// | [`as_tag_mut`](Self::as_tag_mut) | `Result<(u64, &mut Value)>` | Mutable content |
319/// | [`into_tag`](Self::into_tag) | `Result<(u64, Value)>` | Consuming |
320///
321/// Use [`untagged`](Self::untagged) to look through tags without removing
322/// them, [`remove_tag`](Self::remove_tag) to strip the outermost tag, or
323/// [`remove_all_tags`](Self::remove_all_tags) to strip all layers at once.
324///
325/// ```
326/// use cbor_core::Value;
327///
328/// // Create a tagged value (tag 32 = URI)
329/// let mut uri = Value::tag(32, "https://example.com");
330///
331/// // Inspect
332/// let (tag_num, content) = uri.as_tag().unwrap();
333/// assert_eq!(tag_num, 32);
334/// assert_eq!(content.as_str().unwrap(), "https://example.com");
335///
336/// // Look through tags without removing them
337/// assert_eq!(uri.untagged().as_str().unwrap(), "https://example.com");
338///
339/// // Strip the tag in place
340/// let removed = uri.remove_tag();
341/// assert_eq!(removed, Some(32));
342/// assert_eq!(uri.as_str().unwrap(), "https://example.com");
343/// ```
344///
345/// Accessor methods see through tags transparently: calling `as_str()`
346/// on a tagged text string works without manually unwrapping the tag
347/// first. This applies to all accessors (`to_*`, `as_*`, `into_*`).
348///
349/// ```
350/// use cbor_core::Value;
351///
352/// let uri = Value::tag(32, "https://example.com");
353/// assert_eq!(uri.as_str().unwrap(), "https://example.com");
354///
355/// // Nested tags are also transparent
356/// let nested = Value::tag(100, Value::tag(200, 42));
357/// assert_eq!(nested.to_u32().unwrap(), 42);
358/// ```
359///
360/// Big integers are internally represented as tagged byte strings
361/// (tags 2 and 3). The integer accessors recognise these tags and
362/// decode the bytes automatically, even when wrapped in additional
363/// custom tags. Byte-level accessors like `as_bytes()` also see
364/// through tags, so calling `as_bytes()` on a big integer returns
365/// the raw payload bytes.
366///
367/// If a tag is removed via `remove_tag`, `remove_all_tags`, or by
368/// consuming through `into_tag`, the value becomes a plain byte
369/// string and can no longer be read as an integer.
370///
371/// # Type introspection
372///
373/// [`data_type`](Self::data_type) returns a [`DataType`] enum for
374/// lightweight type checks without matching on the full enum.
375///
376/// ```
377/// use cbor_core::Value;
378///
379/// let v = Value::from(3.14);
380/// assert!(v.data_type().is_float());
381/// ```
382#[derive(Debug, Clone)]
383pub enum Value {
384 /// Simple value such as `null`, `true`, or `false` (major type 7).
385 ///
386 /// In CBOR, booleans and null are simple values, not distinct types.
387 /// A `Value::from(true)` is stored as `SimpleValue(21)` and is
388 /// accessible through both [`to_bool`](Self::to_bool) and
389 /// [`to_simple_value`](Self::to_simple_value).
390 SimpleValue(SimpleValue),
391
392 /// Unsigned integer (major type 0). Stores values 0 through 2^64-1.
393 Unsigned(u64),
394
395 /// Negative integer (major type 1). The actual value is -1 - n,
396 /// covering -1 through -2^64.
397 Negative(u64),
398
399 /// IEEE 754 floating-point number (major type 7, additional info 25-27).
400 Float(Float),
401
402 /// Byte string (major type 2).
403 ByteString(Vec<u8>),
404
405 /// UTF-8 text string (major type 3).
406 TextString(String),
407
408 /// Array of data items (major type 4).
409 Array(Vec<Value>),
410
411 /// Map of key-value pairs in canonical order (major type 5).
412 Map(BTreeMap<Value, Value>),
413
414 /// Tagged data item (major type 6). The first field is the tag number,
415 /// the second is the enclosed content.
416 Tag(u64, Box<Value>),
417}
418
419impl Value {
420 /// Take the value out, leaving `null` in its place.
421 ///
422 /// ```
423 /// use cbor_core::Value;
424 ///
425 /// let mut v = Value::from(42);
426 /// let taken = v.take();
427 /// assert_eq!(taken.to_u32().unwrap(), 42);
428 /// assert!(v.data_type().is_null());
429 /// ```
430 pub fn take(&mut self) -> Self {
431 std::mem::take(self)
432 }
433
434 /// Replace the value, returning the old one.
435 ///
436 /// ```
437 /// use cbor_core::Value;
438 ///
439 /// let mut v = Value::from("hello");
440 /// let old = v.replace(Value::from("world"));
441 /// assert_eq!(old.as_str().unwrap(), "hello");
442 /// assert_eq!(v.as_str().unwrap(), "world");
443 /// ```
444 pub fn replace(&mut self, value: Self) -> Self {
445 std::mem::replace(self, value)
446 }
447
448 /// Encode this value to binary CBOR bytes.
449 ///
450 /// This is a convenience wrapper around [`write_to`](Self::write_to).
451 ///
452 /// ```
453 /// use cbor_core::Value;
454 /// let bytes = Value::from(42).encode();
455 /// assert_eq!(bytes, [0x18, 42]);
456 /// ```
457 #[must_use]
458 pub fn encode(&self) -> Vec<u8> {
459 let len = self.cbor_len();
460 let mut bytes = Vec::with_capacity(len);
461 self.write_to(&mut bytes).unwrap();
462 debug_assert_eq!(bytes.len(), len);
463 bytes
464 }
465
466 /// Encode this value to a hex-encoded CBOR string.
467 ///
468 /// This is a convenience wrapper around [`write_hex_to`](Self::write_hex_to).
469 ///
470 /// ```
471 /// use cbor_core::Value;
472 /// let hex = Value::from(42).encode_hex();
473 /// assert_eq!(hex, "182a");
474 /// ```
475 #[must_use]
476 pub fn encode_hex(&self) -> String {
477 let len2 = self.cbor_len() * 2;
478 let mut hex = Vec::with_capacity(len2);
479 self.write_hex_to(&mut hex).unwrap();
480 debug_assert_eq!(hex.len(), len2);
481 String::from_utf8(hex).unwrap()
482 }
483
484 /// Decode a CBOR data item from binary bytes.
485 ///
486 /// Accepts any byte source (`&[u8]`, `&str`, `String`, `Vec<u8>`, etc.).
487 /// Returns `Err` if the encoding is not canonical.
488 ///
489 /// This is a convenience wrapper around [`read_from`](Self::read_from).
490 ///
491 /// ```
492 /// use cbor_core::Value;
493 /// let v = Value::decode(&[0x18, 42]).unwrap();
494 /// assert_eq!(v.to_u32().unwrap(), 42);
495 /// ```
496 pub fn decode(bytes: impl AsRef<[u8]>) -> Result<Self> {
497 let mut bytes = bytes.as_ref();
498 Self::read_from(&mut bytes)
499 }
500
501 /// Decode a CBOR data item from hex-encoded bytes.
502 ///
503 /// Accepts any byte source (`&[u8]`, `&str`, `String`, `Vec<u8>`, etc.).
504 /// Both uppercase and lowercase hex digits are accepted.
505 /// Returns `Err` if the encoding is not canonical.
506 ///
507 /// This is a convenience wrapper around [`read_hex_from`](Self::read_hex_from).
508 ///
509 /// ```
510 /// use cbor_core::Value;
511 /// let v = Value::decode_hex("182a").unwrap();
512 /// assert_eq!(v.to_u32().unwrap(), 42);
513 /// ```
514 pub fn decode_hex(hex: impl AsRef<[u8]>) -> Result<Self> {
515 let mut bytes = hex.as_ref();
516 Self::read_hex_from(&mut bytes)
517 }
518
519 /// Read a single CBOR data item from a binary stream.
520 ///
521 /// ```
522 /// use cbor_core::Value;
523 /// let mut bytes: &[u8] = &[0x18, 42];
524 /// let v = Value::read_from(&mut bytes).unwrap();
525 /// assert_eq!(v.to_u32().unwrap(), 42);
526 /// ```
527 pub fn read_from(reader: &mut impl io::Read) -> Result<Self> {
528 let ctrl_byte = {
529 let mut buf = [0];
530 reader.read_exact(&mut buf)?;
531 buf[0]
532 };
533
534 let is_float = matches!(ctrl_byte, CtrlByte::F16 | CtrlByte::F32 | CtrlByte::F64);
535
536 let major = ctrl_byte >> 5;
537 let info = ctrl_byte & 0x1f;
538
539 let argument = {
540 let mut buf = [0; 8];
541
542 if info < ArgLength::U8 {
543 buf[7] = info;
544 } else {
545 match info {
546 ArgLength::U8 => reader.read_exact(&mut buf[7..])?,
547 ArgLength::U16 => reader.read_exact(&mut buf[6..])?,
548 ArgLength::U32 => reader.read_exact(&mut buf[4..])?,
549 ArgLength::U64 => reader.read_exact(&mut buf)?,
550 _ => return Err(Error::InvalidEncoding),
551 }
552 }
553
554 u64::from_be_bytes(buf)
555 };
556
557 if !is_float {
558 let non_deterministic = match info {
559 ArgLength::U8 => argument < ArgLength::U8.into(),
560 ArgLength::U16 => argument <= u8::MAX.into(),
561 ArgLength::U32 => argument <= u16::MAX.into(),
562 ArgLength::U64 => argument <= u32::MAX.into(),
563 _ => false,
564 };
565
566 if non_deterministic {
567 return Err(Error::InvalidEncoding);
568 }
569 }
570
571 let this = match major {
572 Major::UNSIGNED => Self::Unsigned(argument),
573 Major::NEGATIVE => Self::Negative(argument),
574
575 Major::BYTE_STRING => Self::ByteString(read_vec(reader, argument)?),
576
577 Major::TEXT_STRING => {
578 let bytes = read_vec(reader, argument)?;
579 let string = String::from_utf8(bytes).map_err(|_| Error::InvalidUtf8)?;
580 Self::TextString(string)
581 }
582
583 Major::ARRAY => {
584 let mut vec = Vec::with_capacity(argument.try_into().unwrap());
585 for _ in 0..argument {
586 vec.push(Self::read_from(reader)?);
587 }
588 Self::Array(vec)
589 }
590
591 Major::MAP => {
592 let mut map = BTreeMap::new();
593 let mut prev = None;
594
595 for _ in 0..argument {
596 let key = Self::read_from(reader)?;
597 let value = Self::read_from(reader)?;
598
599 if let Some((prev_key, prev_value)) = prev.take() {
600 if prev_key >= key {
601 return Err(Error::InvalidEncoding);
602 }
603 map.insert(prev_key, prev_value);
604 }
605
606 prev = Some((key, value));
607 }
608
609 if let Some((key, value)) = prev.take() {
610 map.insert(key, value);
611 }
612
613 Self::Map(map)
614 }
615
616 Major::TAG => {
617 let content = Box::new(Self::read_from(reader)?);
618
619 // check if conforming bigint
620 if matches!(argument, Tag::POS_BIG_INT | Tag::NEG_BIG_INT)
621 && let Ok(bigint) = content.as_bytes()
622 {
623 let valid = bigint.len() >= 8 && bigint[0] != 0;
624 if !valid {
625 return Err(Error::InvalidEncoding);
626 }
627 }
628
629 Self::Tag(argument, content)
630 }
631
632 Major::SIMPLE_VALUE => match info {
633 0..=ArgLength::U8 => SimpleValue::from_u8(argument as u8)
634 .map(Self::SimpleValue)
635 .map_err(|_| Error::InvalidEncoding)?,
636
637 ArgLength::U16 => Self::Float(Float::from_u16(argument as u16)),
638 ArgLength::U32 => Self::Float(Float::from_u32(argument as u32)?),
639 ArgLength::U64 => Self::Float(Float::from_u64(argument)?),
640
641 _ => return Err(Error::InvalidEncoding),
642 },
643
644 _ => unreachable!(),
645 };
646
647 Ok(this)
648 }
649
650 /// Read a single CBOR data item from a hex-encoded stream.
651 ///
652 /// Each byte of CBOR is expected as two hex digits (uppercase or
653 /// lowercase).
654 ///
655 /// ```
656 /// use cbor_core::Value;
657 /// let mut hex = "182a".as_bytes();
658 /// let v = Value::read_hex_from(&mut hex).unwrap();
659 /// assert_eq!(v.to_u32().unwrap(), 42);
660 /// ```
661 pub fn read_hex_from(reader: impl io::Read) -> Result<Self> {
662 struct HexReader<R>(R);
663
664 impl<R: io::Read> io::Read for HexReader<R> {
665 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
666 fn nibble(char: u8) -> io::Result<u8> {
667 match char {
668 b'0'..=b'9' => Ok(char - b'0'),
669 b'a'..=b'f' => Ok(char - b'a' + 10),
670 b'A'..=b'F' => Ok(char - b'A' + 10),
671 _ => Err(io::ErrorKind::InvalidData.into()),
672 }
673 }
674
675 for byte in buf.iter_mut() {
676 let mut hex = [0; 2];
677 self.0.read_exact(&mut hex)?;
678 *byte = nibble(hex[0])? << 4 | nibble(hex[1])?;
679 }
680
681 Ok(buf.len())
682 }
683 }
684
685 Self::read_from(&mut HexReader(reader))
686 }
687
688 /// Write this value as binary CBOR to a stream.
689 ///
690 /// ```
691 /// use cbor_core::Value;
692 /// let mut buf = Vec::new();
693 /// Value::from(42).write_to(&mut buf).unwrap();
694 /// assert_eq!(buf, [0x18, 42]);
695 /// ```
696 pub fn write_to(&self, writer: &mut impl io::Write) -> Result<()> {
697 let major = self.cbor_major();
698 let (info, argument) = self.cbor_argument();
699
700 let ctrl_byte = major << 5 | info;
701 writer.write_all(&[ctrl_byte])?;
702
703 let buf = argument.to_be_bytes();
704 match info {
705 ArgLength::U8 => writer.write_all(&buf[7..])?,
706 ArgLength::U16 => writer.write_all(&buf[6..])?,
707 ArgLength::U32 => writer.write_all(&buf[4..])?,
708 ArgLength::U64 => writer.write_all(&buf)?,
709 _ => (), // argument embedded in ctrl byte
710 }
711
712 match self {
713 Value::ByteString(bytes) => writer.write_all(bytes)?,
714 Value::TextString(string) => writer.write_all(string.as_bytes())?,
715
716 Value::Tag(_number, content) => content.write_to(writer)?,
717
718 Value::Array(values) => {
719 for value in values {
720 value.write_to(writer)?;
721 }
722 }
723
724 Value::Map(map) => {
725 for (key, value) in map {
726 key.write_to(writer)?;
727 value.write_to(writer)?;
728 }
729 }
730
731 _ => (),
732 }
733
734 Ok(())
735 }
736
737 /// Write this value as hex-encoded CBOR to a stream.
738 ///
739 /// Each binary byte is written as two lowercase hex digits. The
740 /// adapter encodes on the fly without buffering the full output.
741 ///
742 /// ```
743 /// use cbor_core::Value;
744 /// let mut buf = Vec::new();
745 /// Value::from(42).write_hex_to(&mut buf).unwrap();
746 /// assert_eq!(buf, b"182a");
747 /// ```
748 pub fn write_hex_to(&self, writer: impl io::Write) -> Result<()> {
749 struct HexWriter<W>(W);
750
751 impl<W: io::Write> io::Write for HexWriter<W> {
752 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
753 for &byte in buf {
754 write!(self.0, "{byte:02x}")?;
755 }
756 Ok(buf.len())
757 }
758 fn flush(&mut self) -> io::Result<()> {
759 Ok(())
760 }
761 }
762
763 self.write_to(&mut HexWriter(writer))
764 }
765
766 fn cbor_major(&self) -> u8 {
767 match self {
768 Value::Unsigned(_) => Major::UNSIGNED,
769 Value::Negative(_) => Major::NEGATIVE,
770 Value::ByteString(_) => Major::BYTE_STRING,
771 Value::TextString(_) => Major::TEXT_STRING,
772 Value::Array(_) => Major::ARRAY,
773 Value::Map(_) => Major::MAP,
774 Value::Tag(_, _) => Major::TAG,
775 Value::SimpleValue(_) => Major::SIMPLE_VALUE,
776 Value::Float(_) => Major::SIMPLE_VALUE,
777 }
778 }
779
780 fn cbor_argument(&self) -> (u8, u64) {
781 fn arg(value: u64) -> (u8, u64) {
782 if value < ArgLength::U8.into() {
783 (value as u8, value)
784 } else {
785 let info = match value {
786 0x00..=0xFF => ArgLength::U8,
787 0x100..=0xFFFF => ArgLength::U16,
788 0x10000..=0xFFFF_FFFF => ArgLength::U32,
789 _ => ArgLength::U64,
790 };
791 (info, value)
792 }
793 }
794
795 match self {
796 Value::Unsigned(value) => arg(*value),
797 Value::Negative(value) => arg(*value),
798 Value::ByteString(vec) => arg(vec.len().try_into().unwrap()),
799 Value::TextString(str) => arg(str.len().try_into().unwrap()),
800 Value::Array(vec) => arg(vec.len().try_into().unwrap()),
801 Value::Map(map) => arg(map.len().try_into().unwrap()),
802 Value::Tag(number, _) => arg(*number),
803 Value::SimpleValue(value) => arg(value.0.into()),
804 Value::Float(float) => float.cbor_argument(),
805 }
806 }
807
808 /// Encoded length
809 fn cbor_len(&self) -> usize {
810 let (info, _) = self.cbor_argument();
811
812 let header_len = match info {
813 0..ArgLength::U8 => 1,
814 ArgLength::U8 => 2,
815 ArgLength::U16 => 3,
816 ArgLength::U32 => 5,
817 ArgLength::U64 => 9,
818 _ => unreachable!(),
819 };
820
821 let data_len = match self {
822 Self::ByteString(bytes) => bytes.len(),
823 Self::TextString(text) => text.len(),
824 Self::Array(vec) => vec.iter().map(Self::cbor_len).sum(),
825 Self::Map(map) => map.iter().map(|(k, v)| k.cbor_len() + v.cbor_len()).sum(),
826 Self::Tag(_, content) => content.cbor_len(),
827 _ => 0,
828 };
829
830 header_len + data_len
831 }
832
833 // ------------------- constructors -------------------
834
835 /// Create a CBOR null value.
836 #[must_use]
837 pub const fn null() -> Self {
838 Self::SimpleValue(SimpleValue::NULL)
839 }
840
841 /// Create a CBOR simple value.
842 ///
843 /// # Panics
844 ///
845 /// Panics if the value is in the reserved range 24-31.
846 /// Use [`SimpleValue::from_u8`] for a fallible alternative.
847 pub fn simple_value(value: impl TryInto<SimpleValue>) -> Self {
848 match value.try_into() {
849 Ok(sv) => Self::SimpleValue(sv),
850 Err(_) => panic!("Invalid simple value"),
851 }
852 }
853
854 /// Create a CBOR date/time string value (tag 0).
855 ///
856 /// Accepts `&str`, `String`, and [`SystemTime`] via the
857 /// [`DateTime`] helper. The date must be within
858 /// `0001-01-01T00:00:00Z` to `9999-12-31T23:59:59Z`.
859 ///
860 /// # Panics
861 ///
862 /// Panics if the input is not a valid RFC 3339 (ISO 8601 profile)
863 /// UTC timestamp or is out of range.
864 ///
865 /// ```
866 /// use cbor_core::{DataType, Value};
867 ///
868 /// let v = Value::date_time("2000-01-01T00:00:00Z");
869 /// assert_eq!(v.data_type(), DataType::DateTime);
870 /// assert_eq!(v.as_str(), Ok("2000-01-01T00:00:00Z"));
871 /// ```
872 pub fn date_time(value: impl TryInto<DateTime>) -> Self {
873 match value.try_into() {
874 Ok(dt) => dt.into(),
875 Err(_) => panic!("Invalid date/time"),
876 }
877 }
878
879 /// Create a CBOR epoch time value (tag 1).
880 ///
881 /// Accepts integers, floats, and [`SystemTime`] via the
882 /// [`EpochTime`] helper. The value must be in the range 0 to
883 /// 253402300799.
884 ///
885 /// # Panics
886 ///
887 /// Panics if the value is out of range or negative.
888 ///
889 /// ```
890 /// use std::time::{Duration, UNIX_EPOCH};
891 /// use cbor_core::Value;
892 ///
893 /// let v = Value::epoch_time(1_000_000);
894 /// assert_eq!(v.to_system_time(), Ok(UNIX_EPOCH + Duration::from_secs(1_000_000)));
895 /// ```
896 pub fn epoch_time(value: impl TryInto<EpochTime>) -> Self {
897 match value.try_into() {
898 Ok(et) => et.into(),
899 Err(_) => panic!("Invalid epoch time"),
900 }
901 }
902
903 /// Create a CBOR float.
904 ///
905 /// Via the [`Float`] type floats can be created out of integers and booleans too.
906 ///
907 /// ```
908 /// use cbor_core::Value;
909 ///
910 /// let f1 = Value::float(1.0);
911 /// assert!(f1.to_f64() == Ok(1.0));
912 ///
913 /// let f2 = Value::float(2);
914 /// assert!(f2.to_f64() == Ok(2.0));
915 ///
916 /// let f3 = Value::float(true);
917 /// assert!(f3.to_f64() == Ok(1.0));
918 /// ```
919 ///
920 /// The value is stored in the shortest IEEE 754 form (f16, f32,
921 /// or f64) that preserves it exactly.
922 pub fn float(value: impl Into<Float>) -> Self {
923 Self::Float(value.into())
924 }
925
926 /// Create a CBOR array from a `Vec`, slice, or fixed-size array.
927 pub fn array(array: impl Into<Array>) -> Self {
928 Self::Array(array.into().0)
929 }
930
931 /// Create a CBOR map. Keys are stored in canonical order.
932 pub fn map(map: impl Into<Map>) -> Self {
933 Self::Map(map.into().0)
934 }
935
936 /// Wrap a value with a CBOR tag.
937 ///
938 /// ```
939 /// use cbor_core::Value;
940 /// let uri = Value::tag(32, "https://example.com");
941 /// assert_eq!(uri.tag_number().unwrap(), 32);
942 /// ```
943 pub fn tag(number: u64, content: impl Into<Value>) -> Self {
944 Self::Tag(number, Box::new(content.into()))
945 }
946
947 /// Return the [`DataType`] of this value for type-level dispatch.
948 #[must_use]
949 pub const fn data_type(&self) -> DataType {
950 match self {
951 Self::SimpleValue(sv) => sv.data_type(),
952
953 Self::Unsigned(_) | Self::Negative(_) => DataType::Int,
954
955 Self::Float(float) => float.data_type(),
956
957 Self::TextString(_) => DataType::Text,
958 Self::ByteString(_) => DataType::Bytes,
959
960 Self::Array(_) => DataType::Array,
961 Self::Map(_) => DataType::Map,
962
963 Self::Tag(Tag::DATE_TIME, content) if content.data_type().is_text() => DataType::DateTime,
964 Self::Tag(Tag::EPOCH_TIME, content) if content.data_type().is_numeric() => DataType::EpochTime,
965
966 Self::Tag(Tag::POS_BIG_INT | Tag::NEG_BIG_INT, content) if content.data_type().is_bytes() => {
967 DataType::BigInt
968 }
969
970 Self::Tag(_, _) => DataType::Tag,
971 }
972 }
973
974 /// Internal shortcut helper
975 pub(crate) const fn is_bytes(&self) -> bool {
976 self.data_type().is_bytes()
977 }
978
979 /// Extract a boolean. Returns `Err` for non-boolean values.
980 pub const fn to_bool(&self) -> Result<bool> {
981 match self {
982 Self::SimpleValue(sv) => sv.to_bool(),
983 Self::Tag(_number, content) => content.untagged().to_bool(),
984 _ => Err(Error::IncompatibleType),
985 }
986 }
987
988 /// Extract the raw simple value number (0-255, excluding 24-31).
989 pub const fn to_simple_value(&self) -> Result<u8> {
990 match self {
991 Self::SimpleValue(sv) => Ok(sv.0),
992 Self::Tag(_number, content) => content.untagged().to_simple_value(),
993 _ => Err(Error::IncompatibleType),
994 }
995 }
996
997 fn to_uint<T>(&self) -> Result<T>
998 where
999 T: TryFrom<u64> + TryFrom<u128>,
1000 {
1001 match self {
1002 Self::Unsigned(x) => T::try_from(*x).or(Err(Error::Overflow)),
1003 Self::Negative(_) => Err(Error::NegativeUnsigned),
1004
1005 Self::Tag(Tag::POS_BIG_INT, content) if content.is_bytes() => {
1006 T::try_from(u128_from_bytes(self.as_bytes()?)?).or(Err(Error::Overflow))
1007 }
1008
1009 Self::Tag(Tag::NEG_BIG_INT, content) if content.is_bytes() => Err(Error::NegativeUnsigned),
1010 Self::Tag(_other_number, content) => content.peeled().to_uint(),
1011 _ => Err(Error::IncompatibleType),
1012 }
1013 }
1014
1015 /// Narrow to `u8`. Returns `Err(Overflow)` or `Err(NegativeUnsigned)` on mismatch.
1016 pub fn to_u8(&self) -> Result<u8> {
1017 self.to_uint()
1018 }
1019
1020 /// Narrow to `u16`.
1021 pub fn to_u16(&self) -> Result<u16> {
1022 self.to_uint()
1023 }
1024
1025 /// Narrow to `u32`.
1026 pub fn to_u32(&self) -> Result<u32> {
1027 self.to_uint()
1028 }
1029
1030 /// Narrow to `u64`.
1031 pub fn to_u64(&self) -> Result<u64> {
1032 self.to_uint()
1033 }
1034
1035 /// Narrow to `u128`. Handles big integers (tag 2) transparently.
1036 pub fn to_u128(&self) -> Result<u128> {
1037 self.to_uint()
1038 }
1039
1040 /// Narrow to `usize`.
1041 pub fn to_usize(&self) -> Result<usize> {
1042 self.to_uint()
1043 }
1044
1045 #[allow(dead_code)]
1046 pub(crate) fn as_integer_bytes(&self) -> Result<IntegerBytes<'_>> {
1047 match self {
1048 Self::Unsigned(x) => Ok(IntegerBytes::UnsignedOwned(x.to_be_bytes())),
1049 Self::Negative(x) => Ok(IntegerBytes::NegativeOwned(x.to_be_bytes())),
1050
1051 Self::Tag(Tag::POS_BIG_INT, content) if content.is_bytes() => {
1052 Ok(IntegerBytes::UnsignedBorrowed(content.as_bytes()?))
1053 }
1054
1055 Self::Tag(Tag::NEG_BIG_INT, content) if content.is_bytes() => {
1056 Ok(IntegerBytes::NegativeBorrowed(content.as_bytes()?))
1057 }
1058
1059 Self::Tag(_other_number, content) => content.peeled().as_integer_bytes(),
1060 _ => Err(Error::IncompatibleType),
1061 }
1062 }
1063
1064 fn to_sint<T>(&self) -> Result<T>
1065 where
1066 T: TryFrom<u64> + TryFrom<u128> + std::ops::Not<Output = T>,
1067 {
1068 match self {
1069 Self::Unsigned(x) => T::try_from(*x).or(Err(Error::Overflow)),
1070 Self::Negative(x) => T::try_from(*x).map(T::not).or(Err(Error::Overflow)),
1071
1072 Self::Tag(Tag::POS_BIG_INT, content) if content.is_bytes() => {
1073 T::try_from(u128_from_bytes(self.as_bytes()?)?).or(Err(Error::Overflow))
1074 }
1075
1076 Self::Tag(Tag::NEG_BIG_INT, content) if content.is_bytes() => {
1077 T::try_from(u128_from_bytes(self.as_bytes()?)?)
1078 .map(T::not)
1079 .or(Err(Error::Overflow))
1080 }
1081
1082 Self::Tag(_other_number, content) => content.peeled().to_sint(),
1083 _ => Err(Error::IncompatibleType),
1084 }
1085 }
1086
1087 /// Narrow to `i8`.
1088 pub fn to_i8(&self) -> Result<i8> {
1089 self.to_sint()
1090 }
1091
1092 /// Narrow to `i16`.
1093 pub fn to_i16(&self) -> Result<i16> {
1094 self.to_sint()
1095 }
1096
1097 /// Narrow to `i32`.
1098 pub fn to_i32(&self) -> Result<i32> {
1099 self.to_sint()
1100 }
1101
1102 /// Narrow to `i64`.
1103 pub fn to_i64(&self) -> Result<i64> {
1104 self.to_sint()
1105 }
1106
1107 /// Narrow to `i128`. Handles big integers (tags 2 and 3) transparently.
1108 pub fn to_i128(&self) -> Result<i128> {
1109 self.to_sint()
1110 }
1111
1112 /// Narrow to `isize`.
1113 pub fn to_isize(&self) -> Result<isize> {
1114 self.to_sint()
1115 }
1116
1117 /// Convert to `f32`. Returns `Err(Precision)` for f64-width values.
1118 pub fn to_f32(&self) -> Result<f32> {
1119 match self {
1120 Self::Float(float) => float.to_f32(),
1121 Self::Tag(_number, content) => content.untagged().to_f32(),
1122 _ => Err(Error::IncompatibleType),
1123 }
1124 }
1125
1126 /// Convert to `f64`. Always succeeds for float values.
1127 pub fn to_f64(&self) -> Result<f64> {
1128 match self {
1129 Self::Float(float) => Ok(float.to_f64()),
1130 Self::Tag(_number, content) => content.untagged().to_f64(),
1131 _ => Err(Error::IncompatibleType),
1132 }
1133 }
1134
1135 /// Convert a time value to [`SystemTime`].
1136 ///
1137 /// Accepts date/time strings (tag 0), epoch time values (tag 1),
1138 /// and untagged integers or floats. Numeric values must be
1139 /// non-negative and in the range 0 to 253402300799. Date/time
1140 /// strings may include a timezone offset, which is converted to
1141 /// UTC.
1142 ///
1143 /// Returns `Err(IncompatibleType)` for values that are neither
1144 /// numeric nor text, `Err(Overflow)` if a numeric value is out of
1145 /// range, and `Err(InvalidEncoding)` if a text string is not a
1146 /// valid RFC 3339 timestamp. Leap seconds (`:60`) are rejected
1147 /// because [`SystemTime`] cannot represent them.
1148 ///
1149 /// ```
1150 /// use std::time::{Duration, UNIX_EPOCH};
1151 /// use cbor_core::Value;
1152 ///
1153 /// let v = Value::tag(1, 1_000_000);
1154 /// let t = v.to_system_time().unwrap();
1155 /// assert_eq!(t, UNIX_EPOCH + Duration::from_secs(1_000_000));
1156 /// ```
1157 pub fn to_system_time(&self) -> Result<SystemTime> {
1158 if let Ok(s) = self.as_str() {
1159 use crate::iso3339::Timestamp;
1160 let ts: Timestamp = s.parse().or(Err(Error::InvalidEncoding))?;
1161 Ok(ts.try_into().or(Err(Error::InvalidEncoding))?)
1162 } else if let Ok(f) = self.to_f64() {
1163 if f.is_finite() && (0.0..=253402300799.0).contains(&f) {
1164 Ok(SystemTime::UNIX_EPOCH + Duration::from_secs_f64(f))
1165 } else {
1166 Err(Error::Overflow)
1167 }
1168 } else {
1169 match self.to_u64() {
1170 Ok(secs) if secs <= 253402300799 => Ok(SystemTime::UNIX_EPOCH + Duration::from_secs(secs)),
1171 Ok(_) | Err(Error::NegativeUnsigned) => Err(Error::Overflow),
1172 Err(error) => Err(error),
1173 }
1174 }
1175 }
1176
1177 /// Borrow the byte string as a slice.
1178 pub fn as_bytes(&self) -> Result<&[u8]> {
1179 match self {
1180 Self::ByteString(vec) => Ok(vec.as_slice()),
1181 Self::Tag(_number, content) => content.untagged().as_bytes(),
1182 _ => Err(Error::IncompatibleType),
1183 }
1184 }
1185
1186 /// Borrow the byte string as a mutable `Vec`.
1187 pub const fn as_bytes_mut(&mut self) -> Result<&mut Vec<u8>> {
1188 match self {
1189 Self::ByteString(vec) => Ok(vec),
1190 Self::Tag(_number, content) => content.untagged_mut().as_bytes_mut(),
1191 _ => Err(Error::IncompatibleType),
1192 }
1193 }
1194
1195 /// Take ownership of the byte string.
1196 pub fn into_bytes(self) -> Result<Vec<u8>> {
1197 match self {
1198 Self::ByteString(vec) => Ok(vec),
1199 Self::Tag(_number, content) => content.into_untagged().into_bytes(),
1200 _ => Err(Error::IncompatibleType),
1201 }
1202 }
1203
1204 /// Borrow the text string as a `&str`.
1205 pub fn as_str(&self) -> Result<&str> {
1206 match self {
1207 Self::TextString(s) => Ok(s.as_str()),
1208 Self::Tag(_number, content) => content.untagged().as_str(),
1209 _ => Err(Error::IncompatibleType),
1210 }
1211 }
1212
1213 /// Borrow the text string as a mutable `String`.
1214 pub const fn as_string_mut(&mut self) -> Result<&mut String> {
1215 match self {
1216 Self::TextString(s) => Ok(s),
1217 Self::Tag(_number, content) => content.untagged_mut().as_string_mut(),
1218 _ => Err(Error::IncompatibleType),
1219 }
1220 }
1221
1222 /// Take ownership of the text string.
1223 pub fn into_string(self) -> Result<String> {
1224 match self {
1225 Self::TextString(s) => Ok(s),
1226 Self::Tag(_number, content) => content.into_untagged().into_string(),
1227 _ => Err(Error::IncompatibleType),
1228 }
1229 }
1230
1231 /// Borrow the array elements as a slice.
1232 pub fn as_array(&self) -> Result<&[Value]> {
1233 match self {
1234 Self::Array(v) => Ok(v.as_slice()),
1235 Self::Tag(_number, content) => content.untagged().as_array(),
1236 _ => Err(Error::IncompatibleType),
1237 }
1238 }
1239
1240 /// Borrow the array as a mutable `Vec`.
1241 pub const fn as_array_mut(&mut self) -> Result<&mut Vec<Value>> {
1242 match self {
1243 Self::Array(v) => Ok(v),
1244 Self::Tag(_number, content) => content.untagged_mut().as_array_mut(),
1245 _ => Err(Error::IncompatibleType),
1246 }
1247 }
1248
1249 /// Take ownership of the array.
1250 pub fn into_array(self) -> Result<Vec<Value>> {
1251 match self {
1252 Self::Array(v) => Ok(v),
1253 Self::Tag(_number, content) => content.into_untagged().into_array(),
1254 _ => Err(Error::IncompatibleType),
1255 }
1256 }
1257
1258 /// Borrow the map.
1259 pub const fn as_map(&self) -> Result<&BTreeMap<Value, Value>> {
1260 match self {
1261 Self::Map(m) => Ok(m),
1262 Self::Tag(_number, content) => content.untagged().as_map(),
1263 _ => Err(Error::IncompatibleType),
1264 }
1265 }
1266
1267 /// Borrow the map mutably.
1268 pub const fn as_map_mut(&mut self) -> Result<&mut BTreeMap<Value, Value>> {
1269 match self {
1270 Self::Map(m) => Ok(m),
1271 Self::Tag(_number, content) => content.untagged_mut().as_map_mut(),
1272 _ => Err(Error::IncompatibleType),
1273 }
1274 }
1275
1276 /// Take ownership of the map.
1277 pub fn into_map(self) -> Result<BTreeMap<Value, Value>> {
1278 match self {
1279 Self::Map(m) => Ok(m),
1280 Self::Tag(_number, content) => content.into_untagged().into_map(),
1281 _ => Err(Error::IncompatibleType),
1282 }
1283 }
1284
1285 // --------------- Index access ---------------
1286
1287 /// Look up an element by index (arrays) or key (maps).
1288 ///
1289 /// Returns `None` if the value is not an array or map, the index
1290 /// is out of bounds, or the key is missing.
1291 ///
1292 /// ```
1293 /// use cbor_core::{Value, array, map};
1294 ///
1295 /// let a = array![10, 20, 30];
1296 /// assert_eq!(a.get(1).unwrap().to_u32().unwrap(), 20);
1297 /// assert!(a.get(5).is_none());
1298 ///
1299 /// let m = map! { "x" => 10 };
1300 /// assert_eq!(m.get("x").unwrap().to_u32().unwrap(), 10);
1301 /// assert!(m.get("missing").is_none());
1302 /// ```
1303 pub fn get(&self, index: impl Into<Value>) -> Option<&Value> {
1304 let key = index.into();
1305 match self.untagged() {
1306 Value::Array(arr) => key.to_usize().ok().and_then(|i| arr.get(i)),
1307 Value::Map(map) => map.get(&key),
1308 _ => None,
1309 }
1310 }
1311
1312 /// Mutable version of [`get`](Self::get).
1313 ///
1314 /// ```
1315 /// use cbor_core::{Value, array};
1316 ///
1317 /// let mut a = array![10, 20, 30];
1318 /// *a.get_mut(1).unwrap() = Value::from(99);
1319 /// assert_eq!(a[1].to_u32().unwrap(), 99);
1320 /// ```
1321 pub fn get_mut(&mut self, index: impl Into<Value>) -> Option<&mut Value> {
1322 let key = index.into();
1323 match self.untagged_mut() {
1324 Value::Array(arr) => key.to_usize().ok().and_then(|i| arr.get_mut(i)),
1325 Value::Map(map) => map.get_mut(&key),
1326 _ => None,
1327 }
1328 }
1329
1330 // ------------------- Tags ------------------
1331
1332 /// Return the tag number.
1333 pub const fn tag_number(&self) -> Result<u64> {
1334 match self {
1335 Self::Tag(number, _content) => Ok(*number),
1336 _ => Err(Error::IncompatibleType),
1337 }
1338 }
1339
1340 /// Borrow the tag content.
1341 pub const fn tag_content(&self) -> Result<&Self> {
1342 match self {
1343 Self::Tag(_tag, content) => Ok(content),
1344 _ => Err(Error::IncompatibleType),
1345 }
1346 }
1347
1348 /// Mutably borrow the tag content.
1349 pub const fn tag_content_mut(&mut self) -> Result<&mut Self> {
1350 match self {
1351 Self::Tag(_, value) => Ok(value),
1352 _ => Err(Error::IncompatibleType),
1353 }
1354 }
1355
1356 /// Borrow tag number and content together.
1357 pub fn as_tag(&self) -> Result<(u64, &Value)> {
1358 match self {
1359 Self::Tag(number, content) => Ok((*number, content)),
1360 _ => Err(Error::IncompatibleType),
1361 }
1362 }
1363
1364 /// Borrow tag number and mutable content together.
1365 pub fn as_tag_mut(&mut self) -> Result<(u64, &mut Value)> {
1366 match self {
1367 Self::Tag(number, content) => Ok((*number, content)),
1368 _ => Err(Error::IncompatibleType),
1369 }
1370 }
1371
1372 /// Consume self and return tag number and content.
1373 pub fn into_tag(self) -> Result<(u64, Value)> {
1374 match self {
1375 Self::Tag(number, content) => Ok((number, *content)),
1376 _ => Err(Error::IncompatibleType),
1377 }
1378 }
1379
1380 /// Remove the outermost tag, returning its number. Returns `None` if
1381 /// the value is not tagged.
1382 pub fn remove_tag(&mut self) -> Option<u64> {
1383 let mut result = None;
1384 if let Self::Tag(number, content) = self {
1385 result = Some(*number);
1386 *self = std::mem::take(content);
1387 }
1388 result
1389 }
1390
1391 /// Remove all nested tags, returning their numbers from outermost to
1392 /// innermost.
1393 pub fn remove_all_tags(&mut self) -> Vec<u64> {
1394 let mut tags = Vec::new();
1395 while let Self::Tag(number, content) = self {
1396 tags.push(*number);
1397 *self = std::mem::take(content);
1398 }
1399 tags
1400 }
1401
1402 /// Skip all tag wrappers except the innermost one.
1403 /// Returns `self` unchanged if not tagged or only single-tagged.
1404 #[must_use]
1405 pub(crate) const fn peeled(&self) -> &Self {
1406 let mut result = self;
1407 while let Self::Tag(_, content) = result
1408 && content.data_type().is_tag()
1409 {
1410 result = content;
1411 }
1412 result
1413 }
1414
1415 /// Borrow the innermost non-tag value, skipping all tag wrappers.
1416 #[must_use]
1417 pub const fn untagged(&self) -> &Self {
1418 let mut result = self;
1419 while let Self::Tag(_, content) = result {
1420 result = content;
1421 }
1422 result
1423 }
1424
1425 /// Mutable version of [`untagged`](Self::untagged).
1426 pub const fn untagged_mut(&mut self) -> &mut Self {
1427 let mut result = self;
1428 while let Self::Tag(_, content) = result {
1429 result = content;
1430 }
1431 result
1432 }
1433
1434 /// Consuming version of [`untagged`](Self::untagged).
1435 #[must_use]
1436 pub fn into_untagged(mut self) -> Self {
1437 while let Self::Tag(_number, content) = self {
1438 self = *content;
1439 }
1440 self
1441 }
1442}
1443
1444// -------------------- Helpers --------------------
1445
1446fn u128_from_bytes(bytes: &[u8]) -> Result<u128> {
1447 let mut buf = [0; 16];
1448 let offset = buf.len().checked_sub(bytes.len()).ok_or(Error::Overflow)?;
1449 buf[offset..].copy_from_slice(bytes);
1450 Ok(u128::from_be_bytes(buf))
1451}
1452
1453fn read_vec(reader: &mut impl io::Read, len: u64) -> Result<Vec<u8>> {
1454 use io::Read;
1455
1456 let len_usize = usize::try_from(len).map_err(|_| Error::LengthTooLarge)?;
1457
1458 let mut buf = Vec::with_capacity(len_usize.min(100_000_000)); // Mitigate OOM
1459 let bytes_read = reader.take(len).read_to_end(&mut buf)?;
1460
1461 if bytes_read == len_usize {
1462 Ok(buf)
1463 } else {
1464 Err(Error::UnexpectedEof)
1465 }
1466}