bc_ur/
ur_encodable.rs

1use crate::ur::UR;
2
3use dcbor::prelude::*;
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!("CBOR tag {} must have a name. Did you call `register_tags()`?", tag.value());
14        }
15    }
16
17    /// Returns the UR string representation of the object.
18    fn ur_string(&self) -> String {
19        self.ur().string()
20    }
21}
22
23impl<T> UREncodable for T where T: CBORTaggedEncodable {}