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
// 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

// ═══════════════════════════════════════════════════════════════════════════════
// METERING — Per-tenant usage tracking types for billing periods
// ═══════════════════════════════════════════════════════════════════════════════
//
// Phase 50: Usage Metering & Billing Foundation.
// These types represent per-tenant usage counters, quota status, billing periods,
// and historical usage summaries. All types enforce bounds via validate() and
// reject control/format characters in string fields.

use serde::{Deserialize, Serialize};

/// Maximum number of historical periods returned in a usage summary.
pub const MAX_USAGE_PERIODS: usize = 120;

/// Maximum length for tenant_id in metering types (matches tenant validation).
pub const MAX_METERING_TENANT_ID_LEN: usize = 64;

/// Maximum length for ISO 8601 timestamp strings in metering types.
const MAX_TIMESTAMP_LEN: usize = 64;

/// Per-tenant usage counters for a billing period.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UsageMetrics {
    /// Tenant identifier.
    pub tenant_id: String,
    /// Period start (ISO 8601).
    pub period_start: String,
    /// Period end (ISO 8601).
    pub period_end: String,
    /// Total evaluations in this period.
    pub evaluations: u64,
    /// Evaluations that resulted in Allow.
    pub evaluations_allowed: u64,
    /// Evaluations that resulted in Deny.
    pub evaluations_denied: u64,
    /// Policies created in this period.
    pub policies_created: u64,
    /// Approvals processed (approve + deny) in this period.
    pub approvals_processed: u64,
    /// Audit entries recorded in this period.
    pub audit_entries: u64,
}

impl UsageMetrics {
    /// Validate usage metrics fields.
    pub fn validate(&self) -> Result<(), String> {
        validate_metering_tenant_id(&self.tenant_id)?;
        validate_metering_timestamp(&self.period_start, "period_start")?;
        validate_metering_timestamp(&self.period_end, "period_end")?;
        // evaluations_allowed + evaluations_denied should not exceed total
        // (but we don't enforce this strictly — counters may be updated independently)
        Ok(())
    }
}

/// Quota status for a tenant — usage vs limits.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct QuotaStatus {
    /// Tenant identifier.
    pub tenant_id: String,
    /// Evaluations used in current period.
    pub evaluations_used: u64,
    /// Evaluation limit for current period.
    pub evaluations_limit: u64,
    /// Evaluations remaining (saturating subtraction).
    pub evaluations_remaining: u64,
    /// Policies currently active for this tenant.
    pub policies_used: u64,
    /// Policy limit for this tenant.
    pub policies_limit: u64,
    /// Period start (ISO 8601).
    pub period_start: String,
    /// Period end (ISO 8601).
    pub period_end: String,
    /// Whether the evaluation quota has been exceeded.
    pub quota_exceeded: bool,
}

impl QuotaStatus {
    /// Validate quota status fields.
    pub fn validate(&self) -> Result<(), String> {
        validate_metering_tenant_id(&self.tenant_id)?;
        validate_metering_timestamp(&self.period_start, "period_start")?;
        validate_metering_timestamp(&self.period_end, "period_end")?;
        Ok(())
    }
}

/// Billing period boundaries.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BillingPeriod {
    /// Period start (ISO 8601).
    pub start: String,
    /// Period end (ISO 8601).
    pub end: String,
}

impl BillingPeriod {
    /// Validate billing period fields.
    pub fn validate(&self) -> Result<(), String> {
        validate_metering_timestamp(&self.start, "start")?;
        validate_metering_timestamp(&self.end, "end")?;
        Ok(())
    }
}

/// Usage summary across periods (for historical queries).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UsageSummary {
    /// Tenant identifier.
    pub tenant_id: String,
    /// Historical usage periods (bounded by MAX_USAGE_PERIODS).
    pub periods: Vec<UsageMetrics>,
    /// Lifetime total evaluations across all returned periods.
    pub total_evaluations: u64,
}

