1
+ use std:: collections:: HashMap ;
2
+ use std:: error:: Error ;
1
3
use serde:: { Deserialize , Serialize } ;
2
4
3
5
#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
@@ -18,35 +20,194 @@ pub struct Entry {
18
20
19
21
impl Default for Data {
20
22
fn default ( ) -> Self {
21
- let data = load_data_from_str ( include_str ! ( "table_data.json " ) ) . expect ( "Failed to load data from embedded JSON " ) ;
23
+ let data = load_data_from_csv ( include_str ! ( "table_data.csv " ) ) . expect ( "Failed to load data from CSV file " ) ;
22
24
Self { data }
23
25
}
24
26
}
25
27
26
- fn load_data_from_str ( json_str : & str ) -> Result < Vec < Entry > , Box < dyn std:: error:: Error > > {
27
- let json_data: Vec < serde_json:: Value > = serde_json:: from_str ( json_str) ?;
28
+
29
+ fn load_data_from_csv ( csv_data : & str ) -> Result < Vec < Entry > , Box < dyn Error > > {
30
+ let expected_headers = vec ! [ "id" , "element" , "nucleons" , "library" , "incident_particle" , "mt" , "temperature" ] ;
31
+
32
+ let reaction_name = get_reaction_name_map ( ) ;
33
+
34
+ let mut lines = csv_data. lines ( ) ;
35
+
36
+ // Check the headers
37
+ if let Some ( header_line) = lines. next ( ) {
38
+ let headers: Vec < & str > = header_line. split ( ',' ) . collect ( ) ;
39
+ if headers != expected_headers {
40
+ return Err ( format ! ( "CSV header does not match expected columns. Found: {:?}, Expected: {:?}" , headers, expected_headers) . into ( ) ) ;
41
+ }
42
+ } else {
43
+ return Err ( "Failed to read the header line from the CSV data" . into ( ) ) ;
44
+ }
45
+
28
46
let mut data = Vec :: new ( ) ;
29
- for item in json_data {
30
- let id : i32 = item [ "i" ] . as_i64 ( ) . unwrap ( ) as i32 ;
31
- let element = item [ "e" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
32
- let nucleons : i32 = item [ "n" ] . as_i64 ( ) . unwrap ( ) as i32 ;
33
- let library = item [ "l" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
34
- let reaction = item [ "r" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
35
- let mt : i32 = item [ "m" ] . as_i64 ( ) . unwrap ( ) as i32 ;
36
- let temperature = item [ "t" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
37
- data . push ( Entry {
38
- id,
39
- element,
40
- nucleons,
41
- library,
47
+ for line in lines {
48
+ let fields : Vec < & str > = line . split ( ',' ) . collect ( ) ;
49
+ if fields . len ( ) != expected_headers . len ( ) {
50
+ return Err ( format ! ( "CSV row does not match expected number of columns. Found: {}, Expected: {}" , fields . len ( ) , expected_headers . len ( ) ) . into ( ) ) ;
51
+ }
52
+ let mt : i32 = fields [ 5 ] . parse ( ) ? ;
53
+ let reaction_description = reaction_name . get ( & mt ) . cloned ( ) . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
54
+ let reaction = format ! ( "(n,{})" , reaction_description ) ;
55
+ let entry = Entry {
56
+ id : fields [ 0 ] . parse ( ) ? ,
57
+ element : fields [ 1 ] . to_string ( ) ,
58
+ nucleons : fields [ 2 ] . parse ( ) ? ,
59
+ library : fields [ 3 ] . to_string ( ) ,
42
60
reaction,
43
61
mt,
44
- temperature,
45
- } ) ;
62
+ temperature : fields[ 6 ] . to_string ( ) ,
63
+ } ;
64
+ data. push ( entry) ;
46
65
}
47
66
Ok ( data)
48
67
}
49
68
69
+
70
+ fn get_reaction_name_map ( ) -> HashMap < i32 , String > {
71
+ let mut reaction_name = HashMap :: new ( ) ;
72
+ reaction_name. insert ( 1 , "total" . to_string ( ) ) ;
73
+ reaction_name. insert ( 2 , "elastic" . to_string ( ) ) ;
74
+ reaction_name. insert ( 3 , "nonelastic" . to_string ( ) ) ;
75
+ reaction_name. insert ( 4 , "level" . to_string ( ) ) ;
76
+ reaction_name. insert ( 5 , "misc" . to_string ( ) ) ;
77
+ reaction_name. insert ( 11 , "2nd" . to_string ( ) ) ;
78
+ reaction_name. insert ( 16 , "2n" . to_string ( ) ) ;
79
+ reaction_name. insert ( 17 , "3n" . to_string ( ) ) ;
80
+ reaction_name. insert ( 18 , "fission" . to_string ( ) ) ;
81
+ reaction_name. insert ( 19 , "f" . to_string ( ) ) ;
82
+ reaction_name. insert ( 20 , "nf" . to_string ( ) ) ;
83
+ reaction_name. insert ( 21 , "2nf" . to_string ( ) ) ;
84
+ reaction_name. insert ( 22 , "na" . to_string ( ) ) ;
85
+ reaction_name. insert ( 23 , "n3a" . to_string ( ) ) ;
86
+ reaction_name. insert ( 24 , "2na" . to_string ( ) ) ;
87
+ reaction_name. insert ( 25 , "3na" . to_string ( ) ) ;
88
+ reaction_name. insert ( 27 , "absorption" . to_string ( ) ) ;
89
+ reaction_name. insert ( 28 , "np" . to_string ( ) ) ;
90
+ reaction_name. insert ( 29 , "n2a" . to_string ( ) ) ;
91
+ reaction_name. insert ( 30 , "2n2a" . to_string ( ) ) ;
92
+ reaction_name. insert ( 32 , "nd" . to_string ( ) ) ;
93
+ reaction_name. insert ( 33 , "nt" . to_string ( ) ) ;
94
+ reaction_name. insert ( 34 , "nHe-3" . to_string ( ) ) ;
95
+ reaction_name. insert ( 35 , "nd2a" . to_string ( ) ) ;
96
+ reaction_name. insert ( 36 , "nt2a" . to_string ( ) ) ;
97
+ reaction_name. insert ( 37 , "4n" . to_string ( ) ) ;
98
+ reaction_name. insert ( 38 , "3nf" . to_string ( ) ) ;
99
+ reaction_name. insert ( 41 , "2np" . to_string ( ) ) ;
100
+ reaction_name. insert ( 42 , "3np" . to_string ( ) ) ;
101
+ reaction_name. insert ( 44 , "n2p" . to_string ( ) ) ;
102
+ reaction_name. insert ( 45 , "npa" . to_string ( ) ) ;
103
+ reaction_name. insert ( 91 , "nc" . to_string ( ) ) ;
104
+ reaction_name. insert ( 101 , "disappear" . to_string ( ) ) ;
105
+ reaction_name. insert ( 102 , "gamma" . to_string ( ) ) ;
106
+ reaction_name. insert ( 103 , "p" . to_string ( ) ) ;
107
+ reaction_name. insert ( 104 , "d" . to_string ( ) ) ;
108
+ reaction_name. insert ( 105 , "t" . to_string ( ) ) ;
109
+ reaction_name. insert ( 106 , "3He" . to_string ( ) ) ;
110
+ reaction_name. insert ( 107 , "a" . to_string ( ) ) ;
111
+ reaction_name. insert ( 108 , "2a" . to_string ( ) ) ;
112
+ reaction_name. insert ( 109 , "3a" . to_string ( ) ) ;
113
+ reaction_name. insert ( 111 , "2p" . to_string ( ) ) ;
114
+ reaction_name. insert ( 112 , "pa" . to_string ( ) ) ;
115
+ reaction_name. insert ( 113 , "t2a" . to_string ( ) ) ;
116
+ reaction_name. insert ( 114 , "d2a" . to_string ( ) ) ;
117
+ reaction_name. insert ( 115 , "pd" . to_string ( ) ) ;
118
+ reaction_name. insert ( 116 , "pt" . to_string ( ) ) ;
119
+ reaction_name. insert ( 117 , "da" . to_string ( ) ) ;
120
+ reaction_name. insert ( 152 , "5n" . to_string ( ) ) ;
121
+ reaction_name. insert ( 153 , "6n" . to_string ( ) ) ;
122
+ reaction_name. insert ( 154 , "2nt" . to_string ( ) ) ;
123
+ reaction_name. insert ( 155 , "ta" . to_string ( ) ) ;
124
+ reaction_name. insert ( 156 , "4np" . to_string ( ) ) ;
125
+ reaction_name. insert ( 157 , "3nd" . to_string ( ) ) ;
126
+ reaction_name. insert ( 158 , "nda" . to_string ( ) ) ;
127
+ reaction_name. insert ( 159 , "2npa" . to_string ( ) ) ;
128
+ reaction_name. insert ( 160 , "7n" . to_string ( ) ) ;
129
+ reaction_name. insert ( 161 , "8n" . to_string ( ) ) ;
130
+ reaction_name. insert ( 162 , "5np" . to_string ( ) ) ;
131
+ reaction_name. insert ( 163 , "6np" . to_string ( ) ) ;
132
+ reaction_name. insert ( 164 , "7np" . to_string ( ) ) ;
133
+ reaction_name. insert ( 165 , "4na" . to_string ( ) ) ;
134
+ reaction_name. insert ( 166 , "5na" . to_string ( ) ) ;
135
+ reaction_name. insert ( 167 , "6na" . to_string ( ) ) ;
136
+ reaction_name. insert ( 168 , "7na" . to_string ( ) ) ;
137
+ reaction_name. insert ( 169 , "4nd" . to_string ( ) ) ;
138
+ reaction_name. insert ( 170 , "5nd" . to_string ( ) ) ;
139
+ reaction_name. insert ( 171 , "6nd" . to_string ( ) ) ;
140
+ reaction_name. insert ( 172 , "3nt" . to_string ( ) ) ;
141
+ reaction_name. insert ( 173 , "4nt" . to_string ( ) ) ;
142
+ reaction_name. insert ( 174 , "5nt" . to_string ( ) ) ;
143
+ reaction_name. insert ( 175 , "6nt" . to_string ( ) ) ;
144
+ reaction_name. insert ( 176 , "2n3He" . to_string ( ) ) ;
145
+ reaction_name. insert ( 177 , "3n3He" . to_string ( ) ) ;
146
+ reaction_name. insert ( 178 , "4n3He" . to_string ( ) ) ;
147
+ reaction_name. insert ( 179 , "3n2p" . to_string ( ) ) ;
148
+ reaction_name. insert ( 180 , "3n3a" . to_string ( ) ) ;
149
+ reaction_name. insert ( 181 , "3npa" . to_string ( ) ) ;
150
+ reaction_name. insert ( 182 , "dt" . to_string ( ) ) ;
151
+ reaction_name. insert ( 183 , "npd" . to_string ( ) ) ;
152
+ reaction_name. insert ( 184 , "npt" . to_string ( ) ) ;
153
+ reaction_name. insert ( 185 , "ndt" . to_string ( ) ) ;
154
+ reaction_name. insert ( 186 , "np3He" . to_string ( ) ) ;
155
+ reaction_name. insert ( 187 , "nd3He" . to_string ( ) ) ;
156
+ reaction_name. insert ( 188 , "nt3He" . to_string ( ) ) ;
157
+ reaction_name. insert ( 189 , "nta" . to_string ( ) ) ;
158
+ reaction_name. insert ( 190 , "2n2p" . to_string ( ) ) ;
159
+ reaction_name. insert ( 191 , "p3He" . to_string ( ) ) ;
160
+ reaction_name. insert ( 192 , "d3He" . to_string ( ) ) ;
161
+ reaction_name. insert ( 193 , "3Hea" . to_string ( ) ) ;
162
+ reaction_name. insert ( 194 , "4n2p" . to_string ( ) ) ;
163
+ reaction_name. insert ( 195 , "4n2a" . to_string ( ) ) ;
164
+ reaction_name. insert ( 196 , "4npa" . to_string ( ) ) ;
165
+ reaction_name. insert ( 197 , "3p" . to_string ( ) ) ;
166
+ reaction_name. insert ( 198 , "n3p" . to_string ( ) ) ;
167
+ reaction_name. insert ( 199 , "3n2pa" . to_string ( ) ) ;
168
+ reaction_name. insert ( 200 , "5n2p" . to_string ( ) ) ;
169
+ reaction_name. insert ( 444 , "damage" . to_string ( ) ) ;
170
+ reaction_name. insert ( 649 , "pc" . to_string ( ) ) ;
171
+ reaction_name. insert ( 699 , "dc" . to_string ( ) ) ;
172
+ reaction_name. insert ( 749 , "tc" . to_string ( ) ) ;
173
+ reaction_name. insert ( 799 , "3Hec" . to_string ( ) ) ;
174
+ reaction_name. insert ( 849 , "ac" . to_string ( ) ) ;
175
+ reaction_name. insert ( 891 , "2nc" . to_string ( ) ) ;
176
+
177
+ for i in 50 ..91 {
178
+ reaction_name. insert ( i, format ! ( "n{}" , i - 50 ) ) ;
179
+ }
180
+ for i in 600 ..649 {
181
+ reaction_name. insert ( i, format ! ( "p{}" , i - 600 ) ) ;
182
+ }
183
+ for i in 650 ..699 {
184
+ reaction_name. insert ( i, format ! ( "d{}" , i - 650 ) ) ;
185
+ }
186
+ for i in 700 ..749 {
187
+ reaction_name. insert ( i, format ! ( "t{}" , i - 700 ) ) ;
188
+ }
189
+ for i in 750 ..799 {
190
+ reaction_name. insert ( i, format ! ( "3He{}" , i - 750 ) ) ;
191
+ }
192
+ for i in 800 ..849 {
193
+ reaction_name. insert ( i, format ! ( "a{}" , i - 800 ) ) ;
194
+ }
195
+ for i in 875 ..891 {
196
+ reaction_name. insert ( i, format ! ( "2n{}" , i - 875 ) ) ;
197
+ }
198
+
199
+ reaction_name. insert ( 203 , "Xp" . to_string ( ) ) ;
200
+ reaction_name. insert ( 204 , "Xd" . to_string ( ) ) ;
201
+ reaction_name. insert ( 205 , "Xt" . to_string ( ) ) ;
202
+ reaction_name. insert ( 206 , "3He" . to_string ( ) ) ;
203
+ reaction_name. insert ( 207 , "Xa" . to_string ( ) ) ;
204
+ reaction_name. insert ( 301 , "heat" . to_string ( ) ) ;
205
+ reaction_name. insert ( 901 , "displacement NRT" . to_string ( ) ) ;
206
+
207
+ reaction_name
208
+ }
209
+
210
+
50
211
pub enum DataActions {
51
212
#[ allow( dead_code) ]
52
213
RemoveData ( i32 ) ,
0 commit comments