vellaveto-types 6.1.1

Core type definitions for Vellaveto policy engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright 2026 Paolo Vella
// SPDX-License-Identifier: MPL-2.0

//! Compliance Evidence Pack types for DORA, NIS2, ISO 42001, and EU AI Act.
//!
//! Defines a unified evidence bundle format for auditor-ready compliance
//! evidence packs. Each pack contains sections of evidence items mapping
//! regulatory requirements to Vellaveto capabilities.

use serde::{Deserialize, Serialize};

// ── Constants ────────────────────────────────────────────────────────────────

/// Maximum number of sections in an evidence pack.
pub const MAX_EVIDENCE_SECTIONS: usize = 100;

/// Maximum number of evidence items per section.
pub const MAX_EVIDENCE_ITEMS_PER_SECTION: usize = 200;

/// Maximum number of critical gaps in an evidence pack.
pub const MAX_EVIDENCE_PACK_GAPS: usize = 500;

/// Maximum number of recommendations in an evidence pack.
pub const MAX_EVIDENCE_RECOMMENDATIONS: usize = 100;

/// Maximum length for evidence string fields.
pub const MAX_EVIDENCE_STRING_LEN: usize = 4_096;

// ── Evidence Framework ───────────────────────────────────────────────────────

/// Compliance framework for which an evidence pack can be generated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum EvidenceFramework {
    /// EU Digital Operational Resilience Act.
    Dora,
    /// EU Network and Information Security Directive 2.
    Nis2,
    /// ISO/IEC 42001 AI Management System.
    Iso42001,
    /// EU Artificial Intelligence Act.
    EuAiAct,
}

impl std::fmt::Display for EvidenceFramework {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Dora => write!(f, "DORA"),
            Self::Nis2 => write!(f, "NIS2"),
            Self::Iso42001 => write!(f, "ISO 42001"),
            Self::EuAiAct => write!(f, "EU AI Act"),
        }
    }
}

// ── Evidence Confidence ──────────────────────────────────────────────────────

/// Confidence level of evidence mapping a requirement to a capability.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EvidenceConfidence {
    /// No evidence available.
    None,
    /// Minimal evidence — indirect or tangential coverage.
    Low,
    /// Partial evidence — some capability mapped but gaps remain.
    Medium,
    /// Strong evidence — capability directly addresses the requirement.
    High,
    /// Complete evidence — full coverage with verification.
    Full,
}

impl std::fmt::Display for EvidenceConfidence {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::None => write!(f, "None"),
            Self::Low => write!(f, "Low"),
            Self::Medium => write!(f, "Medium"),
            Self::High => write!(f, "High"),
            Self::Full => write!(f, "Full"),
        }
    }
}

// ── Evidence Item ────────────────────────────────────────────────────────────

/// A single evidence item mapping a regulatory requirement to a Vellaveto capability.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EvidenceItem {
    /// Requirement identifier (e.g., "Art 5", "21.2.a").
    pub requirement_id: String,
    /// Short title of the requirement.
    pub requirement_title: String,
    /// Article or clause reference in the regulation.
    pub article_ref: String,
    /// Vellaveto capability providing the evidence.
    pub vellaveto_capability: String,
    /// Description of how the capability addresses the requirement.
    pub evidence_description: String,
    /// Confidence level of this evidence mapping.
    pub confidence: EvidenceConfidence,
    /// Identified gaps for this requirement.
    #[serde(default)]
    pub gaps: Vec<String>,
}

impl EvidenceItem {
    /// Validate evidence item bounds.
    pub fn validate(&self) -> Result<(), String> {
        Self::check_field("requirement_id", &self.requirement_id)?;
        Self::check_field("requirement_title", &self.requirement_title)?;
        Self::check_field("article_ref", &self.article_ref)?;
        Self::check_field("vellaveto_capability", &self.vellaveto_capability)?;
        Self::check_field("evidence_description", &self.evidence_description)?;
        if self.gaps.len() > MAX_EVIDENCE_PACK_GAPS {
            return Err(format!(
                "EvidenceItem.gaps has {} entries, max is {}",
                self.gaps.len(),
                MAX_EVIDENCE_PACK_GAPS,
            ));
        }
        for (i, gap) in self.gaps.iter().enumerate() {
            if gap.len() > MAX_EVIDENCE_STRING_LEN {
                return Err(format!(
                    "EvidenceItem.gaps[{}] length {} exceeds max {}",
                    i,
                    gap.len(),
                    MAX_EVIDENCE_STRING_LEN,
                ));
            }
            if crate::has_dangerous_chars(gap) {
                return Err(format!(
                    "EvidenceItem.gaps[{i}] contains control or format characters",
                ));
            }
        }
        Ok(())
    }

