dpp-rules 0.2.0

Pure EU ESPR cross-field regulatory rules, shared by dpp-domain and the Wasm sector plugins
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
//! Battery recycled content validation — EU Regulation 2023/1542, Art. 8 + Annex X.
//!
//! Art. 8 and Annex X set minimum recycled content targets for four metals.
//! Phase 1 (from 18 Aug 2031) covers **EV batteries, SLI batteries, and
//! industrial batteries with a capacity > 2 kWh** (excluding those with
//! exclusively external storage). **LMT batteries** join only in Phase 2 (from
//! **18 Aug 2036**), at the higher targets. Portable batteries are out of scope.
//!
//! The targets are **finalized law** — they are in the regulation text itself,
//! not in a pending delegated act. However, neither phase is yet in force.
//! The battery plugin therefore returns `NOT_ASSESSED` today; these constants
//! are the single source of truth that the plugin will check against once
//! enforcement begins.
//!
//! ## Phase 1 — EV + SLI + industrial > 2 kWh, from **18 Aug 2031** (Art. 8)
//! | Material | Minimum % |
//! |----------|-----------|
//! | Cobalt   |      16 % |
//! | Lead     |      85 % |
//! | Lithium  |       6 % |
//! | Nickel   |       6 % |
//!
//! ## Phase 2 — Phase 1 categories + **LMT**, from **18 Aug 2036** (Art. 8)
//! | Material | Minimum % |
//! |----------|-----------|
//! | Cobalt   |      26 % |
//! | Lead     |      85 % |
//! | Lithium  |      12 % |
//! | Nickel   |      15 % |

use alloc::vec::Vec;

// ✅ COMPLIANCE-PIN: EU 2023/1542, Art. 8 + Annex X (OJ L 2023/1542, 28 Jul 2023)
// Percentages: verified correct per Annex X.
// Phase-1 date: 18 Aug 2031. Phase-2 date: 18 Aug 2036.
// Category scope (corrected 2026-06-22, audit H-2): Phase 1 = EV + SLI + industrial
// > 2 kWh (excl. exclusively-external-storage); LMT batteries join only in Phase 2
// (LMT minimum content from 18 Aug 2036). SLI is **in** Phase-1 scope — a prior note
// here wrongly excluded it. Reconciled against multiple authoritative secondary
// sources (White & Case, EUR-Lex summary, GLEIF-independent battery guidance); the
// 🟠 residual is verbatim OJ Art. 8(2)/(3) confirmation, blocked here by EUR-Lex
// JavaScript rendering. Numeric percentages/dates are not in dispute.

// ── Phase 1 constants — EV + industrial ≥ 2 kWh from 18 Aug 2031 ─────────────

/// Minimum cobalt recycled content — Art. 8 + Annex X Phase 1, from 18 Aug 2031.
pub const COBALT_RECYCLED_PCT_2031: f64 = 16.0;
/// Minimum lead recycled content — Art. 8 + Annex X Phase 1, from 18 Aug 2031.
pub const LEAD_RECYCLED_PCT_2031: f64 = 85.0;
/// Minimum lithium recycled content — Art. 8 + Annex X Phase 1, from 18 Aug 2031.
pub const LITHIUM_RECYCLED_PCT_2031: f64 = 6.0;
/// Minimum nickel recycled content — Art. 8 + Annex X Phase 1, from 18 Aug 2031.
pub const NICKEL_RECYCLED_PCT_2031: f64 = 6.0;

// ── Phase 2 constants — EV + industrial ≥ 2 kWh + LMT from 18 Aug 2036 ───────

/// Minimum cobalt recycled content — Art. 8 + Annex X Phase 2, from 18 Aug 2036.
pub const COBALT_RECYCLED_PCT_2036: f64 = 26.0;
/// Minimum lead recycled content — Art. 8 + Annex X Phase 2, from 18 Aug 2036.
pub const LEAD_RECYCLED_PCT_2036: f64 = 85.0;
/// Minimum lithium recycled content — Art. 8 + Annex X Phase 2, from 18 Aug 2036.
pub const LITHIUM_RECYCLED_PCT_2036: f64 = 12.0;
/// Minimum nickel recycled content — Art. 8 + Annex X Phase 2, from 18 Aug 2036.
pub const NICKEL_RECYCLED_PCT_2036: f64 = 15.0;

