cuke_dedup/source_adapter/
finalization.rs1use super::SourceLanguage;
4use crate::model::{SourceLocation, StepDefinition};
5use std::path::PathBuf;
6
7#[non_exhaustive]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum UncertaintyScope {
11 Definition(SourceLocation),
13 File(PathBuf),
15 Registry(SourceLanguage),
17}
18
19#[non_exhaustive]
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum UncertaintyCause {
23 Registration,
25 Handler,
27 Source,
29}
30
31#[non_exhaustive]
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct SourceUncertainty {
35 pub scope: UncertaintyScope,
37 pub cause: UncertaintyCause,
39 pub message: String,
41}
42
43impl SourceUncertainty {
44 pub fn new(
46 scope: UncertaintyScope,
47 cause: UncertaintyCause,
48 message: impl Into<String>,
49 ) -> Self {
50 Self {
51 scope,
52 cause,
53 message: message.into(),
54 }
55 }
56
57 fn affects(&self, definition: &StepDefinition) -> bool {
58 match &self.scope {
59 UncertaintyScope::Definition(location) => &definition.location == location,
60 UncertaintyScope::File(path) => &definition.location.path == path,
61 UncertaintyScope::Registry(language) => {
62 definition.analysis_profile().comparison_domain == language.comparison_domain()
63 }
64 }
65 }
66}
67
68#[non_exhaustive]
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct SourceAdvisory {
72 pub location: SourceLocation,
74 pub message: String,
76}
77
78impl SourceAdvisory {
79 pub fn new(location: SourceLocation, message: impl Into<String>) -> Self {
81 Self {
82 location,
83 message: message.into(),
84 }
85 }
86}
87
88#[non_exhaustive]
90#[derive(Debug, Clone, Default, PartialEq, Eq)]
91pub struct SourceFinalization {
92 pub uncertainties: Vec<SourceUncertainty>,
94 pub advisories: Vec<SourceAdvisory>,
97}
98
99impl SourceFinalization {
100 pub fn apply(&self, definitions: &mut Vec<StepDefinition>) {
102 for uncertainty in &self.uncertainties {
103 match uncertainty.cause {
104 UncertaintyCause::Registration => {
105 definitions.retain(|definition| !uncertainty.affects(definition))
106 }
107 UncertaintyCause::Handler => {
108 for definition in definitions
109 .iter_mut()
110 .filter(|definition| uncertainty.affects(definition))
111 {
112 definition.handler.comparable = false;
113 }
114 }
115 UncertaintyCause::Source => {}
116 }
117 }
118 }
119}