impl UsageSummary {
    /// Validate usage summary fields.
    pub fn validate(&self) -> Result<(), String> {
        validate_metering_tenant_id(&self.tenant_id)?;
        if self.periods.len() > MAX_USAGE_PERIODS {
            return Err(format!(
                "periods has {} entries, max is {}",
                self.periods.len(),
                MAX_USAGE_PERIODS
            ));
        }
        for (i, period) in self.periods.iter().enumerate() {
            period
                .validate()
                .map_err(|e| format!("periods[{i}]: {e}"))?;
        }
        Ok(())
    }
}

/// Validate a tenant ID for metering types.
fn validate_metering_tenant_id(id: &str) -> Result<(), String> {
    if id.is_empty() {
        return Err("tenant_id must not be empty".to_string());
    }
    if id.len() > MAX_METERING_TENANT_ID_LEN {
        return Err(format!(
            "tenant_id length {} exceeds max {}",
            id.len(),
            MAX_METERING_TENANT_ID_LEN
        ));
    }
    if !id
        .chars()
        .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
    {
        return Err("tenant_id contains invalid characters".to_string());
    }
    Ok(())
}

/// Validate a timestamp string for metering types.
fn validate_metering_timestamp(ts: &str, field: &str) -> Result<(), String> {
    if ts.is_empty() {
        return Err(format!("{field} must not be empty"));
    }
    if ts.len() > MAX_TIMESTAMP_LEN {
        return Err(format!(
            "{} length {} exceeds max {}",
            field,
            ts.len(),
            MAX_TIMESTAMP_LEN
        ));
    }
    if crate::has_dangerous_chars(ts) {
        return Err(format!("{field} contains control or format characters"));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_usage_metrics_validate_ok() {
        let m = UsageMetrics {
            tenant_id: "acme".to_string(),
            period_start: "2026-02-01T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 100,
            evaluations_allowed: 80,
            evaluations_denied: 20,
            policies_created: 5,
            approvals_processed: 3,
            audit_entries: 100,
        };
        assert!(m.validate().is_ok());
    }

    #[test]
    fn test_usage_metrics_validate_empty_tenant() {
        let m = UsageMetrics {
            tenant_id: String::new(),
            period_start: "2026-02-01T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 0,
            evaluations_allowed: 0,
            evaluations_denied: 0,
            policies_created: 0,
            approvals_processed: 0,
            audit_entries: 0,
        };
        let err = m.validate().unwrap_err();
        assert!(err.contains("must not be empty"));
    }

    #[test]
    fn test_usage_metrics_validate_tenant_too_long() {
        let m = UsageMetrics {
            tenant_id: "a".repeat(65),
            period_start: "2026-02-01T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 0,
            evaluations_allowed: 0,
            evaluations_denied: 0,
            policies_created: 0,
            approvals_processed: 0,
            audit_entries: 0,
        };
        let err = m.validate().unwrap_err();
        assert!(err.contains("exceeds max"));
    }

    #[test]
    fn test_usage_metrics_validate_tenant_invalid_chars() {
        let m = UsageMetrics {
            tenant_id: "acme/corp".to_string(),
            period_start: "2026-02-01T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 0,
            evaluations_allowed: 0,
            evaluations_denied: 0,
            policies_created: 0,
            approvals_processed: 0,
            audit_entries: 0,
        };
        let err = m.validate().unwrap_err();
        assert!(err.contains("invalid characters"));
    }

    #[test]
    fn test_usage_metrics_validate_empty_timestamp() {
        let m = UsageMetrics {
            tenant_id: "acme".to_string(),
            period_start: String::new(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 0,
            evaluations_allowed: 0,
            evaluations_denied: 0,
            policies_created: 0,
            approvals_processed: 0,
            audit_entries: 0,
        };
        let err = m.validate().unwrap_err();
        assert!(err.contains("period_start must not be empty"));
    }

    #[test]
    fn test_usage_metrics_validate_timestamp_control_chars() {
        let m = UsageMetrics {
            tenant_id: "acme".to_string(),
            period_start: "2026-02-01\x00T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 0,
            evaluations_allowed: 0,
            evaluations_denied: 0,
            policies_created: 0,
            approvals_processed: 0,
            audit_entries: 0,
        };
        let err = m.validate().unwrap_err();
        assert!(err.contains("control or format characters"));
    }

    #[test]
    fn test_quota_status_validate_ok() {
        let q = QuotaStatus {
            tenant_id: "acme".to_string(),
            evaluations_used: 50,
            evaluations_limit: 100,
            evaluations_remaining: 50,
            policies_used: 5,
            policies_limit: 100,
            period_start: "2026-02-01T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            quota_exceeded: false,
        };
        assert!(q.validate().is_ok());
    }

    #[test]
    fn test_billing_period_validate_ok() {
        let bp = BillingPeriod {
            start: "2026-02-01T00:00:00Z".to_string(),
            end: "2026-03-01T00:00:00Z".to_string(),
        };
        assert!(bp.validate().is_ok());
    }

    #[test]
    fn test_billing_period_validate_empty_start() {
        let bp = BillingPeriod {
            start: String::new(),
            end: "2026-03-01T00:00:00Z".to_string(),
        };
        assert!(bp.validate().is_err());
    }

    #[test]
    fn test_usage_summary_validate_ok() {
        let s = UsageSummary {
            tenant_id: "acme".to_string(),
            periods: vec![],
            total_evaluations: 0,
        };
        assert!(s.validate().is_ok());
    }

    #[test]
    fn test_usage_summary_validate_too_many_periods() {
        let periods: Vec<UsageMetrics> = (0..=MAX_USAGE_PERIODS)
            .map(|i| UsageMetrics {
                tenant_id: "acme".to_string(),
                period_start: format!("2026-{:02}-01T00:00:00Z", (i % 12) + 1),
                period_end: format!("2026-{:02}-01T00:00:00Z", (i % 12) + 1),
                evaluations: 0,
                evaluations_allowed: 0,
                evaluations_denied: 0,
                policies_created: 0,
                approvals_processed: 0,
                audit_entries: 0,
            })
            .collect();
        let s = UsageSummary {
            tenant_id: "acme".to_string(),
            periods,
            total_evaluations: 0,
        };
        let err = s.validate().unwrap_err();
        assert!(err.contains("max is"));
    }

    #[test]
    fn test_usage_metrics_serde_roundtrip() {
        let m = UsageMetrics {
            tenant_id: "acme".to_string(),
            period_start: "2026-02-01T00:00:00Z".to_string(),
            period_end: "2026-03-01T00:00:00Z".to_string(),
            evaluations: 42,
            evaluations_allowed: 30,
            evaluations_denied: 12,
            policies_created: 2,
            approvals_processed: 1,
            audit_entries: 42,
        };
        let json = serde_json::to_string(&m).expect("serialize");
        let parsed: UsageMetrics = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(parsed.tenant_id, "acme");
        assert_eq!(parsed.evaluations, 42);
    }

    #[test]
    fn test_usage_metrics_deny_unknown_fields() {
        let json = r#"{"tenant_id":"acme","period_start":"2026-02-01","period_end":"2026-03-01","evaluations":0,"evaluations_allowed":0,"evaluations_denied":0,"policies_created":0,"approvals_processed":0,"audit_entries":0,"unknown":"bad"}"#;
        let result: Result<UsageMetrics, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_quota_status_deny_unknown_fields() {
        let json = r#"{"tenant_id":"acme","evaluations_used":0,"evaluations_limit":0,"evaluations_remaining":0,"policies_used":0,"policies_limit":0,"period_start":"x","period_end":"x","quota_exceeded":false,"extra":"bad"}"#;
        let result: Result<QuotaStatus, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_metering_tenant_id_underscore_allowed() {
        // Underscores and hyphens are allowed in tenant IDs
        assert!(validate_metering_tenant_id("my_tenant-1").is_ok());
    }

    #[test]
    fn test_validate_metering_timestamp_too_long() {
        let long_ts = "x".repeat(MAX_TIMESTAMP_LEN + 1);
        let err = validate_metering_timestamp(&long_ts, "test").unwrap_err();
        assert!(err.contains("exceeds max"));
    }
}