hamcty 0.2.0

Load and search country data for ham radio callsigns in the CTY database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Load and search in a CTY database
//!
//! licensed under CC BY-SA 4.0,
//! To view a copy of this license, visit <http://creativecommons.org/licenses/by-sa/4.0/>
//!
//! Author: Andreas, <df1asc@darc.de>
//!
//! # Example
//!
//! ```
//! use hamcty::CountryDatabase;
//!
//! fn main() {
//!     // Use crate internal data (maybe not up to date)...
//!     let cdb = CountryDatabase::new();
//!     // ...or load data from path (you decide about the version)
//!     let cdb = CountryDatabase::from_path("test_data/cty.csv").expect("Could not load cty data");
//!
//!     let call = "DF1ASC";
//!     assert_eq!(cdb.country(call).unwrap().code, "DL");
//!     assert_eq!(cdb.prefix(call), Ok("DF".to_string()));
//!  }
//! ```

use regex::Regex;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::LazyLock;
use std::{error, fs};

#[derive(Debug, PartialEq)]
pub enum Error {
    CtyFileError(String),
    CSVError(String),
    ParseError(String),
    PrefixNotFound,
    CountryCodeNotFound,
    CountryNotFound,
    VersionEntityNotFound,
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl error::Error for Error {}

/// Ham radio country code
pub type CountryCode = String;

/// Continents
#[derive(Debug, Clone, PartialEq)]
pub enum Continent {
    NorthAmerica,
    SouthAmerica,
    Europe,
    Africa,
    Oceania,
    Asia,
    Antarctica,
    Unknown(String),
}

impl From<&str> for Continent {
    /// Converts two letter continent codes to `Continent`
    fn from(value: &str) -> Self {
        match value.to_uppercase().as_str() {
            "NA" => Continent::NorthAmerica,
            "SA" => Continent::SouthAmerica,
            "EU" => Continent::Europe,
            "AF" => Continent::Africa,
            "OC" => Continent::Oceania,
            "AS" => Continent::Asia,
            "AN" => Continent::Antarctica,
            _ => Continent::Unknown(value.to_string()),
        }
    }
}

impl Display for Continent {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Continent::NorthAmerica => "NA",
                Continent::SouthAmerica => "SA",
                Continent::Europe => "EU",
                Continent::Africa => "AF",
                Continent::Oceania => "OC",
                Continent::Asia => "AS",
                Continent::Antarctica => "AN",
                Continent::Unknown(cont) => cont,
            }
        )
    }
}

/// DXCC entity code
pub type DXCCEntity = u16;

/// CQ zone number
pub type CQZone = u8;

/// ITU zone number
pub type ITUZone = u8;

/// Coordinates for a location
#[derive(Debug, Clone, PartialEq)]
pub struct Location {
    pub lat: f32,
    pub lon: f32,
}

/// UTC time offset
pub type TimeOffset = f32;

/// Holds a country information set
#[derive(Debug, Clone, PartialEq)]
pub struct Country {
    /// Ham radio country code
    pub code: CountryCode,
    /// English country name
    pub name: String,
    /// DXCC country code
    pub dxcc: DXCCEntity,
    /// The continent
    pub continent: Continent,
    /// CQ Zone number
    pub cq: CQZone,
    /// ITU Zone number
    pub itu: ITUZone,
    /// Latitude and longitude of country center
    pub location: Location,
    /// Time offset from UTC
    pub time_off: TimeOffset,
}

impl Display for Country {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// Maps the Ham radio country code to the country information
pub type CountryMap = HashMap<CountryCode, Country>;

/// Holds mapping data to mapping prefixes or calls to a country
#[derive(Debug, Clone)]
struct OverrideData {
    /// Ham radio country code
    code: CountryCode,
    /// CQ Zone Override
    cq: Option<CQZone>,
    /// ITU Zone override
    itu: Option<ITUZone>,
}

//noinspection RegExpRedundantEscape
static RE_OVERRIDES: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"([A-Z0-9/=]+)(?:\((\d+)\))?(?:\[(\d+)\])?.*").unwrap());

/// Holds the complete country database
pub struct CountryDatabase {
    /// Mapping for country code to country
    countries: CountryMap,
    /// Mapping for callsign prefixes to country code
    prefix_overrides: HashMap<String, OverrideData>,
    /// Prefixes as sorted list
    prefixes: Vec<String>,
    /// Mapping for callsign overrides to country code
    call_overrides: HashMap<String, OverrideData>,
    /// The data version
    version: String,
}