// ── Input type ────────────────────────────────────────────────────────────────

/// Declared recycled content percentages for the four regulated metals.
///
/// `None` means the metal is absent or undeclared — it is skipped in target
/// checks. Only declared values can fail a target check.
#[derive(Debug, Clone, Copy)]
pub struct RecycledContentInput {
    pub cobalt_pct: Option<f64>,
    pub lithium_pct: Option<f64>,
    pub nickel_pct: Option<f64>,
    pub lead_pct: Option<f64>,
}

/// A recycled-content shortfall for a single material.
#[derive(Debug, Clone, Copy)]
pub struct RecycledContentShortfall {
    pub material: &'static str,
    pub declared_pct: f64,
    pub required_pct: f64,
}

// ── Phase-check functions ─────────────────────────────────────────────────────

/// Check declared recycled content against Annex X Phase 1 targets (from 2031).
///
/// Returns every material whose declared percentage falls below the Phase 1
/// minimum. An empty `Vec` means all declared metals pass. Undeclared metals
/// are not checked — battery-type scoping (Phase 1: EV / SLI / industrial
/// > 2 kWh; LMT only from Phase 2) is the caller's responsibility.
#[must_use]
pub fn annex_x_shortfalls_2031(input: &RecycledContentInput) -> Vec<RecycledContentShortfall> {
    check_targets(
        input,
        COBALT_RECYCLED_PCT_2031,
        LEAD_RECYCLED_PCT_2031,
        LITHIUM_RECYCLED_PCT_2031,
        NICKEL_RECYCLED_PCT_2031,
    )
}

/// Check declared recycled content against Annex X Phase 2 targets (from 2036).
#[must_use]
pub fn annex_x_shortfalls_2036(input: &RecycledContentInput) -> Vec<RecycledContentShortfall> {
    check_targets(
        input,
        COBALT_RECYCLED_PCT_2036,
        LEAD_RECYCLED_PCT_2036,
        LITHIUM_RECYCLED_PCT_2036,
        NICKEL_RECYCLED_PCT_2036,
    )
}

fn check_targets(
    input: &RecycledContentInput,
    cobalt_req: f64,
    lead_req: f64,
    lithium_req: f64,
    nickel_req: f64,
) -> Vec<RecycledContentShortfall> {
    let mut out = Vec::new();
    if let Some(pct) = input.cobalt_pct {
        // Non-finite (NaN/Inf) cannot demonstrate compliance — treat as shortfall.
        if !pct.is_finite() || pct < cobalt_req {
            out.push(RecycledContentShortfall {
                material: "cobalt",
                declared_pct: pct,
                required_pct: cobalt_req,
            });
        }
    }
    if let Some(pct) = input.lead_pct
        && (!pct.is_finite() || pct < lead_req)
    {
        out.push(RecycledContentShortfall {
            material: "lead",
            declared_pct: pct,
            required_pct: lead_req,
        });
    }
    if let Some(pct) = input.lithium_pct
        && (!pct.is_finite() || pct < lithium_req)
    {
        out.push(RecycledContentShortfall {
            material: "lithium",
            declared_pct: pct,
            required_pct: lithium_req,
        });
    }
    if let Some(pct) = input.nickel_pct
        && (!pct.is_finite() || pct < nickel_req)
    {
        out.push(RecycledContentShortfall {
            material: "nickel",
            declared_pct: pct,
            required_pct: nickel_req,
        });
    }
    out
}

// ── Chemistry → regulated-metal applicability ──────────────────────────────────

/// The Annex X regulated metals (cobalt, lithium, nickel, lead) that are
/// *meaningfully present* for a given battery chemistry.
///
/// Used to scope recycled-content checks so a chemistry that does not contain a
/// metal is never flagged for that metal's "shortfall" — e.g. an LFP cell
/// (LiFePO₄, no cobalt or nickel) must not produce a cobalt shortfall just
/// because the field defaulted to `0.0`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RegulatedMetals {
    pub cobalt: bool,
    pub lithium: bool,
    pub nickel: bool,
    pub lead: bool,
}

/// Map a battery chemistry code (e.g. `"LFP"`, `"NMC"`, `"lead-acid"`) to the
/// Annex X regulated metals it contains.
///
/// Matching is case-insensitive. Unknown chemistries return **all `true`**
/// (conservative: every declared value is checked, since we cannot rule a metal
/// out). The caller still skips any metal whose declared percentage is absent.
#[must_use]
pub fn chemistry_regulated_metals(chemistry: &str) -> RegulatedMetals {
    let c = chemistry.trim();
    let eq = |s: &str| c.eq_ignore_ascii_case(s);
    if eq("LFP") {
        RegulatedMetals {
            cobalt: false,
            lithium: true,
            nickel: false,
            lead: false,
        }
    } else if eq("NMC") || eq("NCA") {
        RegulatedMetals {
            cobalt: true,
            lithium: true,
            nickel: true,
            lead: false,
        }
    } else if eq("LCO") {
        RegulatedMetals {
            cobalt: true,
            lithium: true,
            nickel: false,
            lead: false,
        }
    } else if eq("NiMH") || eq("NiCd") {
        RegulatedMetals {
            cobalt: false,
            lithium: false,
            nickel: true,
            lead: false,
        }
    } else if eq("lead-acid") {
        RegulatedMetals {
            cobalt: false,
            lithium: false,
            nickel: false,
            lead: true,
        }
    } else if eq("solid-state") {
        RegulatedMetals {
            cobalt: false,
            lithium: true,
            nickel: false,
            lead: false,
        }
    } else {
        // Unknown chemistry — cannot exclude any metal; check whatever is declared.
        RegulatedMetals {
            cobalt: true,
            lithium: true,
            nickel: true,
            lead: true,
        }
    }
}

/// Metals whose recycled content is declared **> 0** but which the chemistry
/// does **not** contain — a data-integrity contradiction (e.g. cobalt recycled
/// content on an LFP cell, which has no cobalt).
///
/// A declared `0.0` is *not* a conflict (it states "no recycled content", which
/// is trivially true for an absent metal). Unknown chemistries contain every
/// metal per [`chemistry_regulated_metals`], so they never conflict.
#[must_use]
pub fn recycled_content_chemistry_conflicts(
    chemistry: &str,
    cobalt_pct: Option<f64>,
    lithium_pct: Option<f64>,
    nickel_pct: Option<f64>,
    lead_pct: Option<f64>,
) -> Vec<&'static str> {
    let reg = chemistry_regulated_metals(chemistry);
    let positive = |v: Option<f64>| matches!(v, Some(x) if x.is_finite() && x > 0.0);
    let mut out = Vec::new();
    if positive(cobalt_pct) && !reg.cobalt {
        out.push("cobalt");
    }
    if positive(lithium_pct) && !reg.lithium {
        out.push("lithium");
    }
    if positive(nickel_pct) && !reg.nickel {
        out.push("nickel");
    }
    if positive(lead_pct) && !reg.lead {
        out.push("lead");
    }
    out
}

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

    fn all_metals(co: f64, pb: f64, li: f64, ni: f64) -> RecycledContentInput {
        RecycledContentInput {
            cobalt_pct: Some(co),
            lead_pct: Some(pb),
            lithium_pct: Some(li),
            nickel_pct: Some(ni),
        }
    }

    #[test]
    fn exactly_at_2031_targets_passes() {
        let input = all_metals(16.0, 85.0, 6.0, 6.0);
        assert!(annex_x_shortfalls_2031(&input).is_empty());
    }

    #[test]
    fn above_2031_targets_passes() {
        let input = all_metals(20.0, 90.0, 10.0, 10.0);
        assert!(annex_x_shortfalls_2031(&input).is_empty());
    }

    #[test]
    fn below_2031_cobalt_flagged() {
        let input = all_metals(15.0, 85.0, 6.0, 6.0); // cobalt 15 < 16
        let shortfalls = annex_x_shortfalls_2031(&input);
        assert_eq!(shortfalls.len(), 1);
        assert_eq!(shortfalls[0].material, "cobalt");
        assert_eq!(shortfalls[0].required_pct, 16.0);
    }

    #[test]
    fn multiple_shortfalls_all_returned() {
        let input = all_metals(10.0, 80.0, 3.0, 4.0); // all below
        assert_eq!(annex_x_shortfalls_2031(&input).len(), 4);
    }

    #[test]
    fn undeclared_metals_not_flagged() {
        let input = RecycledContentInput {
            cobalt_pct: Some(20.0),
            lead_pct: None,
            lithium_pct: None,
            nickel_pct: None,
        };
        assert!(annex_x_shortfalls_2031(&input).is_empty());
    }

    #[test]
    fn phase2_stricter_than_phase1() {
        // 16% cobalt passes 2031 but fails 2036 (target 26%)
        let input = all_metals(16.0, 85.0, 6.0, 6.0);
        assert!(annex_x_shortfalls_2031(&input).is_empty());
        let shortfalls = annex_x_shortfalls_2036(&input);
        assert!(shortfalls.iter().any(|s| s.material == "cobalt"));
    }

    #[test]
    fn nan_cobalt_treated_as_shortfall() {
        let input = RecycledContentInput {
            cobalt_pct: Some(f64::NAN),
            lead_pct: None,
            lithium_pct: None,
            nickel_pct: None,
        };
        let shortfalls = annex_x_shortfalls_2031(&input);
        assert_eq!(shortfalls.len(), 1);
        assert_eq!(shortfalls[0].material, "cobalt");
    }

    #[test]
    fn infinity_cobalt_treated_as_shortfall() {
        let input = RecycledContentInput {
            cobalt_pct: Some(f64::INFINITY),
            lead_pct: None,
            lithium_pct: None,
            nickel_pct: None,
        };
        let shortfalls = annex_x_shortfalls_2031(&input);
        assert_eq!(shortfalls.len(), 1);
        assert_eq!(shortfalls[0].material, "cobalt");
    }

    #[test]
    fn lfp_regulates_lithium_only() {
        let m = chemistry_regulated_metals("LFP");
        assert!(m.lithium);
        assert!(!m.cobalt && !m.nickel && !m.lead);
        // case-insensitive
        assert_eq!(chemistry_regulated_metals("lfp"), m);
    }

    #[test]
    fn nmc_and_nca_regulate_cobalt_lithium_nickel() {
        for chem in ["NMC", "NCA"] {
            let m = chemistry_regulated_metals(chem);
            assert!(m.cobalt && m.lithium && m.nickel);
            assert!(!m.lead);
        }
    }

    #[test]
    fn lead_acid_regulates_lead_only() {
        let m = chemistry_regulated_metals("lead-acid");
        assert!(m.lead);
        assert!(!m.cobalt && !m.lithium && !m.nickel);
    }

    #[test]
    fn unknown_chemistry_checks_all_metals() {
        let m = chemistry_regulated_metals("mystery-cell");
        assert!(m.cobalt && m.lithium && m.nickel && m.lead);
    }

    #[test]
    fn positive_cobalt_on_lfp_is_a_conflict() {
        let c = recycled_content_chemistry_conflicts("LFP", Some(5.0), Some(12.0), None, None);
        assert_eq!(c.len(), 1);
        assert_eq!(c[0], "cobalt");
    }

    #[test]
    fn zero_cobalt_on_lfp_is_not_a_conflict() {
        // 0.0 declares "no recycled cobalt" — trivially true for an absent metal.
        let c = recycled_content_chemistry_conflicts("LFP", Some(0.0), Some(12.0), Some(0.0), None);
        assert!(c.is_empty(), "got: {c:?}");
    }

    #[test]
    fn nmc_cobalt_and_nickel_declared_no_conflict() {
        let c = recycled_content_chemistry_conflicts("NMC", Some(16.0), Some(6.0), Some(8.0), None);
        assert!(c.is_empty(), "got: {c:?}");
    }

    #[test]
    fn lead_declared_on_lfp_is_a_conflict() {
        let c = recycled_content_chemistry_conflicts("LFP", None, Some(12.0), None, Some(80.0));
        assert_eq!(c.len(), 1);
        assert_eq!(c[0], "lead");
    }

    #[test]
    fn unknown_chemistry_never_conflicts() {
        let c = recycled_content_chemistry_conflicts(
            "mystery",
            Some(5.0),
            Some(5.0),
            Some(5.0),
            Some(5.0),
        );
        assert!(c.is_empty());
    }
}