11use serde:: Serialize ;
22
3- #[ derive( Debug , Clone , Serialize ) ]
4- #[ serde( rename_all = "kebab-case" ) ]
5- pub enum CheckKind {
6- NotInherited ,
7- VersionMismatch ,
8- PromotionCandidate ,
9- }
10-
113#[ derive( Debug , Clone , PartialEq , Eq , Serialize ) ]
124#[ serde( rename_all = "lowercase" ) ]
135pub enum Severity {
@@ -18,44 +10,63 @@ pub enum Severity {
1810#[ derive( Debug , Clone , Serialize ) ]
1911pub struct Diagnostic {
2012 pub severity : Severity ,
21- pub check : CheckKind ,
2213 pub dependency : String ,
23- #[ serde( skip_serializing_if = "Option::is_none" ) ]
24- pub version : Option < String > ,
25- #[ serde( skip_serializing_if = "Option::is_none" ) ]
26- pub member : Option < String > ,
27- #[ serde( skip_serializing_if = "Option::is_none" ) ]
28- pub workspace_version : Option < String > ,
29- #[ serde( skip_serializing_if = "Option::is_none" ) ]
30- pub count : Option < usize > ,
31- #[ serde( skip_serializing_if = "Option::is_none" ) ]
32- pub members : Option < Vec < String > > ,
33- #[ serde( skip_serializing_if = "Option::is_none" ) ]
34- pub suggested_version : Option < String > ,
14+ #[ serde( flatten) ]
15+ pub kind : DiagnosticKind ,
16+ }
17+
18+ #[ derive( Debug , Clone , Serialize ) ]
19+ #[ serde( tag = "check" , rename_all = "kebab-case" ) ]
20+ pub enum DiagnosticKind {
21+ NotInherited {
22+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
23+ version : Option < String > ,
24+ member : String ,
25+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
26+ workspace_version : Option < String > ,
27+ } ,
28+ VersionMismatch {
29+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
30+ version : Option < String > ,
31+ member : String ,
32+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
33+ workspace_version : Option < String > ,
34+ } ,
35+ PromotionCandidate {
36+ count : usize ,
37+ members : Vec < String > ,
38+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
39+ suggested_version : Option < String > ,
40+ } ,
3541}
3642
3743impl Diagnostic {
3844 pub fn format_human ( & self ) -> String {
39- match self . check {
40- CheckKind :: NotInherited => {
41- let ver = self . version . as_deref ( ) . unwrap_or ( "?" ) ;
42- let member = self . member . as_deref ( ) . unwrap_or ( "?" ) ;
45+ match & self . kind {
46+ DiagnosticKind :: NotInherited { version, member, .. } => {
47+ let ver = version. as_deref ( ) . unwrap_or ( "?" ) ;
4348 format ! (
4449 "error: `{dep} = \" {ver}\" ` in {member} should use `{dep} = {{ workspace = true }}`" ,
4550 dep = self . dependency,
4651 )
4752 }
48- CheckKind :: VersionMismatch => {
49- let ver = self . version . as_deref ( ) . unwrap_or ( "?" ) ;
50- let member = self . member . as_deref ( ) . unwrap_or ( "?" ) ;
51- let ws_ver = self . workspace_version . as_deref ( ) . unwrap_or ( "?" ) ;
53+ DiagnosticKind :: VersionMismatch {
54+ version,
55+ member,
56+ workspace_version,
57+ } => {
58+ let ver = version. as_deref ( ) . unwrap_or ( "?" ) ;
59+ let ws_ver = workspace_version. as_deref ( ) . unwrap_or ( "?" ) ;
5260 format ! (
5361 "error: `{dep} = \" {ver}\" ` in {member} has a different version than workspace `{dep} = \" {ws_ver}\" `" ,
5462 dep = self . dependency,
5563 )
5664 }
57- CheckKind :: PromotionCandidate => {
58- let count = self . count . unwrap_or ( 0 ) ;
65+ DiagnosticKind :: PromotionCandidate {
66+ count,
67+ members,
68+ suggested_version,
69+ } => {
5970 let severity = match self . severity {
6071 Severity :: Error => "error" ,
6172 Severity :: Warning => "warning" ,
@@ -64,12 +75,10 @@ impl Diagnostic {
6475 "{severity}: `{}` appears in {count} crates but is not in [workspace.dependencies]" ,
6576 self . dependency,
6677 ) ] ;
67- if let Some ( members) = & self . members {
68- for m in members {
69- lines. push ( format ! ( " --> {m}" ) ) ;
70- }
78+ for m in members {
79+ lines. push ( format ! ( " --> {m}" ) ) ;
7180 }
72- if let Some ( ver) = & self . suggested_version {
81+ if let Some ( ver) = suggested_version {
7382 lines. push ( format ! (
7483 " hint: consider adding `{} = \" {}\" ` to [workspace.dependencies]" ,
7584 self . dependency, ver,
@@ -140,14 +149,12 @@ mod tests {
140149 fn test_not_inherited_human_format ( ) {
141150 let d = Diagnostic {
142151 severity : Severity :: Error ,
143- check : CheckKind :: NotInherited ,
144152 dependency : "lru" . into ( ) ,
145- version : Some ( "0.12" . into ( ) ) ,
146- member : Some ( "crates/crypto/Cargo.toml" . into ( ) ) ,
147- workspace_version : Some ( "0.12" . into ( ) ) ,
148- count : None ,
149- members : None ,
150- suggested_version : None ,
153+ kind : DiagnosticKind :: NotInherited {
154+ version : Some ( "0.12" . into ( ) ) ,
155+ member : "crates/crypto/Cargo.toml" . into ( ) ,
156+ workspace_version : Some ( "0.12" . into ( ) ) ,
157+ } ,
151158 } ;
152159 let output = d. format_human ( ) ;
153160 assert ! ( output. contains( "error:" ) ) ;
@@ -160,14 +167,12 @@ mod tests {
160167 fn test_version_mismatch_human_format ( ) {
161168 let d = Diagnostic {
162169 severity : Severity :: Error ,
163- check : CheckKind :: VersionMismatch ,
164170 dependency : "rand" . into ( ) ,
165- version : Some ( "0.7" . into ( ) ) ,
166- member : Some ( "crates/utils/Cargo.toml" . into ( ) ) ,
167- workspace_version : Some ( "0.8" . into ( ) ) ,
168- count : None ,
169- members : None ,
170- suggested_version : None ,
171+ kind : DiagnosticKind :: VersionMismatch {
172+ version : Some ( "0.7" . into ( ) ) ,
173+ member : "crates/utils/Cargo.toml" . into ( ) ,
174+ workspace_version : Some ( "0.8" . into ( ) ) ,
175+ } ,
171176 } ;
172177 let output = d. format_human ( ) ;
173178 assert ! ( output. contains( "error:" ) ) ;
@@ -180,17 +185,15 @@ mod tests {
180185 fn test_promotion_candidate_human_format ( ) {
181186 let d = Diagnostic {
182187 severity : Severity :: Warning ,
183- check : CheckKind :: PromotionCandidate ,
184188 dependency : "serde_yaml" . into ( ) ,
185- version : None ,
186- member : None ,
187- workspace_version : None ,
188- count : Some ( 3 ) ,
189- members : Some ( vec ! [
190- "crates/config/Cargo.toml" . into( ) ,
191- "crates/node/Cargo.toml" . into( ) ,
192- ] ) ,
193- suggested_version : Some ( "0.9" . into ( ) ) ,
189+ kind : DiagnosticKind :: PromotionCandidate {
190+ count : 3 ,
191+ members : vec ! [
192+ "crates/config/Cargo.toml" . into( ) ,
193+ "crates/node/Cargo.toml" . into( ) ,
194+ ] ,
195+ suggested_version : Some ( "0.9" . into ( ) ) ,
196+ } ,
194197 } ;
195198 let output = d. format_human ( ) ;
196199 assert ! ( output. contains( "warning:" ) ) ;
@@ -203,14 +206,12 @@ mod tests {
203206 fn test_report_summary_human ( ) {
204207 let report = DiagnosticReport :: new ( vec ! [ Diagnostic {
205208 severity: Severity :: Error ,
206- check: CheckKind :: NotInherited ,
207209 dependency: "lru" . into( ) ,
208- version: Some ( "0.12" . into( ) ) ,
209- member: Some ( "crates/crypto/Cargo.toml" . into( ) ) ,
210- workspace_version: Some ( "0.12" . into( ) ) ,
211- count: None ,
212- members: None ,
213- suggested_version: None ,
210+ kind: DiagnosticKind :: NotInherited {
211+ version: Some ( "0.12" . into( ) ) ,
212+ member: "crates/crypto/Cargo.toml" . into( ) ,
213+ workspace_version: Some ( "0.12" . into( ) ) ,
214+ } ,
214215 } ] ) ;
215216 let output = report. format_human ( ) ;
216217 assert ! ( output. contains( "1 error" ) ) ;
@@ -220,14 +221,12 @@ mod tests {
220221 fn test_report_json ( ) {
221222 let report = DiagnosticReport :: new ( vec ! [ Diagnostic {
222223 severity: Severity :: Error ,
223- check: CheckKind :: NotInherited ,
224224 dependency: "lru" . into( ) ,
225- version: Some ( "0.12" . into( ) ) ,
226- member: Some ( "crates/crypto/Cargo.toml" . into( ) ) ,
227- workspace_version: Some ( "0.12" . into( ) ) ,
228- count: None ,
229- members: None ,
230- suggested_version: None ,
225+ kind: DiagnosticKind :: NotInherited {
226+ version: Some ( "0.12" . into( ) ) ,
227+ member: "crates/crypto/Cargo.toml" . into( ) ,
228+ workspace_version: Some ( "0.12" . into( ) ) ,
229+ } ,
231230 } ] ) ;
232231 let json = report. format_json ( ) ;
233232 let parsed: serde_json:: Value = serde_json:: from_str ( & json) . unwrap ( ) ;
0 commit comments