    fn check_field(name: &str, value: &str) -> Result<(), String> {
        if value.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidenceItem.{} length {} exceeds max {}",
                name,
                value.len(),
                MAX_EVIDENCE_STRING_LEN,
            ));
        }
        if crate::has_dangerous_chars(value) {
            return Err(format!(
                "EvidenceItem.{name} contains control or format characters",
            ));
        }
        Ok(())
    }
}

// ── Evidence Section ─────────────────────────────────────────────────────────

/// A group of related evidence items within an evidence pack.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EvidenceSection {
    /// Section identifier (e.g., "ict-risk", "art-21-2").
    pub section_id: String,
    /// Human-readable section title.
    pub title: String,
    /// Section description.
    pub description: String,
    /// Evidence items in this section.
    pub items: Vec<EvidenceItem>,
    /// Coverage percentage for this section (0.0–100.0).
    pub section_coverage_percent: f32,
}

impl EvidenceSection {
    /// Validate evidence section bounds.
    pub fn validate(&self) -> Result<(), String> {
        if self.section_id.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidenceSection.section_id length {} exceeds max {}",
                self.section_id.len(),
                MAX_EVIDENCE_STRING_LEN,
            ));
        }
        if crate::has_dangerous_chars(&self.section_id) {
            return Err(
                "EvidenceSection.section_id contains control or format characters".to_string(),
            );
        }
        if self.title.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidenceSection.title length {} exceeds max {}",
                self.title.len(),
                MAX_EVIDENCE_STRING_LEN,
            ));
        }
        if crate::has_dangerous_chars(&self.title) {
            return Err("EvidenceSection.title contains control or format characters".to_string());
        }
        if self.description.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidenceSection.description length {} exceeds max {MAX_EVIDENCE_STRING_LEN}",
                self.description.len(),
            ));
        }
        if crate::has_dangerous_chars(&self.description) {
            return Err(
                "EvidenceSection.description contains control or format characters".to_string(),
            );
        }
        if self.items.len() > MAX_EVIDENCE_ITEMS_PER_SECTION {
            return Err(format!(
                "EvidenceSection.items has {} entries, max is {}",
                self.items.len(),
                MAX_EVIDENCE_ITEMS_PER_SECTION,
            ));
        }
        if !self.section_coverage_percent.is_finite()
            || self.section_coverage_percent < 0.0
            || self.section_coverage_percent > 100.0
        {
            return Err(format!(
                "EvidenceSection.section_coverage_percent {} out of range [0.0, 100.0]",
                self.section_coverage_percent,
            ));
        }
        for item in &self.items {
            item.validate()?;
        }
        Ok(())
    }
}

// ── Evidence Pack ────────────────────────────────────────────────────────────

/// A complete compliance evidence pack for a single framework.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EvidencePack {
    /// The compliance framework this pack covers.
    pub framework: EvidenceFramework,
    /// Human-readable framework name.
    pub framework_name: String,
    /// ISO 8601 timestamp of generation.
    pub generated_at: String,
    /// Organization name.
    pub organization_name: String,
    /// System identifier.
    pub system_id: String,
    /// Optional period start (ISO 8601).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub period_start: Option<String>,
    /// Optional period end (ISO 8601).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub period_end: Option<String>,
    /// Evidence sections grouped by regulatory topic.
    pub sections: Vec<EvidenceSection>,
    /// Overall coverage percentage (0.0–100.0).
    pub overall_coverage_percent: f32,
    /// Total number of requirements in the framework.
    pub total_requirements: usize,
    /// Number of fully covered requirements.
    pub covered_requirements: usize,
    /// Number of partially covered requirements.
    pub partial_requirements: usize,
    /// Number of uncovered requirements.
    pub uncovered_requirements: usize,
    /// Critical gaps requiring attention.
    pub critical_gaps: Vec<String>,
    /// Actionable recommendations.
    pub recommendations: Vec<String>,
}

