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

input-output-hk / catalyst-libs / 17261071422

27 Aug 2025 08:09AM UTC coverage: 66.962% (+0.01%) from 66.948%
17261071422

Pull #497

github

web-flow
Merge 94dfdf398 into 74fefc0f1
Pull Request #497: refactor(rust/signed-doc): Move `validate_id_and_ver` as separate rules `IdRule` and `VerRule`

88 of 111 new or added lines in 4 files covered. (79.28%)

3 existing lines in 1 file now uncovered.

12623 of 18851 relevant lines covered (66.96%)

2799.35 hits per line

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

78.57
/rust/signed_doc/src/validator/rules/id.rs
1
//! Validator for Signed Document ID
2

3
use std::time::{Duration, SystemTime};
4

5
use anyhow::Context;
6

7
use crate::{providers::CatalystSignedDocumentProvider, CatalystSignedDocument};
8

9
/// Signed Document `id` field validation rule
10
pub(crate) struct IdRule;
11

12
impl IdRule {
13
    /// Validates document `id` field on the timestamps:
14
    /// 1. If `provider.future_threshold()` not `None`, document `id` cannot be too far in
15
    ///    the future (`future_threshold` arg) from `SystemTime::now()` based on the
16
    ///    provide threshold
17
    /// 2. If `provider.future_threshold()` not `None`, document `id` cannot be too far
18
    ///    behind (`past_threshold` arg) from `SystemTime::now()` based on the provide
19
    ///    threshold
20
    #[allow(clippy::unused_async)]
21
    pub(crate) async fn check<Provider>(
18✔
22
        &self,
18✔
23
        doc: &CatalystSignedDocument,
18✔
24
        provider: &Provider,
18✔
25
    ) -> anyhow::Result<bool>
18✔
26
    where
18✔
27
        Provider: CatalystSignedDocumentProvider,
18✔
28
    {
18✔
29
        let Ok(id) = doc.doc_id() else {
18✔
NEW
30
            doc.report().missing_field(
×
NEW
31
                "id",
×
NEW
32
                "Cannot get the document field during the field validation",
×
33
            );
NEW
34
            return Ok(false);
×
35
        };
36

37
        let mut is_valid = true;
18✔
38

39
        let (id_time_secs, id_time_nanos) = id
18✔
40
            .uuid()
18✔
41
            .get_timestamp()
18✔
42
            .ok_or(anyhow::anyhow!("Document `id` field must be a UUIDv7"))?
18✔
43
            .to_unix();
18✔
44

45
        let Some(id_time) =
18✔
46
            SystemTime::UNIX_EPOCH.checked_add(Duration::new(id_time_secs, id_time_nanos))
18✔
47
        else {
NEW
48
            doc.report().invalid_value(
×
NEW
49
                    "id",
×
NEW
50
                    &id.to_string(),
×
NEW
51
                    "Must a valid duration since `UNIX_EPOCH`",
×
NEW
52
                    "Cannot instantiate a valid `SystemTime` value from the provided `id` field timestamp.",
×
53
                );
NEW
54
            return Ok(false);
×
55
        };
56

57
        let now = SystemTime::now();
18✔
58

59
        if let Ok(id_age) = id_time.duration_since(now) {
18✔
60
            // `now` is earlier than `id_time`
61
            if let Some(future_threshold) = provider.future_threshold() {
2✔
62
                if id_age > future_threshold {
2✔
63
                    doc.report().invalid_value(
1✔
64
                        "id",
1✔
65
                        &id.to_string(),
1✔
66
                        "id < now + future_threshold",
1✔
67
                        &format!("Document Version timestamp {id} cannot be too far in future (threshold: {future_threshold:?}) from now: {now:?}"),
1✔
68
                    );
1✔
69
                    is_valid = false;
1✔
70
                }
1✔
NEW
71
            }
×
72
        } else {
73
            // `id_time` is earlier than `now`
74
            let id_age = now
16✔
75
                .duration_since(id_time)
16✔
76
                .context("BUG! `id_time` must be earlier than `now` at this place")?;
16✔
77

78
            if let Some(past_threshold) = provider.past_threshold() {
16✔
79
                if id_age > past_threshold {
16✔
80
                    doc.report().invalid_value(
1✔
81
                        "id",
1✔
82
                        &id.to_string(),
1✔
83
                        "id > now - past_threshold",
1✔
84
                        &format!("Document Version timestamp {id} cannot be too far behind (threshold: {past_threshold:?}) from now: {now:?}",),
1✔
85
                    );
1✔
86
                    is_valid = false;
1✔
87
                }
15✔
NEW
88
            }
×
89
        }
90

91
        Ok(is_valid)
18✔
92
    }
18✔
93
}
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

© 2025 Coveralls, Inc