impl CountryDatabase {
    /// Create a country database from included CTY data
    pub fn new() -> Self {
        // This should not fail, because it is tested to load
        Self::load(include_str!("../cty/cty.csv").as_bytes()).unwrap()
    }

    /// Loads a cty.csv (either normal or big) from a path
    pub fn from_path(path: impl AsRef<str>) -> Result<Self, Error> {
        Self::load(
            fs::read(path.as_ref())
                .map_err(|e| Error::CtyFileError(e.to_string()))?
                .as_ref(),
        )
    }

    /// Load CTY CSV data
    fn load(data: &[u8]) -> Result<Self, Error> {
        let mut countries = CountryMap::new();
        let mut prefix_overrides: HashMap<String, OverrideData> = HashMap::new();
        let mut call_overrides: HashMap<String, OverrideData> = HashMap::new();
        let mut version = String::new();

        let mut reader = csv::ReaderBuilder::new()
            .delimiter(b',')
            .has_headers(false)
            .from_reader(data);

        for res in reader.records() {
            //1A,Sov Mil Order of Malta,246,EU,15,28,41.90,-12.43,-1.0,1A;
            let record = res.map_err(|e| Error::CSVError(e.to_string()))?;
            let code = record.get(0).unwrap_or_default().replace("*", "");

            countries.insert(
                code.to_string(),
                Country {
                    code: code.to_string(),
                    name: record.get(1).unwrap_or_default().to_string(),
                    dxcc: record.get(2).unwrap_or_default().parse().map_err(|_| {
                        Error::ParseError(format!("DXCC for country code '{code}'"))
                    })?,
                    continent: record.get(3).unwrap_or_default().into(),
                    cq: record.get(4).unwrap_or_default().parse().map_err(|_| {
                        Error::ParseError(format!("CQ zone for country code '{code}'"))
                    })?,
                    itu: record.get(5).unwrap_or_default().parse().map_err(|_| {
                        Error::ParseError(format!("ITU zone for country code '{code}'"))
                    })?,
                    location: Location {
                        lat: record.get(6).unwrap_or_default().parse().map_err(|_| {
                            Error::ParseError(format!("longitude for country code '{code}'"))
                        })?,
                        lon: record.get(7).unwrap_or_default().parse().map_err(|_| {
                            Error::ParseError(format!("latitude for country code '{code}'"))
                        })?,
                    },
                    time_off: record.get(8).unwrap_or_default().parse().map_err(|_| {
                        Error::ParseError(format!("time offset for country code '{code}'"))
                    })?,
                },
            );

            for pfx in record
                .get(9)
                .unwrap_or_default()
                .replace(";", "")
                .split(" ")
            {
                let (pfx, cq, itu) = if let Some(cap) = Regex::captures(&RE_OVERRIDES, pfx) {
                    (
                        cap.get(1).map(|v| v.as_str().to_string()),
                        cap.get(2).map(|v| v.as_str().parse::<CQZone>().unwrap()),
                        cap.get(3).map(|v| v.as_str().parse::<ITUZone>().unwrap()),
                    )
                } else {
                    (None, None, None)
                };

                let Some(pfx) = pfx else { continue };

                if let Some(pfx) = pfx.strip_prefix("=") {
                    if code == "VE" && pfx.starts_with("VER") {
                        version = pfx.to_string();
                        continue;
                    }
                    if !call_overrides.contains_key(pfx) {
                        call_overrides.insert(
                            pfx.to_string(),
                            OverrideData {
                                code: code.to_string(),
                                cq,
                                itu,
                            },
                        );
                    }
                } else {
                    prefix_overrides.insert(
                        pfx,
                        OverrideData {
                            code: code.to_string(),
                            cq,
                            itu,
                        },
                    );
                }
            }
        }

        let mut prefixes = prefix_overrides
            .clone()
            .keys()
            .map(|pfx| pfx.to_string())
            .collect::<Vec<String>>();
        prefixes.sort();

        Ok(Self {
            countries,
            prefix_overrides,
            prefixes,
            call_overrides,
            version,
        })
    }

    /// Returns the database version
    pub fn version(&self) -> &String {
        &self.version
    }

    /// Returns the version entity for verification purpose
    pub fn version_entity(&self) -> Result<(CountryCode, String), Error> {
        let cty = self
            .country("VERSION")
            .map_err(|_| Error::VersionEntityNotFound)?;
        Ok((cty.code, cty.name))
    }