impl EvidencePack {
    /// Validate evidence pack bounds.
    pub fn validate(&self) -> Result<(), String> {
        // SECURITY (IMP-R222-EP-001): Validate generated_at and period timestamps.
        // These ISO 8601 strings were missing length + dangerous char checks.
        for (name, val) in [
            ("generated_at", Some(&self.generated_at)),
            ("period_start", self.period_start.as_ref()),
            ("period_end", self.period_end.as_ref()),
        ] {
            if let Some(v) = val {
                if v.len() > MAX_EVIDENCE_STRING_LEN {
                    return Err(format!(
                        "EvidencePack.{name} length {} exceeds max {MAX_EVIDENCE_STRING_LEN}",
                        v.len(),
                    ));
                }
                if crate::has_dangerous_chars(v) {
                    return Err(format!(
                        "EvidencePack.{name} contains control or format characters",
                    ));
                }
            }
        }
        if self.framework_name.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidencePack.framework_name length {} exceeds max {}",
                self.framework_name.len(),
                MAX_EVIDENCE_STRING_LEN,
            ));
        }
        if crate::has_dangerous_chars(&self.framework_name) {
            return Err(
                "EvidencePack.framework_name contains control or format characters".to_string(),
            );
        }
        if self.organization_name.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidencePack.organization_name length {} exceeds max {}",
                self.organization_name.len(),
                MAX_EVIDENCE_STRING_LEN,
            ));
        }
        if crate::has_dangerous_chars(&self.organization_name) {
            return Err(
                "EvidencePack.organization_name contains control or format characters".to_string(),
            );
        }
        if self.system_id.len() > MAX_EVIDENCE_STRING_LEN {
            return Err(format!(
                "EvidencePack.system_id length {} exceeds max {}",
                self.system_id.len(),
                MAX_EVIDENCE_STRING_LEN,
            ));
        }
        if crate::has_dangerous_chars(&self.system_id) {
            return Err("EvidencePack.system_id contains control or format characters".to_string());
        }
        if self.sections.len() > MAX_EVIDENCE_SECTIONS {
            return Err(format!(
                "EvidencePack.sections has {} entries, max is {}",
                self.sections.len(),
                MAX_EVIDENCE_SECTIONS,
            ));
        }
        if !self.overall_coverage_percent.is_finite()
            || self.overall_coverage_percent < 0.0
            || self.overall_coverage_percent > 100.0
        {
            return Err(format!(
                "EvidencePack.overall_coverage_percent {} out of range [0.0, 100.0]",
                self.overall_coverage_percent,
            ));
        }
        if self.critical_gaps.len() > MAX_EVIDENCE_PACK_GAPS {
            return Err(format!(
                "EvidencePack.critical_gaps has {} entries, max is {}",
                self.critical_gaps.len(),
                MAX_EVIDENCE_PACK_GAPS,
            ));
        }
        for (i, gap) in self.critical_gaps.iter().enumerate() {
            if gap.len() > MAX_EVIDENCE_STRING_LEN {
                return Err(format!(
                    "EvidencePack.critical_gaps[{i}] length {} exceeds max {MAX_EVIDENCE_STRING_LEN}",
                    gap.len(),
                ));
            }
            if crate::has_dangerous_chars(gap) {
                return Err(format!(
                    "EvidencePack.critical_gaps[{i}] contains control or format characters",
                ));
            }
        }
        if self.recommendations.len() > MAX_EVIDENCE_RECOMMENDATIONS {
            return Err(format!(
                "EvidencePack.recommendations has {} entries, max is {}",
                self.recommendations.len(),
                MAX_EVIDENCE_RECOMMENDATIONS,
            ));
        }
        for (i, rec) in self.recommendations.iter().enumerate() {
            if rec.len() > MAX_EVIDENCE_STRING_LEN {
                return Err(format!(
                    "EvidencePack.recommendations[{i}] length {} exceeds max {MAX_EVIDENCE_STRING_LEN}",
                    rec.len(),
                ));
            }
            if crate::has_dangerous_chars(rec) {
                return Err(format!(
                    "EvidencePack.recommendations[{i}] contains control or format characters",
                ));
            }
        }
        // SECURITY (IMP-R222-009): Requirement count consistency check.
        let sum = self
            .covered_requirements
            .saturating_add(self.partial_requirements)
            .saturating_add(self.uncovered_requirements);
        if sum > self.total_requirements {
            return Err(format!(
                "EvidencePack: covered({}) + partial({}) + uncovered({}) = {} exceeds total_requirements({})",
                self.covered_requirements,
                self.partial_requirements,
                self.uncovered_requirements,
                sum,
                self.total_requirements,
            ));
        }
        for section in &self.sections {
            section.validate()?;
        }
        Ok(())
    }
}

// ── Evidence Pack Status ─────────────────────────────────────────────────────

/// Status response for the evidence pack endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EvidencePackStatus {
    /// Frameworks available for evidence pack generation.
    pub available_frameworks: Vec<EvidenceFramework>,
    /// Whether DORA evidence generation is enabled.
    pub dora_enabled: bool,
    /// Whether NIS2 evidence generation is enabled.
    pub nis2_enabled: bool,
}

impl EvidencePackStatus {
    /// Maximum number of available frameworks.
    const MAX_FRAMEWORKS: usize = 50;

    /// Validate structural bounds on deserialized data.
    ///
    /// SECURITY (FIND-R216-016): Prevents unbounded framework lists from
    /// untrusted deserialized payloads.
    pub fn validate(&self) -> Result<(), String> {
        if self.available_frameworks.len() > Self::MAX_FRAMEWORKS {
            return Err(format!(
                "EvidencePackStatus available_frameworks count {} exceeds max {}",
                self.available_frameworks.len(),
                Self::MAX_FRAMEWORKS,
            ));
        }
        Ok(())
    }
}