1use crate::model::identifier::{fips::State, has_geoid_string::HasGeoidString, Geoid};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
5#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
6pub enum StateCode {
7 Alabama,
8 Alaska,
9 Arizona,
10 Arkansas,
11 California,
12 Colorado,
13 Connecticut,
14 Delaware,
15 DistrictOfColumbia,
16 Florida,
17 Georgia,
18 Hawaii,
19 Idaho,
20 Illinois,
21 Indiana,
22 Iowa,
23 Kansas,
24 Kentucky,
25 Louisiana,
26 Maine,
27 Maryland,
28 Massachusetts,
29 Michigan,
30 Minnesota,
31 Mississippi,
32 Missouri,
33 Montana,
34 Nebraska,
35 Nevada,
36 NewHampshire,
37 NewJersey,
38 NewMexico,
39 NewYork,
40 NorthCarolina,
41 NorthDakota,
42 Ohio,
43 Oklahoma,
44 Oregon,
45 Pennsylvania,
46 RhodeIsland,
47 SouthCarolina,
48 SouthDakota,
49 Tennessee,
50 Texas,
51 Utah,
52 Vermont,
53 Virginia,
54 Washington,
55 WestVirginia,
56 Wisconsin,
57 Wyoming,
58}
59
60impl TryFrom<State> for StateCode {
61 type Error = String;
62
63 fn try_from(value: State) -> Result<Self, Self::Error> {
64 match value {
65 State(1) => Ok(StateCode::Alabama),
66 State(2) => Ok(StateCode::Alaska),
67 State(4) => Ok(StateCode::Arizona),
68 State(5) => Ok(StateCode::Arkansas),
69 State(6) => Ok(StateCode::California),
70 State(8) => Ok(StateCode::Colorado),
71 State(9) => Ok(StateCode::Connecticut),
72 State(10) => Ok(StateCode::Delaware),
73 State(11) => Ok(StateCode::DistrictOfColumbia),
74 State(12) => Ok(StateCode::Florida),
75 State(13) => Ok(StateCode::Georgia),
76 State(15) => Ok(StateCode::Hawaii),
77 State(16) => Ok(StateCode::Idaho),
78 State(17) => Ok(StateCode::Illinois),
79 State(18) => Ok(StateCode::Indiana),
80 State(19) => Ok(StateCode::Iowa),
81 State(20) => Ok(StateCode::Kansas),
82 State(21) => Ok(StateCode::Kentucky),
83 State(22) => Ok(StateCode::Louisiana),
84 State(23) => Ok(StateCode::Maine),
85 State(24) => Ok(StateCode::Maryland),
86 State(25) => Ok(StateCode::Massachusetts),
87 State(26) => Ok(StateCode::Michigan),
88 State(27) => Ok(StateCode::Minnesota),
89 State(28) => Ok(StateCode::Mississippi),
90 State(29) => Ok(StateCode::Missouri),
91 State(30) => Ok(StateCode::Montana),
92 State(31) => Ok(StateCode::Nebraska),
93 State(32) => Ok(StateCode::Nevada),
94 State(33) => Ok(StateCode::NewHampshire),
95 State(34) => Ok(StateCode::NewJersey),
96 State(35) => Ok(StateCode::NewMexico),
97 State(36) => Ok(StateCode::NewYork),
98 State(37) => Ok(StateCode::NorthCarolina),
99 State(38) => Ok(StateCode::NorthDakota),
100 State(39) => Ok(StateCode::Ohio),
101 State(40) => Ok(StateCode::Oklahoma),
102 State(41) => Ok(StateCode::Oregon),
103 State(42) => Ok(StateCode::Pennsylvania),
104 State(44) => Ok(StateCode::RhodeIsland),
105 State(45) => Ok(StateCode::SouthCarolina),
106 State(46) => Ok(StateCode::SouthDakota),
107 State(47) => Ok(StateCode::Tennessee),
108 State(48) => Ok(StateCode::Texas),
109 State(49) => Ok(StateCode::Utah),
110 State(50) => Ok(StateCode::Vermont),
111 State(51) => Ok(StateCode::Virginia),
112 State(53) => Ok(StateCode::Washington),
113 State(54) => Ok(StateCode::WestVirginia),
114 State(55) => Ok(StateCode::Wisconsin),
115 State(56) => Ok(StateCode::Wyoming),
116 _ => Err(format!("unknown FIPS state code {}", value.geoid_string())),
117 }
118 }
119}
120
121impl TryFrom<Geoid> for StateCode {
122 type Error = String;
123
124 fn try_from(value: Geoid) -> Result<Self, Self::Error> {
125 match value.to_state() {
126 Geoid::State(s) => s.try_into(),
127 _ => Err(String::from("internal error")),
128 }
129 }
130}
131
132impl From<StateCode> for State {
133 fn from(val: StateCode) -> Self {
134 match val {
135 StateCode::Alabama => State(1),
136 StateCode::Alaska => State(2),
137 StateCode::Arizona => State(4),
138 StateCode::Arkansas => State(5),
139 StateCode::California => State(6),
140 StateCode::Colorado => State(8),
141 StateCode::Connecticut => State(9),
142 StateCode::Delaware => State(10),
143 StateCode::DistrictOfColumbia => State(11),
144 StateCode::Florida => State(12),
145 StateCode::Georgia => State(13),
146 StateCode::Hawaii => State(15),
147 StateCode::Idaho => State(16),
148 StateCode::Illinois => State(17),
149 StateCode::Indiana => State(18),
150 StateCode::Iowa => State(19),
151 StateCode::Kansas => State(20),
152 StateCode::Kentucky => State(21),
153 StateCode::Louisiana => State(22),
154 StateCode::Maine => State(23),
155 StateCode::Maryland => State(24),
156 StateCode::Massachusetts => State(25),
157 StateCode::Michigan => State(26),
158 StateCode::Minnesota => State(27),
159 StateCode::Mississippi => State(28),
160 StateCode::Missouri => State(29),
161 StateCode::Montana => State(30),
162 StateCode::Nebraska => State(31),
163 StateCode::Nevada => State(32),
164 StateCode::NewHampshire => State(33),
165 StateCode::NewJersey => State(34),
166 StateCode::NewMexico => State(35),
167 StateCode::NewYork => State(36),
168 StateCode::NorthCarolina => State(37),
169 StateCode::NorthDakota => State(38),
170 StateCode::Ohio => State(39),
171 StateCode::Oklahoma => State(40),
172 StateCode::Oregon => State(41),
173 StateCode::Pennsylvania => State(42),
174 StateCode::RhodeIsland => State(44),
175 StateCode::SouthCarolina => State(45),
176 StateCode::SouthDakota => State(46),
177 StateCode::Tennessee => State(47),
178 StateCode::Texas => State(48),
179 StateCode::Utah => State(49),
180 StateCode::Vermont => State(50),
181 StateCode::Virginia => State(51),
182 StateCode::Washington => State(53),
183 StateCode::WestVirginia => State(54),
184 StateCode::Wisconsin => State(55),
185 StateCode::Wyoming => State(56),
186 }
187 }
188}
189
190impl StateCode {
191 pub const ALL: [StateCode; 51] = [
192 StateCode::Alabama,
193 StateCode::Alaska,
194 StateCode::Arizona,
195 StateCode::Arkansas,
196 StateCode::California,
197 StateCode::Colorado,
198 StateCode::Connecticut,
199 StateCode::Delaware,
200 StateCode::DistrictOfColumbia,
201 StateCode::Florida,
202 StateCode::Georgia,
203 StateCode::Hawaii,
204 StateCode::Idaho,
205 StateCode::Illinois,
206 StateCode::Indiana,
207 StateCode::Iowa,
208 StateCode::Kansas,
209 StateCode::Kentucky,
210 StateCode::Louisiana,
211 StateCode::Maine,
212 StateCode::Maryland,
213 StateCode::Massachusetts,
214 StateCode::Michigan,
215 StateCode::Minnesota,
216 StateCode::Mississippi,
217 StateCode::Missouri,
218 StateCode::Montana,
219 StateCode::Nebraska,
220 StateCode::Nevada,
221 StateCode::NewHampshire,
222 StateCode::NewJersey,
223 StateCode::NewMexico,
224 StateCode::NewYork,
225 StateCode::NorthCarolina,
226 StateCode::NorthDakota,
227 StateCode::Ohio,
228 StateCode::Oklahoma,
229 StateCode::Oregon,
230 StateCode::Pennsylvania,
231 StateCode::RhodeIsland,
232 StateCode::SouthCarolina,
233 StateCode::SouthDakota,
234 StateCode::Tennessee,
235 StateCode::Texas,
236 StateCode::Utah,
237 StateCode::Vermont,
238 StateCode::Virginia,
239 StateCode::Washington,
240 StateCode::WestVirginia,
241 StateCode::Wisconsin,
242 StateCode::Wyoming,
243 ];
244
245 pub fn to_state_abbreviation(&self) -> String {
247 match self {
248 StateCode::Alabama => String::from("AL"),
249 StateCode::Alaska => String::from("AK"),
250 StateCode::Arizona => String::from("AZ"),
251 StateCode::Arkansas => String::from("AR"),
252 StateCode::California => String::from("CA"),
253 StateCode::Colorado => String::from("CO"),
254 StateCode::Connecticut => String::from("CT"),
255 StateCode::Delaware => String::from("DE"),
256 StateCode::DistrictOfColumbia => String::from("DC"),
257 StateCode::Florida => String::from("FL"),
258 StateCode::Georgia => String::from("GA"),
259 StateCode::Hawaii => String::from("HI"),
260 StateCode::Idaho => String::from("ID"),
261 StateCode::Illinois => String::from("IL"),
262 StateCode::Indiana => String::from("IN"),
263 StateCode::Iowa => String::from("IA"),
264 StateCode::Kansas => String::from("KS"),
265 StateCode::Kentucky => String::from("Ky"),
266 StateCode::Louisiana => String::from("LA"),
267 StateCode::Maine => String::from("ME"),
268 StateCode::Maryland => String::from("MD"),
269 StateCode::Massachusetts => String::from("MA"),
270 StateCode::Michigan => String::from("MI"),
271 StateCode::Minnesota => String::from("MN"),
272 StateCode::Mississippi => String::from("MS"),
273 StateCode::Missouri => String::from("MO"),
274 StateCode::Montana => String::from("MT"),
275 StateCode::Nebraska => String::from("NE"),
276 StateCode::Nevada => String::from("NV"),
277 StateCode::NewHampshire => String::from("NH"),
278 StateCode::NewJersey => String::from("NJ"),
279 StateCode::NewMexico => String::from("NM"),
280 StateCode::NewYork => String::from("NY"),
281 StateCode::NorthCarolina => String::from("NC"),
282 StateCode::NorthDakota => String::from("ND"),
283 StateCode::Ohio => String::from("OH"),
284 StateCode::Oklahoma => String::from("OK"),
285 StateCode::Oregon => String::from("OR"),
286 StateCode::Pennsylvania => String::from("PA"),
287 StateCode::RhodeIsland => String::from("RI"),
288 StateCode::SouthCarolina => String::from("SC"),
289 StateCode::SouthDakota => String::from("SD"),
290 StateCode::Tennessee => String::from("TN"),
291 StateCode::Texas => String::from("TX"),
292 StateCode::Utah => String::from("UT"),
293 StateCode::Vermont => String::from("VT"),
294 StateCode::Virginia => String::from("VA"),
295 StateCode::Washington => String::from("WA"),
296 StateCode::WestVirginia => String::from("WV"),
297 StateCode::Wisconsin => String::from("WI"),
298 StateCode::Wyoming => String::from("WY"),
299 }
300 }
301
302 pub fn to_fips_string(&self) -> String {
303 match self {
304 StateCode::Alabama => String::from("01"),
305 StateCode::Alaska => String::from("02"),
306 StateCode::Arizona => String::from("04"),
307 StateCode::Arkansas => String::from("05"),
308 StateCode::California => String::from("06"),
309 StateCode::Colorado => String::from("08"),
310 StateCode::Connecticut => String::from("09"),
311 StateCode::Delaware => String::from("10"),
312 StateCode::DistrictOfColumbia => String::from("11"),
313 StateCode::Florida => String::from("12"),
314 StateCode::Georgia => String::from("13"),
315 StateCode::Hawaii => String::from("15"),
316 StateCode::Idaho => String::from("16"),
317 StateCode::Illinois => String::from("17"),
318 StateCode::Indiana => String::from("18"),
319 StateCode::Iowa => String::from("19"),
320 StateCode::Kansas => String::from("20"),
321 StateCode::Kentucky => String::from("21"),
322 StateCode::Louisiana => String::from("22"),
323 StateCode::Maine => String::from("23"),
324 StateCode::Maryland => String::from("24"),
325 StateCode::Massachusetts => String::from("25"),
326 StateCode::Michigan => String::from("26"),
327 StateCode::Minnesota => String::from("27"),
328 StateCode::Mississippi => String::from("28"),
329 StateCode::Missouri => String::from("29"),
330 StateCode::Montana => String::from("30"),
331 StateCode::Nebraska => String::from("31"),
332 StateCode::Nevada => String::from("32"),
333 StateCode::NewHampshire => String::from("33"),
334 StateCode::NewJersey => String::from("34"),
335 StateCode::NewMexico => String::from("35"),
336 StateCode::NewYork => String::from("36"),
337 StateCode::NorthCarolina => String::from("37"),
338 StateCode::NorthDakota => String::from("38"),
339 StateCode::Ohio => String::from("39"),
340 StateCode::Oklahoma => String::from("40"),
341 StateCode::Oregon => String::from("41"),
342 StateCode::Pennsylvania => String::from("42"),
343 StateCode::RhodeIsland => String::from("44"),
344 StateCode::SouthCarolina => String::from("45"),
345 StateCode::SouthDakota => String::from("46"),
346 StateCode::Tennessee => String::from("47"),
347 StateCode::Texas => String::from("48"),
348 StateCode::Utah => String::from("49"),
349 StateCode::Vermont => String::from("50"),
350 StateCode::Virginia => String::from("51"),
351 StateCode::Washington => String::from("53"),
352 StateCode::WestVirginia => String::from("54"),
353 StateCode::Wisconsin => String::from("55"),
354 StateCode::Wyoming => String::from("56"),
355 }
356 }
357
358 pub fn to_full_name(&self) -> String {
359 match self {
360 StateCode::Alabama => String::from("Alabama"),
361 StateCode::Alaska => String::from("Alaska"),
362 StateCode::Arizona => String::from("Arizona"),
363 StateCode::Arkansas => String::from("Arkansas"),
364 StateCode::California => String::from("California"),
365 StateCode::Colorado => String::from("Colorado"),
366 StateCode::Connecticut => String::from("Connecticut"),
367 StateCode::Delaware => String::from("Delaware"),
368 StateCode::DistrictOfColumbia => String::from("DistrictOfColumbia"),
369 StateCode::Florida => String::from("Florida"),
370 StateCode::Georgia => String::from("Georgia"),
371 StateCode::Hawaii => String::from("Hawaii"),
372 StateCode::Idaho => String::from("Idaho"),
373 StateCode::Illinois => String::from("Illinois"),
374 StateCode::Indiana => String::from("Indiana"),
375 StateCode::Iowa => String::from("Iowa"),
376 StateCode::Kansas => String::from("Kansas"),
377 StateCode::Kentucky => String::from("Kentucky"),
378 StateCode::Louisiana => String::from("Louisiana"),
379 StateCode::Maine => String::from("Maine"),
380 StateCode::Maryland => String::from("Maryland"),
381 StateCode::Massachusetts => String::from("Massachusetts"),
382 StateCode::Michigan => String::from("Michigan"),
383 StateCode::Minnesota => String::from("Minnesota"),
384 StateCode::Mississippi => String::from("Mississippi"),
385 StateCode::Missouri => String::from("Missouri"),
386 StateCode::Montana => String::from("Montana"),
387 StateCode::Nebraska => String::from("Nebraska"),
388 StateCode::Nevada => String::from("Nevada"),
389 StateCode::NewHampshire => String::from("NewHampshire"),
390 StateCode::NewJersey => String::from("NewJersey"),
391 StateCode::NewMexico => String::from("NewMexico"),
392 StateCode::NewYork => String::from("NewYork"),
393 StateCode::NorthCarolina => String::from("NorthCarolina"),
394 StateCode::NorthDakota => String::from("NorthDakota"),
395 StateCode::Ohio => String::from("Ohio"),
396 StateCode::Oklahoma => String::from("Oklahoma"),
397 StateCode::Oregon => String::from("Oregon"),
398 StateCode::Pennsylvania => String::from("Pennsylvania"),
399 StateCode::RhodeIsland => String::from("RhodeIsland"),
400 StateCode::SouthCarolina => String::from("SouthCarolina"),
401 StateCode::SouthDakota => String::from("SouthDakota"),
402 StateCode::Tennessee => String::from("Tennessee"),
403 StateCode::Texas => String::from("Texas"),
404 StateCode::Utah => String::from("Utah"),
405 StateCode::Vermont => String::from("Vermont"),
406 StateCode::Virginia => String::from("Virginia"),
407 StateCode::Washington => String::from("Washington"),
408 StateCode::WestVirginia => String::from("WestVirginia"),
409 StateCode::Wisconsin => String::from("Wisconsin"),
410 StateCode::Wyoming => String::from("Wyoming"),
411 }
412 }
413}