diff --git a/Cargo.toml b/Cargo.toml index 9be2af9..40c47df 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ipcap" -version = "0.1.4" +version = "0.1.5" description = "🌍 A CLI & library for decoding IP addresses into state, postal code, country, coordinates, etc without internet access." license = "MIT" keywords = ["ip", "geolocation", "geoip", "ipcap"] @@ -21,6 +21,9 @@ clap = { version = "4.4.12", features = ["derive"] , optional = true } dirs = "5.0.1" once_cell = "1.19.0" +[build-dependencies] +ipcap-codegen = { path = "ipcap-codegen", version = "0.1.0" } + [features] cli = ["clap"] diff --git a/README.md b/README.md index 00fbbf8..158f7ae 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) ```sh -ipcap 0.1.4 +ipcap 0.1.5 ▄█ ▄███████▄ ▄████████ ▄████████ ▄███████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ @@ -126,7 +126,7 @@ ipcap -t 8.8.8.8 ```toml [dependencies] -ipcap = "0.1.2" +ipcap = "0.1.5" ``` ```rust diff --git a/build.rs b/build.rs index 3f50f29..fdf384a 100644 --- a/build.rs +++ b/build.rs @@ -7,5 +7,5 @@ pub mod names; use codegen::run; fn main() { - run() + ipcap_codegen::run() } diff --git a/ipcap-codegen/Cargo.toml b/ipcap-codegen/Cargo.toml new file mode 100644 index 0000000..ab6430b --- /dev/null +++ b/ipcap-codegen/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ipcap-codegen" +version = "0.1.0" +edition = "2021" +description = "ipcap codegen crate." +license = "MIT" +keywords = ["ipcap", "ipcap-codegen", "rust"] +repository = "/~https://github.com/wiseaidev/ipcap" +documentation = "https://docs.rs/ipcap" +authors = ["Mahmoud Harmouch "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/ipcap-codegen/countries-names.txt b/ipcap-codegen/countries-names.txt new file mode 100644 index 0000000..4efeca3 --- /dev/null +++ b/ipcap-codegen/countries-names.txt @@ -0,0 +1,254 @@ +Asia/Pacific Region +Europe +Andorra +United Arab Emirates +Afghanistan +Antigua and Barbuda +Anguilla +Albania +Armenia +Netherlands Antilles +Angola +Antarctica +Argentina +American Samoa +Austria +Australia +Aruba +Azerbaijan +Bosnia and Herzegovina +Barbados +Bangladesh +Belgium +Burkina Faso +Bulgaria +Bahrain +Burundi +Benin +Bermuda +Brunei Darussalam +Bolivia +Brazil +Bahamas +Bhutan +Bouvet Island +Botswana +Belarus +Belize +Canada +Cocos (Keeling) Islands +Democratic Republic Of Congo +Central African Republic +Congo +Switzerland +Cote D'Ivoire +Cook Islands +Chile +Cameroon +China +Colombia +Costa Rica +Cuba +Cape Verde +Christmas Island +Cyprus +Czech Republic +Germany +Djibouti +Denmark +Dominica +Dominican Republic +Algeria +Ecuador +Estonia +Egypt +Western Sahara +Eritrea +Spain +Ethiopia +Finland +Fiji +Falkland Islands (Malvinas) +Micronesia, Federated States of +Faroe Islands +France +Metropolitan France +Gabon +United Kingdom +Grenada +Georgia +French Guiana +Ghana +Gibraltar +Greenland +Gambia +Guinea +Guadeloupe +Equatorial Guinea +Greece +South Georgia and the South Sandwich Islands +Guatemala +Guam +Guinea-Bissau +Guyana +Hong Kong +Heard Island and McDonald Islands +Honduras +Croatia +Haiti +Hungary +Indonesia +Ireland +Israel +India +British Indian Ocean Territory +Iraq +Iran, Islamic Republic of +Iceland +Italy +Jamaica +Jordan +Japan +Kenya +Kyrgyzstan +Cambodia +Kiribati +Comoros +Saint Kitts and Nevis +North Korea +South Korea +Kuwait +Cayman Islands +Kazakhstan +Lao People's Democratic Republic +Lebanon +Saint Lucia +Liechtenstein +Sri Lanka +Liberia +Lesotho +Lithuania +Luxembourg +Latvia +Libya +Morocco +Monaco +Moldova, Republic of +Madagascar +Marshall Islands +Macedonia +Mali +Myanmar +Mongolia +Macau +Northern Mariana Islands +Martinique +Mauritania +Montserrat +Malta +Mauritius +Maldives +Malawi +Mexico +Malaysia +Mozambique +Namibia +New Caledonia +Niger +Norfolk Island +Nigeria +Nicaragua +Netherlands +Norway +Nepal +Nauru +Niue +New Zealand +Oman +Panama +Peru +French Polynesia +Papua New Guinea +Philippines +Pakistan +Poland +Saint Pierre and Miquelon +Pitcairn Islands +Puerto Rico +Palestinian Territory +Portugal +Palau +Paraguay +Qatar +Reunion +Romania +Russian Federation +Rwanda +Saudi Arabia +Solomon Islands +Seychelles +Sudan +Sweden +Singapore +Saint Helena +Slovenia +Svalbard and Jan Mayen +Slovakia +Sierra Leone +San Marino +Senegal +Somalia +Suriname +Sao Tome and Principe +El Salvador +Syrian Arab Republic +Swaziland +Turks and Caicos Islands +Chad +French Southern Territories +Togo +Thailand +Tajikistan +Tokelau +Turkmenistan +Tunisia +Tonga +Timor-Leste +Turkey +Trinidad and Tobago +Tuvalu +Taiwan +Tanzania, United Republic of +Ukraine +Uganda +United States Minor Outlying Islands +United States +Uruguay +Uzbekistan +Holy See (Vatican City State) +Saint Vincent and the Grenadines +Venezuela +British Virgin Islands +US Virgin Islands +Vietnam +Vanuatu +Wallis and Futuna +Samoa +Yemen +Mayotte +Serbia +South Africa +Zambia +Montenegro +Zimbabwe +Anonymous Proxy +Satellite Provider +Other +Aland Islands +Guernsey +Isle of Man +Jersey +Saint Barthelemy +Saint Martin +Bonaire, Sint Eustatius and Saba +South Sudan \ No newline at end of file diff --git a/ipcap-codegen/countries-three.txt b/ipcap-codegen/countries-three.txt new file mode 100644 index 0000000..af97a5c --- /dev/null +++ b/ipcap-codegen/countries-three.txt @@ -0,0 +1,254 @@ +AP +EU +AND +ARE +AFG +ATG +AIA +ALB +ARM +ANT +AGO +AQ +ARG +ASM +AUT +AUS +ABW +AZE +BIH +BRB +BGD +BEL +BFA +BGR +BHR +BDI +BEN +BMU +BRN +BOL +BRA +BHS +BTN +BV +BWA +BLR +BLZ +CAN +CC +COD +CAF +COG +CHE +CIV +COK +CHL +CMR +CHN +COL +CRI +CUB +CPV +CX +CYP +CZE +DEU +DJI +DNK +DMA +DOM +DZA +ECU +EST +EGY +ESH +ERI +ESP +ETH +FIN +FJI +FLK +FSM +FRO +FRA +FX +GAB +GBR +GRD +GEO +GUF +GHA +GIB +GRL +GMB +GIN +GLP +GNQ +GRC +GS +GTM +GUM +GNB +GUY +HKG +HM +HND +HRV +HTI +HUN +IDN +IRL +ISR +IND +IO +IRQ +IRN +ISL +ITA +JAM +JOR +JPN +KEN +KGZ +KHM +KIR +COM +KNA +PRK +KOR +KWT +CYM +KAZ +LAO +LBN +LCA +LIE +LKA +LBR +LSO +LTU +LUX +LVA +LBY +MAR +MCO +MDA +MDG +MHL +MKD +MLI +MMR +MNG +MAC +MNP +MTQ +MRT +MSR +MLT +MUS +MDV +MWI +MEX +MYS +MOZ +NAM +NCL +NER +NFK +NGA +NIC +NLD +NOR +NPL +NRU +NIU +NZL +OMN +PAN +PER +PYF +PNG +PHL +PAK +POL +SPM +PCN +PRI +PSE +PRT +PLW +PRY +QAT +REU +ROU +RUS +RWA +SAU +SLB +SYC +SDN +SWE +SGP +SHN +SVN +SJM +SVK +SLE +SMR +SEN +SOM +SUR +STP +SLV +SYR +SWZ +TCA +TCD +TF +TGO +THA +TJK +TKL +TKM +TUN +TON +TLS +TUR +TTO +TUV +TWN +TZA +UKR +UGA +UM +USA +URY +UZB +VAT +VCT +VEN +VGB +VIR +VNM +VUT +WLF +WSM +YEM +YT +SRB +ZAF +ZMB +MNE +ZWE +A1 +A2 +O1 +ALA +GGY +IMN +JEY +BLM +MAF +BES +SSD \ No newline at end of file diff --git a/ipcap-codegen/countries-to-continents.txt b/ipcap-codegen/countries-to-continents.txt new file mode 100644 index 0000000..268d7fc --- /dev/null +++ b/ipcap-codegen/countries-to-continents.txt @@ -0,0 +1,254 @@ +Asia +Europe +Europe +Asia +Asia +NorthAmerica +NorthAmerica +Europe +Asia +NorthAmerica +Africa +Antarctica +SouthAmerica +Oceania +Europe +Oceania +NorthAmerica +Asia +Europe +NorthAmerica +Asia +Europe +Africa +Europe +Asia +Africa +Africa +NorthAmerica +Asia +SouthAmerica +SouthAmerica +NorthAmerica +Asia +Antarctica +Africa +Europe +NorthAmerica +NorthAmerica +Asia +Africa +Africa +Africa +Europe +Africa +Oceania +SouthAmerica +Africa +Asia +SouthAmerica +NorthAmerica +NorthAmerica +Africa +Asia +Asia +Europe +Europe +Africa +Europe +NorthAmerica +NorthAmerica +Africa +SouthAmerica +Europe +Africa +Africa +Africa +Europe +Africa +Europe +Oceania +SouthAmerica +Oceania +Europe +Europe +NorthAmerica +Africa +Europe +NorthAmerica +Asia +SouthAmerica +Africa +Europe +NorthAmerica +Africa +Africa +NorthAmerica +Africa +Europe +Antarctica +NorthAmerica +Oceania +Africa +SouthAmerica +Asia +Antarctica +NorthAmerica +Europe +NorthAmerica +Europe +Asia +Europe +Asia +Asia +Asia +Asia +Asia +Europe +Europe +NorthAmerica +Asia +Asia +Africa +Asia +Asia +Oceania +Africa +NorthAmerica +Asia +Asia +Asia +NorthAmerica +Asia +Asia +Asia +NorthAmerica +Europe +Asia +Africa +Africa +Europe +Europe +Europe +Africa +Africa +Europe +Europe +Africa +Oceania +Europe +Africa +Asia +Asia +Asia +Oceania +NorthAmerica +Africa +NorthAmerica +Europe +Africa +Asia +Africa +NorthAmerica +Asia +Africa +Africa +Oceania +Africa +Oceania +Africa +NorthAmerica +Europe +Europe +Asia +Oceania +Oceania +Oceania +Asia +NorthAmerica +SouthAmerica +Oceania +Oceania +Asia +Asia +Europe +NorthAmerica +Oceania +NorthAmerica +Asia +Europe +Oceania +SouthAmerica +Asia +Africa +Europe +Europe +Africa +Asia +Oceania +Africa +Africa +Europe +Asia +Africa +Europe +Europe +Europe +Africa +Europe +Africa +Africa +SouthAmerica +Africa +NorthAmerica +Asia +Africa +NorthAmerica +Africa +Antarctica +Africa +Asia +Asia +Oceania +Asia +Africa +Oceania +Asia +Europe +NorthAmerica +Oceania +Asia +Africa +Europe +Africa +Oceania +NorthAmerica +SouthAmerica +Asia +Europe +NorthAmerica +SouthAmerica +NorthAmerica +NorthAmerica +Asia +Oceania +Oceania +Oceania +Asia +Africa +Europe +Africa +Africa +Europe +Africa + + + +Europe +Europe +Europe +Europe +NorthAmerica +NorthAmerica +NorthAmerica +Africa \ No newline at end of file diff --git a/ipcap-codegen/countries-two.txt b/ipcap-codegen/countries-two.txt new file mode 100644 index 0000000..cb03690 --- /dev/null +++ b/ipcap-codegen/countries-two.txt @@ -0,0 +1,254 @@ +AP +EU +AD +AE +AF +AG +AI +AL +AM +AN +AO +AQ +AR +AS +AT +AU +AW +AZ +BA +BB +BD +BE +BF +BG +BH +BI +BJ +BM +BN +BO +BR +BS +BT +BV +BW +BY +BZ +CA +CC +CD +CF +CG +CH +CI +CK +CL +CM +CN +CO +CR +CU +CV +CX +CY +CZ +DE +DJ +DK +DM +DO +DZ +EC +EE +EG +EH +ER +ES +ET +FI +FJ +FK +FM +FO +FR +FX +GA +GB +GD +GE +GF +GH +GI +GL +GM +GN +GP +GQ +GR +GS +GT +GU +GW +GY +HK +HM +HN +HR +HT +HU +ID +IE +IL +IN +IO +IQ +IR +IS +IT +JM +JO +JP +KE +KG +KH +KI +KM +KN +KP +KR +KW +KY +KZ +LA +LB +LC +LI +LK +LR +LS +LT +LU +LV +LY +MA +MC +MD +MG +MH +MK +ML +MM +MN +MO +MP +MQ +MR +MS +MT +MU +MV +MW +MX +MY +MZ +NA +NC +NE +NF +NG +NI +NL +NO +NP +NR +NU +NZ +OM +PA +PE +PF +PG +PH +PK +PL +PM +PN +PR +PS +PT +PW +PY +QA +RE +RO +RU +RW +SA +SB +SC +SD +SE +SG +SH +SI +SJ +SK +SL +SM +SN +SO +SR +ST +SV +SY +SZ +TC +TD +TF +TG +TH +TJ +TK +TM +TN +TO +TL +TR +TT +TV +TW +TZ +UA +UG +UM +US +UY +UZ +VA +VC +VE +VG +VI +VN +VU +WF +WS +YE +YT +RS +ZA +ZM +ME +ZW +A1 +A2 +O1 +AX +GG +IM +JE +BL +MF +BQ +SS \ No newline at end of file diff --git a/ipcap-codegen/dma.txt b/ipcap-codegen/dma.txt new file mode 100644 index 0000000..705dbba --- /dev/null +++ b/ipcap-codegen/dma.txt @@ -0,0 +1,212 @@ +500; Portland-Auburn, ME +501; New York, NY +502; Binghamton, NY +503; Macon, GA +504; Philadelphia, PA +505; Detroit, MI +506; Boston, MA +507; Savannah, GA +508; Pittsburgh, PA +509; Ft Wayne, IN +510; Cleveland, OH +511; Washington, DC +512; Baltimore, MD +513; Flint, MI +514; Buffalo, NY +515; Cincinnati, OH +516; Erie, PA +517; Charlotte, NC +518; Greensboro, NC +519; Charleston, SC +520; Augusta, GA +521; Providence, RI +522; Columbus, GA +523; Burlington, VT +524; Atlanta, GA +525; Albany, GA +526; Utica-Rome, NY +527; Indianapolis, IN +528; Miami, FL +529; Louisville, KY +530; Tallahassee, FL +531; Tri-Cities, TN +532; Albany-Schenectady-Troy, NY +533; Hartford, CT +534; Orlando, FL +535; Columbus, OH +536; Youngstown-Warren, OH +537; Bangor, ME +538; Rochester, NY +539; Tampa, FL +540; Traverse City-Cadillac, MI +541; Lexington, KY +542; Dayton, OH +543; Springfield-Holyoke, MA +544; Norfolk-Portsmouth, VA +545; Greenville-New Bern-Washington, NC +546; Columbia, SC +547; Toledo, OH +548; West Palm Beach, FL +549; Watertown, NY +550; Wilmington, NC +551; Lansing, MI +552; Presque Isle, ME +553; Marquette, MI +554; Wheeling, WV +555; Syracuse, NY +556; Richmond-Petersburg, VA +557; Knoxville, TN +558; Lima, OH +559; Bluefield-Beckley-Oak Hill, WV +560; Raleigh-Durham, NC +561; Jacksonville, FL +563; Grand Rapids, MI +564; Charleston-Huntington, WV +565; Elmira, NY +566; Harrisburg-Lancaster-Lebanon-York, PA +567; Greenville-Spartenburg, SC +569; Harrisonburg, VA +570; Florence-Myrtle Beach, SC +571; Ft Myers, FL +573; Roanoke-Lynchburg, VA +574; Johnstown-Altoona, PA +575; Chattanooga, TN +576; Salisbury, MD +577; Wilkes Barre-Scranton, PA +581; Terre Haute, IN +582; Lafayette, IN +583; Alpena, MI +584; Charlottesville, VA +588; South Bend, IN +592; Gainesville, FL +596; Zanesville, OH +597; Parkersburg, WV +598; Clarksburg-Weston, WV +600; Corpus Christi, TX +602; Chicago, IL +603; Joplin-Pittsburg, MO +604; Columbia-Jefferson City, MO +605; Topeka, KS +606; Dothan, AL +609; St Louis, MO +610; Rockford, IL +611; Rochester-Mason City-Austin, MN +612; Shreveport, LA +613; Minneapolis-St Paul, MN +616; Kansas City, MO +617; Milwaukee, WI +618; Houston, TX +619; Springfield, MO +620; Tuscaloosa, AL +622; New Orleans, LA +623; Dallas-Fort Worth, TX +624; Sioux City, IA +625; Waco-Temple-Bryan, TX +626; Victoria, TX +627; Wichita Falls, TX +628; Monroe, LA +630; Birmingham, AL +631; Ottumwa-Kirksville, IA +632; Paducah, KY +633; Odessa-Midland, TX +634; Amarillo, TX +635; Austin, TX +636; Harlingen, TX +637; Cedar Rapids-Waterloo, IA +638; St Joseph, MO +639; Jackson, TN +640; Memphis, TN +641; San Antonio, TX +642; Lafayette, LA +643; Lake Charles, LA +644; Alexandria, LA +646; Anniston, AL +647; Greenwood-Greenville, MS +648; Champaign-Springfield-Decatur, IL +649; Evansville, IN +650; Oklahoma City, OK +651; Lubbock, TX +652; Omaha, NE +656; Panama City, FL +657; Sherman, TX +658; Green Bay-Appleton, WI +659; Nashville, TN +661; San Angelo, TX +662; Abilene-Sweetwater, TX +669; Madison, WI +670; Ft Smith-Fay-Springfield, AR +671; Tulsa, OK +673; Columbus-Tupelo-West Point, MS +675; Peoria-Bloomington, IL +676; Duluth, MN +678; Wichita, KS +679; Des Moines, IA +682; Davenport-Rock Island-Moline, IL +686; Mobile, AL +687; Minot-Bismarck-Dickinson, ND +691; Huntsville, AL +692; Beaumont-Port Author, TX +693; Little Rock-Pine Bluff, AR +698; Montgomery, AL +702; La Crosse-Eau Claire, WI +705; Wausau-Rhinelander, WI +709; Tyler-Longview, TX +710; Hattiesburg-Laurel, MS +711; Meridian, MS +716; Baton Rouge, LA +717; Quincy, IL +718; Jackson, MS +722; Lincoln-Hastings, NE +724; Fargo-Valley City, ND +725; Sioux Falls, SD +734; Jonesboro, AR +736; Bowling Green, KY +737; Mankato, MN +740; North Platte, NE +743; Anchorage, AK +744; Honolulu, HI +745; Fairbanks, AK +746; Biloxi-Gulfport, MS +747; Juneau, AK +749; Laredo, TX +751; Denver, CO +752; Colorado Springs, CO +753; Phoenix, AZ +754; Butte-Bozeman, MT +755; Great Falls, MT +756; Billings, MT +757; Boise, ID +758; Idaho Falls-Pocatello, ID +759; Cheyenne, WY +760; Twin Falls, ID +762; Missoula, MT +764; Rapid City, SD +765; El Paso, TX +766; Helena, MT +767; Casper-Riverton, WY +770; Salt Lake City, UT +771; Yuma, AZ +773; Grand Junction, CO +789; Tucson, AZ +790; Albuquerque, NM +798; Glendive, MT +800; Bakersfield, CA +801; Eugene, OR +802; Eureka, CA +803; Los Angeles, CA +804; Palm Springs, CA +807; San Francisco, CA +810; Yakima-Pasco, WA +811; Reno, NV +813; Medford-Klamath Falls, OR +819; Seattle-Tacoma, WA +820; Portland, OR +821; Bend, OR +825; San Diego, CA +828; Monterey-Salinas, CA +839; Las Vegas, NV +855; Santa Barbara, CA +862; Sacramento, CA +866; Fresno, CA +868; Chico-Redding, CA +881; Spokane, WA \ No newline at end of file diff --git a/ipcap-codegen/src/lib.rs b/ipcap-codegen/src/lib.rs new file mode 100644 index 0000000..ca238fc --- /dev/null +++ b/ipcap-codegen/src/lib.rs @@ -0,0 +1,164 @@ +use crate::names::name_to_enum_name; +use std::fs; + +mod names; + +const NAMES_DATA: &str = include_str!("../countries-names.txt"); +const CODES_2_DATA: &str = include_str!("../countries-two.txt"); +const CODES_3_DATA: &str = include_str!("../countries-three.txt"); +const COUNTRIES_TO_CONTINENTS: &str = include_str!("../countries-to-continents.txt"); +const DMA: &str = include_str!("../dma.txt"); + +trait PushMut { + fn add(self, item: T) -> Self; +} + +impl PushMut for Vec { + fn add(self, item: T) -> Self { + let mut new = self; + new.push(item); + new + } +} + +macro_rules! codes { + ($data: expr, $enum_names: expr) => { + &$data + .split('\n') + .enumerate() + .map(|(i, code)| format!("Country::{} => \"{}\"", $enum_names[i], code)) + .collect::>() + .join(",\n") + }; +} + +macro_rules! codes_reverse { + ($data: expr, $enum_names: expr) => {{ + $data + .split('\n') + .enumerate() + .map(|(i, code)| format!("\"{}\" => Some(Country::{})", code, $enum_names[i])) + .collect::>() + .add("_ => None".to_string()) + .join(",\n") + }}; +} + +macro_rules! _save_content { + ($data: expr, $file_name: expr) => { + let out_dir = std::env::var("OUT_DIR").unwrap(); + fs::write( + format!("{out_dir}/{}", $file_name), + $data + ) + .unwrap(); + }; +} + +macro_rules! save_content { + (result $data: expr, $value: expr, $file_name: expr) => { + _save_content!( + format!( + r#"Ok(match {} {{ + {} + }}?)"#, + $value, + $data + ), + $file_name + ) + }; + + ($data: expr, $value: expr, $file_name: expr) => { + _save_content!( + format!( + r#"match {} {{ + {} + }}"#, + $value, + $data + ), + $file_name + ) + }; +} + +macro_rules! codes_with_fs_write { + (reverse $data: expr, $enum_names: expr, $file_name: expr) => { + save_content!(codes_reverse!($data, $enum_names), "value", $file_name); + }; + + ($data: expr, $enum_names: expr, $file_name: expr) => { + save_content!(codes!($data, $enum_names), "self", $file_name); + }; +} + +pub fn run() { + const OFFSET: u8 = 1; + let names_by_line = NAMES_DATA.split("\n"); + let enum_names = names_by_line + .clone() + .map(name_to_enum_name) + .collect::>(); + + let out_dir = std::env::var("OUT_DIR").unwrap(); + + let enum_content = &*enum_names.join(", \n\t"); + fs::write( + format!("{out_dir}/countries-enum-values"), + format!( + r#" + #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] + pub enum Country {{ + {enum_content} + }}"# + ), + ) + .unwrap(); + + codes_with_fs_write!(CODES_2_DATA, enum_names, "countries-codes-2"); + codes_with_fs_write!(CODES_3_DATA, enum_names, "countries-codes-3"); + codes_with_fs_write!(reverse CODES_2_DATA, enum_names, "countries-codes-2-reverse"); + codes_with_fs_write!(reverse CODES_3_DATA, enum_names, "countries-codes-3-reverse"); + + let match_pattern = names_by_line + .clone() + .enumerate() + .map(|(i, name)| format!("Country::{} => f.write_str(\"{name}\")", enum_names[i])) + .collect::>() + .join(",\n"); + save_content!(result match_pattern, "self", "countries-to-names"); + + let match_pattern = enum_names + .iter() + .enumerate() + .map(|(i, name)| format!("{} => Some(Country::{name})", i as u8 + OFFSET)) + .collect::>() + .add("_ => None".to_string()) + .join(",\n"); + save_content!(match_pattern, "value", "countries-from-buffer"); + + let match_pattern = COUNTRIES_TO_CONTINENTS + .split('\n') + .enumerate() + .map(|(i, continent)| { + if continent.is_empty() { + format!("Country::{} => None", enum_names[i]) + } else { + format!("Country::{} => Some(Continent::{continent})", enum_names[i]) + } + }) + .collect::>() + .join(",\n"); + save_content!(match_pattern, "value", "country-to-continent"); + + let match_pattern = DMA.split('\n') + .map(|dma| { + let data: Vec<&str> = dma.split("; ").collect(); + format!("{} => f.write_str(\"{}\")", data[0], data[1]) + }) + .collect::>() + .add("_ => f.write_str(\"Unknown DMA\")".to_string()) + .join(",\n"); + save_content!(match_pattern, "value", "dma-code-to-name"); +} diff --git a/ipcap-codegen/src/names.rs b/ipcap-codegen/src/names.rs new file mode 100644 index 0000000..935bc69 --- /dev/null +++ b/ipcap-codegen/src/names.rs @@ -0,0 +1,29 @@ +pub fn name_to_enum_name(name: &str) -> String { + let before_coma = name.split(",").collect::>()[0]; + String::from_iter( + remove_content_in_brackets(before_coma) + .chars() + .filter(|char| char.is_alphabetic()), + ) +} + +fn remove_content_in_brackets(content: &str) -> String { + let mut is_open = false; + let mut result = String::new(); + + for char in content.chars() { + if is_open { + if char == ')' { + is_open = false + } + } else { + if char == '(' { + is_open = true + } else { + result.push(char) + } + } + } + + result +} diff --git a/src/designated_market_area.rs b/src/designated_market_area.rs index ee3e13b..0339048 100755 --- a/src/designated_market_area.rs +++ b/src/designated_market_area.rs @@ -1,5 +1,5 @@ -use std::fmt::{Display, Formatter}; use crate::codegen; +use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub struct DesignatedMarketArea(pub u32); diff --git a/src/geo_ip_reader.rs b/src/geo_ip_reader.rs index 809861f..44d39f7 100755 --- a/src/geo_ip_reader.rs +++ b/src/geo_ip_reader.rs @@ -366,7 +366,9 @@ where } Some(DesignatedMarketArea(dma_area)) - } else { None }; + } else { + None + }; let time_zone = time_zone_by_country( country.alphabetic_code_2(), diff --git a/src/lib.rs b/src/lib.rs index 12083f8..67f1ab1 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ //! //! ```toml //! [dependencies] -//! ipcap = "0.1.2" +//! ipcap = "0.1.5" //! ``` //! //! 2. Use the `IpCap` struct to perform IP lookup without internet access: @@ -26,14 +26,14 @@ //! let record = geo_ip.get_record("108.95.4.105"); //! //! let expected_value = Record { -//! dma: Some(DesignatedMarketArea(825858)), -//! postal_code: Some("92109".into()), -//! country: Country::UnitedStates, -//! region_code: Some("CA".into()), -//! city: Some("San Diego".into()), -//! latitude: 32.79769999999999, -//! longitude: -117.23349999999999, -//! time_zone: "America/Los_Angeles" +//! dma: Some(DesignatedMarketArea(825858)), +//! postal_code: Some("92109".into()), +//! country: Country::UnitedStates, +//! region_code: Some("CA".into()), +//! city: Some("San Diego".into()), +//! latitude: 32.79769999999999, +//! longitude: -117.23349999999999, +//! time_zone: "America/Los_Angeles" //! }; //! //! assert_eq!(record, expected_value); diff --git a/src/utils.rs b/src/utils.rs index b76b41f..fb8e74c 100755 --- a/src/utils.rs +++ b/src/utils.rs @@ -216,11 +216,23 @@ pub fn pretty_print_dict(record: Record) { ("dma_code", record.dma.map(|d| d.dma_code().to_string())), ("area_code", record.dma.map(|d| d.area_code().to_string())), ("metro_code", record.dma.map(|c| c.to_string())), - ("postal_code", record.postal_code.as_ref().map(|d| d.to_string())), - ("country_code", Some(record.country.alphabetic_code_2().to_string())), - ("country_code3", Some(record.country.alphabetic_code_3().to_string())), + ( + "postal_code", + record.postal_code.as_ref().map(|d| d.to_string()), + ), + ( + "country_code", + Some(record.country.alphabetic_code_2().to_string()), + ), + ( + "country_code3", + Some(record.country.alphabetic_code_3().to_string()), + ), ("country_name", Some(record.country.to_string())), - ("continent", record.country.continent().map(|c| c.to_string())), + ( + "continent", + record.country.continent().map(|c| c.to_string()), + ), ("region_code", record.region_code.map(|d| d.to_string())), ("city", record.city.map(|d| d.to_string())), ("latitude", Some(record.latitude.to_string())),