    /// Finds the prefix for a callsign
    pub fn prefix(&self, call: impl AsRef<str>) -> Result<String, Error> {
        let mut part = String::new();
        let idx: usize = 0;
        let mut pfx = String::new();

        for c in call.as_ref().to_uppercase().chars() {
            part.push(c);
            if let Some(idx) = self.prefixes[idx..].iter().position(|r| r == &part) {
                pfx = self.prefixes[idx].to_string()
            } else {
                continue;
            }
        }

        if !pfx.is_empty() {
            Ok(pfx)
        } else {
            Err(Error::PrefixNotFound)
        }
    }

    /// Searches the country code and override data for a callsign
    /// If data is not found in callsigns, the data is searched in prefixes
    fn country_code(&self, call: impl AsRef<str>) -> Result<&OverrideData, Error> {
        if let Some(ord) = self.call_overrides.get(&call.as_ref().to_uppercase()) {
            return Ok(ord);
        }

        if let Some(ord) = self
            .prefix_overrides
            .get(&self.prefix(&call).map_err(|_| Error::CountryCodeNotFound)?)
        {
            return Ok(ord);
        }

        // Can never be reached: If prefix is found it is also in prefix overrides
        Err(Error::CountryCodeNotFound)
    }

    /// Fetches the `Country` data for a callsign
    pub fn country(&self, call: impl AsRef<str>) -> Result<Country, Error> {
        let data = self.country_code(&call)?;

        if let Some(cty) = self.countries.get(&data.code) {
            Ok(Country {
                cq: if let Some(cq) = data.cq { cq } else { cty.cq },
                itu: if let Some(itu) = data.itu {
                    itu
                } else {
                    cty.itu
                },
                ..cty.clone()
            })
        } else {
            Err(Error::CountryNotFound)
        }
    }
}

impl Default for CountryDatabase {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // The tests will only work with cty.csv from 3 June 2026 (CTY-3620)
    // VER20260603, Version entity is Kingdom of Eswatini, 3DA
    fn setup() -> CountryDatabase {
        CountryDatabase::from_path("test_data/cty.csv").expect("error loading test data")
    }

    #[test]
    fn test_100_version() {
        let cdb = setup();

        assert_eq!(cdb.version(), "VER20260603");
        assert_eq!(
            cdb.version_entity(),
            Ok(("3DA".to_string(), "Kingdom of Eswatini".to_string()))
        );
    }

    // Test that the correct version is included in the binary
    // Adapt this to the version and entity of the data provided in `cty`
    #[test]
    fn test_101_cur_version() {
        let cdb = CountryDatabase::new();

        assert_eq!(cdb.version(), "VER20260611");
        assert_eq!(
            cdb.version_entity(),
            Ok(("ZK3".to_string(), "Tokelau Islands".to_string()))
        );
    }

    #[test]
    fn test_110_prefix() {
        let cdb = setup();

        assert_eq!(cdb.prefix("DF1ASC"), Ok("DF".to_string()));
        assert_eq!(cdb.prefix("DK50K32MR"), Ok("DK".to_string()));
        assert_eq!(cdb.prefix("XX1XX"), Err(Error::PrefixNotFound));
    }

    #[test]
    fn test_120_search() {
        let cdb = setup();

        let cty = cdb.country("DF1ASC").unwrap();
        assert_eq!(cty.code, "DL");
        assert_eq!(cty.continent, Continent::Europe);

        assert_eq!(cdb.country("XX1XX"), Err(Error::CountryCodeNotFound));
    }

    #[test]
    fn test_130_search_w_override() {
        let cdb = setup();

        // Override country for call
        let cty = cdb.country("LU1ZA").unwrap();
        assert_eq!(cty.code, "VP8/o");
        assert_eq!(cty.continent, Continent::SouthAmerica);

        // Override ITU and CQ for call
        let cty = cdb.country("4U1WB").unwrap();
        assert_eq!(cty.code, "K");
        assert_eq!(cty.continent, Continent::NorthAmerica);
        assert_eq!(cty.cq, 5);
        assert_eq!(cty.itu, 8);

        // Override ITU for prefix
        let cty = cdb.country("PP6XX").unwrap();
        assert_eq!(cty.code, "PY");
        assert_eq!(cty.continent, Continent::SouthAmerica);
        assert_eq!(cty.itu, 13);
        assert_eq!(cty.cq, 11);
    }
}