9
9
use std:: result:: Result ;
10
10
use std:: convert:: From ;
11
11
use std:: io:: { Error as IOError } ;
12
+ use std:: fmt;
12
13
use parser:: { ParseError , RecordParseError } ;
13
14
use record:: { BranchData } ;
14
15
use report:: line:: { Line } ;
@@ -32,10 +33,44 @@ pub enum ChecksumError {
32
33
Mismatch ( MergeLine , MergeLine )
33
34
}
34
35
36
+ impl fmt:: Display for ChecksumError {
37
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
38
+ match self {
39
+ & ChecksumError :: Empty ( ref line) => {
40
+ write ! ( f, "No source code checksum: {}" , line)
41
+ } ,
42
+ & ChecksumError :: Mismatch ( ref line1, ref line2) => {
43
+ write ! ( f, "Source code checksums do not match: line: {}, left: {}, right: {}" ,
44
+ line1. line( ) ,
45
+ line1. checksum( ) ,
46
+ line2. checksum( ) )
47
+ }
48
+ }
49
+ }
50
+ }
51
+
35
52
#[ derive( Debug , PartialEq ) ]
36
53
pub struct MergeLine {
37
- pub line : LineNumber ,
38
- pub checksum : Option < CheckSum >
54
+ line : LineNumber ,
55
+ checksum : Option < CheckSum >
56
+ }
57
+
58
+ impl MergeLine {
59
+ pub fn new ( line_number : LineNumber , checksum : Option < CheckSum > ) -> MergeLine {
60
+ MergeLine {
61
+ line : line_number,
62
+ checksum : checksum
63
+ }
64
+ }
65
+ pub fn line ( & self ) -> & LineNumber {
66
+ & self . line
67
+ }
68
+ pub fn checksum ( & self ) -> & str {
69
+ match self . checksum {
70
+ Some ( ref checksum) => checksum. as_str ( ) ,
71
+ None => & ""
72
+ }
73
+ }
39
74
}
40
75
41
76
impl < ' a > From < & ' a Line > for MergeLine {
@@ -45,10 +80,13 @@ impl<'a> From<&'a Line> for MergeLine {
45
80
Some ( value) => Some ( value. clone ( ) ) ,
46
81
None => None
47
82
} ;
48
- MergeLine {
49
- line : line_number,
50
- checksum : checksum
51
- }
83
+ MergeLine :: new ( line_number, checksum)
84
+ }
85
+ }
86
+
87
+ impl fmt:: Display for MergeLine {
88
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
89
+ write ! ( f, "line: {}, checksum: {}" , self . line( ) , self . checksum( ) )
52
90
}
53
91
}
54
92
@@ -57,6 +95,16 @@ pub enum FunctionError {
57
95
Mismatch ( FunctionName , FunctionName )
58
96
}
59
97
98
+ impl fmt:: Display for FunctionError {
99
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
100
+ match self {
101
+ & FunctionError :: Mismatch ( ref func1, ref func2) => {
102
+ write ! ( f, "Function name mismatch: left = {}, right = {}" , func1, func2)
103
+ }
104
+ }
105
+ }
106
+ }
107
+
60
108
#[ derive( Debug , PartialEq ) ]
61
109
pub struct MergeBranch {
62
110
pub line : LineNumber ,
@@ -87,35 +135,47 @@ impl<'a> From<&'a BranchData> for MergeBranch {
87
135
}
88
136
}
89
137
138
+ impl fmt:: Display for MergeBranch {
139
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
140
+ write ! ( f, "line:{} block:{} branch:{}" , self . line, self . block, self . branch)
141
+ }
142
+ }
143
+
90
144
#[ derive( Debug , PartialEq ) ]
91
145
pub enum BranchError {
92
146
Mismatch ( MergeBranch , MergeBranch )
93
147
}
94
148
149
+ impl fmt:: Display for BranchError {
150
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
151
+ match self {
152
+ & BranchError :: Mismatch ( ref branch1, ref branch2) => {
153
+ write ! ( f, "Branch mismatch: left = {}, right = {}" , branch1, branch2)
154
+ }
155
+ }
156
+ }
157
+ }
158
+
95
159
#[ derive( Debug , PartialEq ) ]
96
160
pub enum TestError {
97
161
Checksum ( ChecksumError ) ,
98
162
Function ( FunctionError ) ,
99
163
Branch ( BranchError )
100
164
}
101
165
102
- impl From < ChecksumError > for TestError {
103
- fn from ( error : ChecksumError ) -> Self {
104
- TestError :: Checksum ( error)
166
+ impl fmt:: Display for TestError {
167
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
168
+ match self {
169
+ & TestError :: Checksum ( ref err) => write ! ( f, "{}" , err) ,
170
+ & TestError :: Function ( ref err) => write ! ( f, "{}" , err) ,
171
+ & TestError :: Branch ( ref err) => write ! ( f, "{}" , err)
172
+ }
105
173
}
106
174
}
107
175
108
- impl From < FunctionError > for TestError {
109
- fn from ( error : FunctionError ) -> Self {
110
- TestError :: Function ( error)
111
- }
112
- }
113
-
114
- impl From < BranchError > for TestError {
115
- fn from ( error : BranchError ) -> Self {
116
- TestError :: Branch ( error)
117
- }
118
- }
176
+ impl_from_error ! ( ChecksumError , TestError :: Checksum ) ;
177
+ impl_from_error ! ( FunctionError , TestError :: Function ) ;
178
+ impl_from_error ! ( BranchError , TestError :: Branch ) ;
119
179
120
180
#[ derive( Debug ) ]
121
181
pub enum MergeError {
@@ -124,29 +184,10 @@ pub enum MergeError {
124
184
Process ( TestError )
125
185
}
126
186
127
- impl From < IOError > for MergeError {
128
- fn from ( error : IOError ) -> Self {
129
- MergeError :: IO ( error)
130
- }
131
- }
132
-
133
- impl From < ChecksumError > for MergeError {
134
- fn from ( error : ChecksumError ) -> Self {
135
- MergeError :: Process ( TestError :: Checksum ( error) )
136
- }
137
- }
138
-
139
- impl From < FunctionError > for MergeError {
140
- fn from ( error : FunctionError ) -> Self {
141
- MergeError :: Process ( TestError :: Function ( error) )
142
- }
143
- }
144
-
145
- impl From < BranchError > for MergeError {
146
- fn from ( error : BranchError ) -> Self {
147
- MergeError :: Process ( TestError :: Branch ( error) )
148
- }
149
- }
187
+ impl_from_error ! ( IOError , MergeError :: IO ) ;
188
+ impl_from_error ! ( ChecksumError , TestError :: Checksum =>MergeError :: Process ) ;
189
+ impl_from_error ! ( FunctionError , TestError :: Function =>MergeError :: Process ) ;
190
+ impl_from_error ! ( BranchError , TestError :: Branch =>MergeError :: Process ) ;
150
191
151
192
impl From < ParseError > for MergeError {
152
193
fn from ( error : ParseError ) -> Self {
@@ -157,8 +198,40 @@ impl From<ParseError> for MergeError {
157
198
}
158
199
}
159
200
160
- impl From < TestError > for MergeError {
161
- fn from ( error : TestError ) -> Self {
162
- MergeError :: Process ( error)
201
+ impl_from_error ! ( TestError , MergeError :: Process ) ;
202
+
203
+ impl fmt:: Display for MergeError {
204
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
205
+ match self {
206
+ & MergeError :: IO ( ref err) => write ! ( f, "{}" , err) ,
207
+ & MergeError :: RecordParse ( ref err) => write ! ( f, "{}" , err) ,
208
+ & MergeError :: Process ( ref err) => write ! ( f, "{}" , err)
209
+ }
210
+ }
211
+ }
212
+
213
+
214
+
215
+ #[ cfg( test) ]
216
+ mod tests {
217
+ use merger:: ops:: { MergeError , TestError , ChecksumError , MergeLine } ;
218
+
219
+ #[ test]
220
+ fn merge_error_of_checksum_empty ( ) {
221
+ let merge_line = MergeLine :: new ( 1 , None ) ;
222
+ let checksum_error = ChecksumError :: Empty ( merge_line) ;
223
+ let test_error = TestError :: from ( checksum_error) ;
224
+ let merge_error = MergeError :: from ( test_error) ;
225
+ assert_eq ! ( merge_error. to_string( ) , "No source code checksum: line: 1, checksum: " )
226
+ }
227
+
228
+ #[ test]
229
+ fn merge_error_of_checksum ( ) {
230
+ let merge_line1 = MergeLine :: new ( 1 , Some ( "xyz" . to_string ( ) ) ) ;
231
+ let merge_line2 = MergeLine :: new ( 1 , Some ( "zzz" . to_string ( ) ) ) ;
232
+ let checksum_error = ChecksumError :: Mismatch ( merge_line1, merge_line2) ;
233
+ let test_error = TestError :: from ( checksum_error) ;
234
+ let merge_error = MergeError :: from ( test_error) ;
235
+ assert_eq ! ( merge_error. to_string( ) , "Source code checksums do not match: line: 1, left: xyz, right: zzz" )
163
236
}
164
237
}
0 commit comments