dcbor/
cbor_tagged_codable.rs

1use crate::{CBORTaggedEncodable, CBORTaggedDecodable};
2
3/// # Tagged CBOR Encoding and Decoding Support
4///
5/// This module provides the `CBORTaggedCodable` trait, which serves as a convenience
6/// marker for types that can be both encoded to and decoded from tagged CBOR values.
7///
8/// The trait is automatically implemented for any type that implements both
9/// `CBORTaggedEncodable` and `CBORTaggedDecodable`.
10/// A trait for types that can be both encoded to and decoded from CBOR with a specific tag.
11///
12/// This trait is automatically implemented for any type that implements both
13/// `CBORTaggedEncodable` and `CBORTaggedDecodable`. It serves as a convenience marker
14/// to indicate full tagged CBOR serialization support.
15///
16/// ## Example
17///
18/// ```
19/// use dcbor::prelude::*;
20///
21/// // Define a Date type
22/// #[derive(Debug, Clone, PartialEq)]
23/// struct Date(f64); // Timestamp as seconds since epoch
24///
25/// // Implement CBORTagged
26/// impl CBORTagged for Date {
27///     fn cbor_tags() -> Vec<Tag> {
28///         vec![Tag::with_value(1)] // Standard date tag
29///     }
30/// }
31///
32/// // Implement encoding to tagged CBOR
33/// impl CBORTaggedEncodable for Date {
34///     fn untagged_cbor(&self) -> CBOR {
35///         self.0.into()
36///     }
37/// }
38///
39/// // Implement decoding from tagged CBOR
40/// impl CBORTaggedDecodable for Date {
41///     fn from_untagged_cbor(cbor: CBOR) -> dcbor::Result<Self> {
42///         let timestamp: f64 = cbor.try_into()?;
43///         Ok(Date(timestamp))
44///     }
45/// }
46///
47/// // Implement TryFrom<CBOR> (required by CBORTaggedDecodable)
48/// impl TryFrom<CBOR> for Date {
49///     type Error = dcbor::Error;
50///
51///     fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
52///         Self::from_tagged_cbor(cbor)
53///     }
54/// }
55///
56/// // The CBORTaggedCodable trait is automatically implemented
57/// // Create a date and demonstrate round-trip conversion
58/// let original = Date(1609459200.0);
59/// let cbor = original.tagged_cbor();
60/// let roundtrip: Date = cbor.try_into().unwrap();
61/// assert_eq!(original, roundtrip);
62/// ```
63pub trait CBORTaggedCodable { }
64
65impl<T> CBORTaggedCodable for T where T: CBORTaggedEncodable + CBORTaggedDecodable { }