• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / catalyst-libs / 15458391692

05 Jun 2025 04:23AM UTC coverage: 66.098% (+0.4%) from 65.732%
15458391692

Pull #356

github

web-flow
Merge c2788a2f2 into bbcb401c2
Pull Request #356: feat(rust/signed-doc): New implementation of `DocType`

374 of 432 new or added lines in 12 files covered. (86.57%)

18 existing lines in 4 files now uncovered.

11275 of 17058 relevant lines covered (66.1%)

2375.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.59
/rust/signed_doc/src/metadata/utils.rs
1
//! Utility functions for metadata decoding fields
2

3
use catalyst_types::{
4
    problem_report::ProblemReport,
5
    uuid::{CborContext, UuidV4, UuidV7},
6
};
7
use coset::{CborSerializable, Label, ProtectedHeader};
8

9
/// Find a value for a predicate in the protected header.
10
pub(crate) fn cose_protected_header_find(
143✔
11
    protected: &coset::ProtectedHeader, mut predicate: impl FnMut(&coset::Label) -> bool,
143✔
12
) -> Option<&coset::cbor::Value> {
143✔
13
    protected
143✔
14
        .header
143✔
15
        .rest
143✔
16
        .iter()
143✔
17
        .find(|(key, _)| predicate(key))
939✔
18
        .map(|(_, value)| value)
143✔
19
}
143✔
20

21
/// Tries to decode field by the `field_name` from the COSE protected header
22
pub(crate) fn decode_document_field_from_protected_header<T>(
110✔
23
    protected: &ProtectedHeader, field_name: &str, report_content: &str, report: &ProblemReport,
110✔
24
) -> Option<T>
110✔
25
where T: for<'a> TryFrom<&'a coset::cbor::Value> {
110✔
26
    if let Some(cbor_doc_field) =
81✔
27
        cose_protected_header_find(protected, |key| key == &Label::Text(field_name.to_string()))
807✔
28
    {
29
        if let Ok(field) = T::try_from(cbor_doc_field) {
81✔
30
            return Some(field);
81✔
31
        }
×
32
        report.conversion_error(
×
33
            &format!("CBOR COSE protected header {field_name}"),
×
34
            &format!("{cbor_doc_field:?}"),
×
35
            "Expected a CBOR UUID",
×
36
            &format!("{report_content}, decoding CBOR UUID for {field_name}",),
×
37
        );
×
38
    }
29✔
39
    None
29✔
40
}
110✔
41

42
/// A convenient wrapper over the `UuidV4` type, to implement
43
/// `TryFrom<coset::cbor::Value>` and `TryFrom<Self> for coset::cbor::Value` traits.
44
pub(crate) struct CborUuidV4(pub(crate) UuidV4);
45
impl TryFrom<&coset::cbor::Value> for CborUuidV4 {
46
    type Error = anyhow::Error;
47

UNCOV
48
    fn try_from(value: &coset::cbor::Value) -> Result<Self, Self::Error> {
×
UNCOV
49
        Ok(Self(decode_cbor_uuid(value)?))
×
UNCOV
50
    }
×
51
}
52
impl TryFrom<CborUuidV4> for coset::cbor::Value {
53
    type Error = anyhow::Error;
54

UNCOV
55
    fn try_from(value: CborUuidV4) -> Result<Self, Self::Error> {
×
UNCOV
56
        encode_cbor_uuid(value.0)
×
UNCOV
57
    }
×
58
}
59

60
/// A convenient wrapper over the `UuidV7` type, to implement
61
/// `TryFrom<coset::cbor::Value>` and `TryFrom<Self> for coset::cbor::Value` traits.
62
pub(crate) struct CborUuidV7(pub(crate) UuidV7);
63
impl TryFrom<&coset::cbor::Value> for CborUuidV7 {
64
    type Error = anyhow::Error;
65

66
    fn try_from(value: &coset::cbor::Value) -> Result<Self, Self::Error> {
118✔
67
        Ok(Self(decode_cbor_uuid(value)?))
118✔
68
    }
118✔
69
}
70
impl TryFrom<CborUuidV7> for coset::cbor::Value {
71
    type Error = anyhow::Error;
72

73
    fn try_from(value: CborUuidV7) -> Result<Self, Self::Error> {
350✔
74
        encode_cbor_uuid(value.0)
350✔
75
    }
350✔
76
}
77

78
/// Encode `uuid::Uuid` type into `coset::cbor::Value`.
79
///
80
/// This is used to encode `UuidV4` and `UuidV7` types.
81
fn encode_cbor_uuid<T: minicbor::encode::Encode<CborContext>>(
350✔
82
    value: T,
350✔
83
) -> anyhow::Result<coset::cbor::Value> {
350✔
84
    let mut cbor_bytes = Vec::new();
350✔
85
    minicbor::encode_with(value, &mut cbor_bytes, &mut CborContext::Tagged)
350✔
86
        .map_err(|e| anyhow::anyhow!("Unable to encode CBOR value, err: {e}"))?;
350✔
87
    coset::cbor::Value::from_slice(&cbor_bytes)
350✔
88
        .map_err(|e| anyhow::anyhow!("Invalid CBOR value, err: {e}"))
350✔
89
}
350✔
90

91
/// Decode `From<uuid::Uuid>` type from `coset::cbor::Value`.
92
///
93
/// This is used to decode `UuidV4` and `UuidV7` types.
94
fn decode_cbor_uuid<T: for<'a> minicbor::decode::Decode<'a, CborContext>>(
118✔
95
    value: &coset::cbor::Value,
118✔
96
) -> anyhow::Result<T> {
118✔
97
    let mut cbor_bytes = Vec::new();
118✔
98
    coset::cbor::ser::into_writer(value, &mut cbor_bytes)
118✔
99
        .map_err(|e| anyhow::anyhow!("Invalid CBOR value, err: {e}"))?;
118✔
100
    minicbor::decode_with(&cbor_bytes, &mut CborContext::Tagged)
118✔
101
        .map_err(|e| anyhow::anyhow!("Invalid UUID, err: {e}"))
118✔
102
}
118✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc