zedazo-core 0.5.1

Núcleo de dominio y casos de uso de Zedazo (sin CLI/HTTP)
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
//! Contrato de procesamiento compartido CLI/API (ADR-0015).

use std::collections::BTreeSet;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::application::cribar;
use crate::application::stats::Stats;
use crate::domain::audit::AuditEntry;
use crate::domain::contact::Contact;
use crate::domain::screening::ScreeningDecision;
use crate::error::CribaError;
use crate::infrastructure::csv_writer::export_csv;
use crate::infrastructure::json_writer::export_json;

/// Identificador opaco de trabajo (ULID/UUID string).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct JobId(pub String);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactKind {
    Vcf,
    AuditTsv,
    StatsJson,
    StatsMarkdown,
    Csv,
    Json,
}

#[derive(Debug, Clone)]
pub enum RulesConfigSource {
    BuiltIn,
    TomlPath(PathBuf),
    TomlInline(String),
}

/// Señal cooperativa de cancelación (sin Tokio en el core).
#[derive(Debug, Default, Clone)]
pub struct CancellationToken {
    cancelled: Arc<AtomicBool>,
}

impl CancellationToken {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::SeqCst);
    }

    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::SeqCst)
    }

    pub fn check(&self) -> Result<(), Cancelled> {
        if self.is_cancelled() {
            Err(Cancelled)
        } else {
            Ok(())
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cancelled;

impl std::fmt::Display for Cancelled {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "procesamiento cancelado")
    }
}

