dcbor/cbor_tagged_codable.rs
1use crate::{CBORTaggedDecodable, CBORTaggedEncodable};
2
3/// # Tagged CBOR Encoding and Decoding Support
4///
5/// This module provides the `CBORTaggedCodable` trait, which serves as a
6/// convenience marker for types that can be both encoded to and decoded from
7/// tagged CBOR values.
8///
9/// The trait is automatically implemented for any type that implements both
10/// `CBORTaggedEncodable` and `CBORTaggedDecodable`.
11/// A trait for types that can be both encoded to and decoded from CBOR with a
12/// specific tag.
13///
14/// This trait is automatically implemented for any type that implements both
15/// `CBORTaggedEncodable` and `CBORTaggedDecodable`. It serves as a convenience
16/// marker to indicate full tagged CBOR serialization support.
17///
18/// ## Example
19///
20/// ```
21/// use dcbor::prelude::*;
22///
23/// // Define a Date type
24/// #[derive(Debug, Clone, PartialEq)]
25/// struct Date(f64); // Timestamp as seconds since epoch
26///
27/// // Implement CBORTagged
28/// impl CBORTagged for Date {
29/// fn cbor_tags() -> Vec<Tag> {
30/// vec![Tag::with_value(1)] // Standard date tag
31/// }
32/// }
33///
34/// // Implement encoding to tagged CBOR
35/// impl CBORTaggedEncodable for Date {
36/// fn untagged_cbor(&self) -> CBOR { self.0.into() }
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
66 T: CBORTaggedEncodable + CBORTaggedDecodable
67{
68}