bc_ur/
ur_encodable.rs

1use dcbor::prelude::*;
2
3use crate::ur::UR;
4
5/// A type that can be encoded to a UR.
6pub trait UREncodable: CBORTaggedEncodable {
7    /// Returns the UR representation of the object.
8    fn ur(&self) -> UR {
9        let tag = &Self::cbor_tags()[0];
10        if let Some(name) = tag.name().as_ref() {
11            UR::new(name.clone(), self.untagged_cbor()).unwrap()
12        } else {
13            panic!(
14                "CBOR tag {} must have a name. Did you call `register_tags()`?",
15                tag.value()
16            );
17        }
18    }
19
20    /// Returns the UR string representation of the object.
21    fn ur_string(&self) -> String { self.ur().string() }
22}
23
24impl<T> UREncodable for T where T: CBORTaggedEncodable {}