impl std::error::Error for Cancelled {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProcessPhase {
    Validating,
    Parsing,
    Screening,
    Normalizing,
    Classifying,
    Deduplicating,
    Verifying,
    WritingArtifacts,
    Completed,
    Failed,
    Cancelled,
}

pub trait ProgressReporter: Send + Sync {
    fn phase(&self, phase: ProcessPhase);
    fn metric(&self, name: &str, value: u64);
    fn message(&self, msg: &str);
}

#[derive(Debug, Default)]
pub struct NullProgress;

impl ProgressReporter for NullProgress {
    fn phase(&self, _phase: ProcessPhase) {}
    fn metric(&self, _name: &str, _value: u64) {}
    fn message(&self, _msg: &str) {}
}

#[derive(Debug, Default)]
pub struct TracingProgress;

impl ProgressReporter for TracingProgress {
    fn phase(&self, phase: ProcessPhase) {
        tracing::info!(?phase, "fase");
    }
    fn metric(&self, name: &str, value: u64) {
        tracing::info!(%name, value, "métrica");
    }
    fn message(&self, msg: &str) {
        tracing::info!("{msg}");
    }
}

pub struct ProcessRequest {
    pub job_id: JobId,
    pub input_path: PathBuf,
    pub working_directory: PathBuf,
    pub config: RulesConfigSource,
    pub requested_artifacts: BTreeSet<ArtifactKind>,
    pub source_override: String,
    pub dry_run: bool,
    pub strict: bool,
    pub cancellation: CancellationToken,
    pub progress: Arc<dyn ProgressReporter>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessSummary {
    pub input_contacts: usize,
    pub retained: usize,
    pub needs_review: usize,
    pub eliminated: usize,
    pub quarantine: usize,
    pub duplicate_groups: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessWarning {
    pub code: String,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArtifactDescriptor {
    pub kind: ArtifactKind,
    pub path: PathBuf,
    pub bytes: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContactView {
    pub uid: String,
    pub fn_value: String,
    pub result: String,
    pub screening_rule: Option<String>,
    pub categories: Vec<String>,
    pub emails: Vec<String>,
    pub tels: Vec<String>,
    pub merged_uids: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DuplicateGroupView {
    pub canonical_uid: String,
    pub member_uids: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEventView {
    pub uid: String,
    pub action: String,
    pub rule: String,
    pub evidence: String,
}

#[derive(Debug)]
pub struct ProcessResult {
    pub summary: ProcessSummary,
    pub stats: Stats,
    pub contacts: Vec<Contact>,
    pub contact_views: Vec<ContactView>,
    pub duplicate_groups: Vec<DuplicateGroupView>,
    pub audit_events: Vec<AuditEventView>,
    pub audit_entries: Vec<AuditEntry>,
    pub artifacts: Vec<ArtifactDescriptor>,
    pub warnings: Vec<ProcessWarning>,
}

impl ContactView {
    pub fn from_contact(c: &Contact) -> Self {
        let result = match &c.decision {
            crate::domain::screening::ScreeningDecision::Conserved => "conserved",
            crate::domain::screening::ScreeningDecision::Eliminated(_) => "eliminated",
            crate::domain::screening::ScreeningDecision::NeedsReview(_) => "needs_review",
            crate::domain::screening::ScreeningDecision::Quarantine(_) => "quarantine",
        }
        .to_string();
        let mut categories: Vec<String> = c.categories.n1.iter().cloned().collect();
        categories.extend(c.categories.n2.iter().cloned());
        categories.extend(c.categories.n3.iter().cloned());
        categories.sort();
        Self {
            uid: c.uid.clone(),
            fn_value: c.fn_value.clone(),
            result,
            screening_rule: if c.screening_rule.is_empty() {
                None
            } else {
                Some(c.screening_rule.clone())
            },
            categories,
            emails: c.emails.iter().map(|e| e.value.clone()).collect(),
            tels: c.tels.iter().map(|t| t.value.clone()).collect(),
            merged_uids: c.merged_uids.clone(),
        }
    }
}

impl AuditEventView {
    pub fn from_entry(e: &AuditEntry) -> Self {
        let action = match e.action {
            crate::domain::audit::AuditAction::Conserved => "conserved",
            crate::domain::audit::AuditAction::Eliminated => "eliminated",
            crate::domain::audit::AuditAction::Quarantine => "quarantine",
            crate::domain::audit::AuditAction::NeedsReview => "needs_review",
            crate::domain::audit::AuditAction::Merged => "merged",
        };
        Self {
            uid: e.uid.clone(),
            action: action.to_string(),
            rule: e.rule.clone(),
            evidence: e.reason.clone(),
        }
    }
}

fn cancelled_err() -> CribaError {
    CribaError::Io(std::io::Error::new(
        std::io::ErrorKind::Interrupted,
        "cancelado",
    ))
}

/// Ejecuta el pipeline vía cribar y materializa artefactos solicitados en `working_directory`.
pub fn run(req: &ProcessRequest) -> Result<ProcessResult, CribaError> {
    req.cancellation.check().map_err(|_| cancelled_err())?;
    req.progress.phase(ProcessPhase::Validating);
    fs::create_dir_all(&req.working_directory)?;

    let config_path = match &req.config {
        RulesConfigSource::BuiltIn => None,
        RulesConfigSource::TomlPath(p) => Some(p.clone()),
        RulesConfigSource::TomlInline(s) => {
            let p = req.working_directory.join("config.toml");
            let mut f = fs::File::create(&p)?;
            f.write_all(s.as_bytes())?;
            Some(p)
        }
    };

    req.progress.phase(ProcessPhase::Parsing);
    req.cancellation.check().map_err(|_| cancelled_err())?;

    let out_vcf = req.working_directory.join("output.vcf");
    let audit_path = req.working_directory.join("audit.tsv");
    let want_vcf = req.requested_artifacts.contains(&ArtifactKind::Vcf);
    let want_audit = req.requested_artifacts.contains(&ArtifactKind::AuditTsv);

    let (stats, contacts) = cribar::execute(
        &req.input_path,
        if want_vcf && !req.dry_run {
            Some(out_vcf.as_path())
        } else {
            None
        },
        if want_audit && !req.dry_run {
            Some(audit_path.as_path())
        } else {
            None
        },
        config_path.as_deref(),
        &req.source_override,
        req.dry_run,
        req.strict,
    )?;

    req.progress.phase(ProcessPhase::WritingArtifacts);
    req.progress.metric("contacts", contacts.len() as u64);

    let mut warnings = Vec::new();
    if let Some(ref p) = config_path {
        if let Ok(cfg) = crate::infrastructure::config::load_config(Some(p)) {
            for w in cfg.warnings {
                warnings.push(ProcessWarning {
                    code: w.code,
                    message: w.message,
                });
            }
        }
    }
    let mut artifacts = Vec::new();
    let audit_entries: Vec<AuditEntry> = Vec::new();
    if audit_path.exists() {
        artifacts.push(ArtifactDescriptor {
            kind: ArtifactKind::AuditTsv,
            path: audit_path.clone(),
            bytes: fs::metadata(&audit_path).ok().map(|m| m.len()),
        });
    }
    if out_vcf.exists() {
        artifacts.push(ArtifactDescriptor {
            kind: ArtifactKind::Vcf,
            path: out_vcf.clone(),
            bytes: fs::metadata(&out_vcf).ok().map(|m| m.len()),
        });
    }

    if !req.dry_run && req.requested_artifacts.contains(&ArtifactKind::Csv) {
        let p = req.working_directory.join("contacts.csv");
        export_csv(&contacts, &p)?;
        artifacts.push(ArtifactDescriptor {
            kind: ArtifactKind::Csv,
            path: p,
            bytes: None,
        });
    }
    if !req.dry_run && req.requested_artifacts.contains(&ArtifactKind::Json) {
        let p = req.working_directory.join("contacts.json");
        export_json(&contacts, &p)?;
        artifacts.push(ArtifactDescriptor {
            kind: ArtifactKind::Json,
            path: p,
            bytes: None,
        });
    }
    if !req.dry_run && req.requested_artifacts.contains(&ArtifactKind::StatsJson) {
        let p = req.working_directory.join("stats.json");
        fs::write(&p, stats.to_json()?)?;
        artifacts.push(ArtifactDescriptor {
            kind: ArtifactKind::StatsJson,
            path: p,
            bytes: None,
        });
    }
    if !req.dry_run
        && req
            .requested_artifacts
            .contains(&ArtifactKind::StatsMarkdown)
    {
        let p = req.working_directory.join("stats.md");
        fs::write(&p, stats.to_markdown())?;
        artifacts.push(ArtifactDescriptor {
            kind: ArtifactKind::StatsMarkdown,
            path: p,
            bytes: None,
        });
    }

    let retained = contacts
        .iter()
        .filter(|c| matches!(c.decision, ScreeningDecision::Conserved))
        .count();
    let needs_review = contacts
        .iter()
        .filter(|c| matches!(c.decision, ScreeningDecision::NeedsReview(_)))
        .count();
    let eliminated = contacts
        .iter()
        .filter(|c| matches!(c.decision, ScreeningDecision::Eliminated(_)))
        .count();
    let quarantine = contacts
        .iter()
        .filter(|c| matches!(c.decision, ScreeningDecision::Quarantine(_)))
        .count();
    let duplicate_groups = contacts
        .iter()
        .filter(|c| !c.merged_uids.is_empty())
        .count();

    let contact_views: Vec<ContactView> = contacts.iter().map(ContactView::from_contact).collect();
    let duplicate_groups_view: Vec<DuplicateGroupView> = contacts
        .iter()
        .filter(|c| !c.merged_uids.is_empty())
        .map(|c| {
            let mut members = c.merged_uids.clone();
            members.push(c.uid.clone());
            DuplicateGroupView {
                canonical_uid: c.uid.clone(),
                member_uids: members,
            }
        })
        .collect();

    req.progress.phase(ProcessPhase::Completed);

    Ok(ProcessResult {
        summary: ProcessSummary {
            input_contacts: stats.total_entrada,
            retained,
            needs_review,
            eliminated,
            quarantine,
            duplicate_groups,
        },
        stats,
        contacts,
        contact_views,
        duplicate_groups: duplicate_groups_view,
        audit_events: audit_entries
            .iter()
            .map(AuditEventView::from_entry)
            .collect(),
        audit_entries,
        artifacts,
        warnings,
    })
}