hamcall 0.2.1

Ham Radio callsign analyzer and ClubLog XML parser
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Analyzer for callsigns based on the data from the ClubLog XML to get further information like the callsigns entity.
//!
//! The example `call.rs` shows the basic usage of this module.

use crate::clublog::{Adif, CallsignException, CqZone, Prefix, ADIF_ID_NO_DXCC};
use crate::clublogquery::ClubLogQuery;
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use regex::Regex;
use thiserror::Error;

/// Representation of a callsign together with detailed information like the name of the entity or the ADIF DXCC identifier.
#[derive(Debug, PartialEq)]
pub struct Callsign {
    /// Complete callsign
    pub call: String,
    /// ADIF DXCC identifier
    pub adif: Adif,
    /// Name of entity
    pub dxcc: Option<String>,
    /// CQ zone
    pub cqzone: Option<CqZone>,
    /// Continent
    pub continent: Option<String>,
    /// Longitude
    pub longitude: Option<f32>,
    /// Latitude
    pub latitude: Option<f32>,
}

impl Callsign {
    /// Check if callsign is assigned to no DXCC (like for /AM, /MM or /SAT)
    ///
    /// # Arguments
    ///
    /// (None)
    ///
    /// # Returns
    ///
    /// True if the callsign is assigned to no DXCC
    pub fn is_special_entity(&self) -> bool {
        self.adif == ADIF_ID_NO_DXCC
    }

    /// Instantiate a new callsign that does not belong to an entity (e.g. /MM, /AM, /SAT)
    ///
    /// # Arguments
    ///
    /// - `call`: Callsign
    ///
    /// # Returns
    ///
    /// Callsign struct
    fn new_special_entity(call: &str) -> Callsign {
        Callsign {
            call: String::from(call),
            adif: ADIF_ID_NO_DXCC,
            dxcc: None,
            cqzone: None,
            continent: None,
            longitude: None,
            latitude: None,
        }
    }

    /// Instantiate a new callsign from a ClubLog prefix
    ///
    /// # Arguments
    ///
    /// - `call`: Callsign
    /// - `prefix`: Callsign exception entry
    ///
    /// # Returns
    ///
    /// Callsign struct
    fn from_prefix(call: &str, prefix: &Prefix) -> Callsign {
        Callsign {
            call: String::from(call),
            adif: prefix.adif,
            dxcc: Some(prefix.entity.clone()),
            cqzone: prefix.cqz,
            continent: prefix.cont.clone(),
            longitude: prefix.long,
            latitude: prefix.lat,
        }
    }

    /// Instantiate a new callsign from a ClubLog callsign exception
    ///
    /// # Arguments
    ///
    /// - `call`: Callsign
    /// - `exc`: Callsign exception entry
    ///
    /// # Returns
    ///
    /// Callsign struct
    fn from_exception(call: &str, exc: &CallsignException) -> Callsign {
        Callsign {
            call: String::from(call),
            adif: exc.adif,
            dxcc: Some(exc.entity.clone()),
            cqzone: exc.cqz,
            continent: exc.cont.clone(),
            longitude: exc.long,
            latitude: exc.lat,
        }
    }
}

/// Possible reasons for an invalid callsign
#[derive(Error, Debug, PartialEq)]
pub enum CallsignError {
    /// Callsign is of invalid format or includes invalid characters
    #[error("Callsign is of invalid format or includes invalid characters")]
    BasicFormat,

    /// Callsign was used in an invalid operation
    #[error("Callsign was used in an invalid operation")]
    InvalidOperation,

    /// Callsign does not begin with a valid prefix
    #[error("Callsign does not begin with a valid prefix")]
    BeginWithoutPrefix,

    /// Too much prefixes
    #[error("Too much prefixes")]
    TooMuchPrefixes,

    /// Multiple special appendices like /MM, /AM or /6, /8, ...
    #[error("Multiple special appendices")]
    MultipleSpecialAppendices,
}

/// Special appendices that may not be interpreted as prefixes
const APPENDIX_SPECIAL: [&str; 7] = ["AM", "MM", "SAT", "P", "M", "QRP", "LH"];

/// Type of split
#[derive(PartialEq, Eq)]
enum PartType {
    /// Prefix
    Prefix,
    /// Everything other than a prefix
    Other,
}

/// State of the call element classification statemachine
#[derive(PartialEq, Eq)]
enum State {
    /// No prefix found so far
    NoPrefix,
    /// Single prefix
    SinglePrefix,
    /// Double prefix
    DoublePrefix,
    /// Found complete prefix, only appendices may follow
    PrefixComplete(u8),
}

/// Check if the callsign is whitelisted if the whitelist option is enabled for the entity of the callsign at the given point in time.
///
/// # Arguments
///
/// - `clublog`: Reference to ClubLog data
/// - `call`: Callsign to check
/// - `timestamp`: Timestamp to use for the check
///
/// # Returns
///
/// Returns true if the callsign is valid or false if whitelisting for that entity is enabled and the callsign is not on the whitelist.
pub fn check_whitelist(
    clublog: &dyn ClubLogQuery,
    call: &Callsign,
    timestamp: &DateTime<Utc>,
) -> bool {
    // Get entity for adif identifier
    // Note that not all valid adif identifiers refer to an entity (e.g. aeronautical mobile calls)
    if let Some(entity) = clublog.get_entity(call.adif, timestamp) {
        // Check if whitelisting is enabled
        if entity.whitelist == Some(true) {
            // Check if an exception for the call at the given point in time is present
            if let Some(prefix) = clublog.get_callsign_exception(&call.call, timestamp) {
                // There may be a callsign exception for a whitelisted entity but the exception refers a different adif identifier
                return prefix.adif == call.adif;
            }

            // Check if the given point in time is before the start of whitelisting for that entity
            if let Some(whitelist_start) = entity.whitelist_start {
                if *timestamp < whitelist_start {
                    return true;
                }
            }

            // Check if the given point in time is after the end of whitelisting for that entity
            if let Some(whitelist_end) = entity.whitelist_end {
                if *timestamp > whitelist_end {
                    return true;
                }
            }

            return false;
        }
    }

    true
}

/// Analyze callsign to get further information like the name of the entity or the AIDF DXCC identifier.
///
/// # Arguments:
///
/// - `clublog`: Reference to ClubLog data
/// - `call`: Callsign to analyze
/// - `timestamp`: Timestamp to use for the check
///
/// # Returns
///
/// Returns further information about the callsign or an error.
pub fn analyze_callsign(
    clublog: &dyn ClubLogQuery,
    call: &str,
    timestamp: &DateTime<Utc>,
) -> Result<Callsign, CallsignError> {
    // Strategy
    // Step 1: Check for an invalid operation
    // Step 2: Check for a callsign exception
    // Step 3: Classify each part of the callsign (split by '/') if it is a valid prefix or not
    // Step 4: Check for basic validity of the callsign by using the classification results and categorize the call into generic callsign structures
    // Step 5: Handle the call based on the determined category

    lazy_static! {
        static ref RE_COMPLETE_CALL: Regex = Regex::new(r"^[A-Z0-9]+[A-Z0-9/]*[A-Z0-9]+$").unwrap();
    }

    // Check that only allowed characters are present and the callsign does not begin or end with a /
    if !RE_COMPLETE_CALL.is_match(call) {
        return Err(CallsignError::BasicFormat);
    }

    // ### Step 1 ###
    // Check if the callsign was used in an invalid operation
    if clublog.is_invalid_operation(call, timestamp) {
        return Err(CallsignError::InvalidOperation);
    }

    // ### Step 2 ###
    // Check if clublog lists a callsign exception
    if let Some(call_exc) = clublog.get_callsign_exception(call, timestamp) {
        return Ok(Callsign::from_exception(call, call_exc));
    }

    // Split raw callsign into its parts
    let parts: Vec<&str> = call.split('/').collect();

    // ### Step 3 ###
    // Iterate through all parts of the callsign and check wether the part of the callsigns is a valid prefix or something else
    let mut parttypes: Vec<PartType> = Vec::with_capacity(parts.len());
    for (pos, part) in parts.iter().enumerate() {
        let pt = if get_prefix(clublog, part, timestamp, &parts[pos + 1..]).is_some() {
            // MM and AM may be valid prefixes or special appendices depending on the position within the complete callsign.
            // For example MM as a prefix evaluates to Scotland, MM as an appendix indicates a maritime mobile activation.
            // Special appendices are only valid as those if they are right at the beginning of the callsign.
            // Therefore ignore the first element of the call and check for special appendices beginning from the second element onwards.
            if pos >= 1 && APPENDIX_SPECIAL.contains(part) {
                PartType::Other
            } else {
                PartType::Prefix
            }
        } else {
            PartType::Other
        };
        parttypes.push(pt);
    }

    // ### Step 4 ###
    // Check for basic validity with a small statemachine.
    // For example check that the call begins with a prefix, has not too much prefixes, ...
    let mut state = State::NoPrefix;
    for parttype in parttypes.iter() {
        match (&state, parttype) {
            (State::NoPrefix, PartType::Prefix) => state = State::SinglePrefix,
            (State::NoPrefix, PartType::Other) => Err(CallsignError::BeginWithoutPrefix)?,
            (State::SinglePrefix, PartType::Prefix) => state = State::DoublePrefix,
            (State::SinglePrefix, PartType::Other) => state = State::PrefixComplete(1),
            (State::DoublePrefix, PartType::Prefix) => state = State::PrefixComplete(3),
            (State::DoublePrefix, PartType::Other) => state = State::PrefixComplete(2),
            (State::PrefixComplete(_), PartType::Prefix) => Err(CallsignError::TooMuchPrefixes)?,
            (State::PrefixComplete(_), PartType::Other) => (),
        }
    }

    // ### Step 5 ###
    match state {
        // The callsign consists of a single prefix and zero or more appendices
        State::SinglePrefix | State::PrefixComplete(1) => {
            // Complete homecall
            // Example: W1AW
            let homecall = &parts[0];

            // Prefix of the homecall
            // Example: W for the homecall W1AW
            // Unwrap is safe here, otherwise there is an internal error
            let mut homecall_prefix = get_prefix(clublog, homecall, timestamp, &parts[1..])
                .unwrap()
                .0;

            // Special appendix like /AM or /MM is present
            // Example: W1ABC/AM
            if is_no_entity_by_appendix(&parts[1..])? {
                return Ok(Callsign::new_special_entity(call));
            }

            // Check if a single digit appendix is present
            // If so, check if the single digit appendix changes the prefix to a different one
            // Example: "SV0ABC/9" where SV is Greece, but SV9 is Crete
            if let Some(pref) = is_different_prefix_by_single_digit_appendix(
                clublog,
                homecall,
                timestamp,
                &parts[1..],
            )? {
                homecall_prefix = pref;
            }

            // No special rule matched, just return information
            let mut callsign = Callsign::from_prefix(call, homecall_prefix);
            check_apply_cqzone_exception(clublog, &mut callsign, timestamp);
            Ok(callsign)
        }
        // The callsign consists of two prefixes and zero or more appendices
        State::DoublePrefix | State::PrefixComplete(2) => {
            // Get prefix information for both prefixes.
            let pref_first = get_prefix(clublog, parts[0], timestamp, &parts[1..]).unwrap();
            let pref_second = get_prefix(clublog, parts[1], timestamp, &parts[2..]).unwrap();

            // Check if the first prefix may be a valid special prefix like 3D2/R
            // Example: "3D2ABC/R" contains two valid prefixes at first sight, 3D2 and R but the first and second prefix together form the special prefix 3D2/R
            let pref = if pref_first.0.call.contains('/') {
                pref_first.0
            } else {
                // Decide which one to use by how many characters were removed from the potential prefix before it matched a prefix from the list.
                // The prefix which required less character removals wins.
                // This is probably not 100% correct, but seems good enough.
                if pref_first.1 <= pref_second.1 {
                    pref_first.0
                } else {
                    pref_second.0
                }
            };

            let mut callsign = Callsign::from_prefix(call, pref);
            check_apply_cqzone_exception(clublog, &mut callsign, timestamp);
            Ok(callsign)
        }
        // The callsign consists out of three prefixes and zero or more appendices
        // This is a very special case and only takes account of calls with a special prefix like 3D2/R and therefore callsigns like 3D2/W1ABC/R.
        // Calls like 3D2ABC/R are already covered, since there are only two potential valid prefixes.
        // The call 3D2/W1ABC/R contains three potential valid prefixes 3D2, W and R but 3D2/R is the actual prefix (according to my understanding of the special prefix annotation)
        State::PrefixComplete(3) => {
            let pref = get_prefix(clublog, parts[0], timestamp, &parts[1..]).unwrap();
            if pref.0.call.contains('/') {
                let mut callsign = Callsign::from_prefix(call, pref.0);
                check_apply_cqzone_exception(clublog, &mut callsign, timestamp);
                Ok(callsign)
            } else {
                Err(CallsignError::TooMuchPrefixes)
            }
        }
        _ => panic!("Internal error"),
    }
}

/// Check if a CQ zone exception exists based on the gathered callsign information.
/// If there is one, replace the CQ zone directly in the given callsign struct.
///
/// # Arguments
///
/// - `clublog`: Reference to ClubLog data
/// - `call`: Gathered callsign information
/// - `timestamp`: Timestamp to use for the check
///
/// # Returns
/// (None)
fn check_apply_cqzone_exception(
    clublog: &dyn ClubLogQuery,
    call: &mut Callsign,
    timestamp: &DateTime<Utc>,
) {
    if let Some(cqz) = clublog.get_zone_exception(&call.call, timestamp) {
        call.cqzone = Some(cqz);
    }
}

/// Check if the list of appendices contains an appendix with a single digit that may indicate a different prefix.
/// If there is such single digit appendix, replace the digit within the callsign and query the prefix information for the potential new prefix.
///
/// Example: "SV0ABC/9" where SV is Greece, but SV9 is Crete
///
/// # Arguments
///
/// - `clublog`: Reference to ClubLog data
/// - `homecall`: Part of the complete callsign that is assumend to be the homecall
/// - `timestamp`: Timestamp to use for the check
/// - `appendices`: List of appendices to the homecall
///
/// # Returns
///
/// A potential new prefix, `None` if nothing changed or an error.
fn is_different_prefix_by_single_digit_appendix<'a>(
    clublog: &'a dyn ClubLogQuery,
    homecall: &str,
    timestamp: &DateTime<Utc>,
    appendices: &[&str],
) -> Result<Option<&'a Prefix>, CallsignError> {
    lazy_static! {
        static ref RE: Regex = Regex::new(r"^([A-Z0-9]+)(\d)([A-Z0-9]+)$").unwrap();
    }

    // Search for single digits in the list of appendices
    let single_digits: Vec<&&str> = appendices
        .iter()
        .filter(|e| {
            if e.len() == 1 {
                e.chars().next().unwrap().is_numeric()
            } else {
                false
            }
        })
        .collect();

    // Act based on how much single digit appendices were found
    let new_digit = match single_digits.len() {
        // Nothing to do if there is no single digit
        0 => return Ok(None),
        // If there is only a single digit, take it
        1 => single_digits[0],
        // For multiple single digits throw an error -> not sure which one to choose? Ignoring all would also be unexpected behaviour
        _ => return Err(CallsignError::MultipleSpecialAppendices),
    };

    // Assemble potential new intermediate call that will be used to check for a potential different prefix
    let new_homecall = RE.replace(homecall, format!("${{1}}{}${{3}}", new_digit));

    Ok(get_prefix(clublog, &new_homecall, timestamp, appendices).map(|i| i.0))
}

/// Check if a special appendix (`MM`, `AM`, `SAT`) is part of the appendices list.
/// If such a speical appendix is present, it indicates that the actual prefix of the overall call shall be ignored.
///
/// Example: /MM indicates maritime mobile and therefore does not reference an entity
///
/// # Arguments
///
/// - `appendices`: List of callsign appendices, like `QRP`, `5`, ...
///
/// # Returns
///
/// True if a special prefix is present, false if not. Otherwise an error is returned.
fn is_no_entity_by_appendix(appendices: &[&str]) -> Result<bool, CallsignError> {
    let special_cnt = appendices
        .iter()
        .filter(|e| **e == "MM" || **e == "AM" || **e == "SAT")
        .count();

    // Act based on how much special appendices were found
    match special_cnt {
        // Zero found, nothing to do
        0 => Ok(false),
        // Single one found, return it
        1 => Ok(true),
        // Multiple found, throw an error -> which one to choose?
        _ => Err(CallsignError::MultipleSpecialAppendices),
    }
}

/// Search for a matching prefix by brutforcing all possibilities.
/// The potential prefix will be shortened char by char from the back until a prefix matches.
/// Furthermore, to take in account of prefixes like SV/A, append all single char appendices to the end of the potential prefix before checking for a match.
///
/// # Arguments
///
/// - `clublog`: Reference to ClubLog data
/// - `potential_prefix`: Potential prefix to check against the data
/// - `timestamp`: Timestamp to use for the check
/// - `appendices`: List of callsign appendices, like `QRP`, `5`, ...
///
/// # Returns
///
/// If there is a match, next to the prefix information the number of removed chars is returned.
fn get_prefix<'a>(
    clublog: &'a dyn ClubLogQuery,
    potential_prefix: &str,
    timestamp: &DateTime<Utc>,
    appendices: &[&str],
) -> Option<(&'a Prefix, usize)> {
    let len_potential_prefix = potential_prefix.len();
    assert!(len_potential_prefix >= 1);

    // Search for single char appendices
    // For example SV/A is a valid prefix but indicates a different entity as the prefix SV
    let single_char_appendices: Vec<&&str> = appendices
        .iter()
        .filter(|e| {
            if e.len() == 1 {
                e.chars().next().unwrap().is_alphabetic()
            } else {
                false
            }
        })
        .collect();

    // Bruteforce all possibilities
    // Shortening the call from the back is required to due to calls like UA9ABC where both prefixes U and UA9 a potential matches,
    // but the more explicit one is the correct one.
    let mut prefix: Option<(&Prefix, usize)> = None;
    for cnt in (1..len_potential_prefix + 1).rev() {
        // Shortened call
        let slice = &potential_prefix[0..cnt];

        // Append all single chars to the call as <call>/<appendix> and check if the prefix is valid
        // This check is required for prefixes like SV/A where the callsign SV1ABC/A shall match too
        if let Some(pref) = single_char_appendices
            .iter()
            .find_map(|a| clublog.get_prefix(&format!("{}/{}", slice, a), timestamp))
        {
            prefix = Some((pref, len_potential_prefix - cnt));
            break;
        }

        // Check if prefix is valid
        if let Some(pref) = clublog.get_prefix(slice, timestamp) {
            prefix = Some((pref, len_potential_prefix - cnt));
            break;
        }
    }

    prefix
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{clublog::ClubLog, clublogmap::ClubLogMap};
    use lazy_static::lazy_static;
    use std::fs;

    fn read_clublog_xml() -> &'static dyn ClubLogQuery {
        lazy_static! {
            static ref CLUBLOG: ClubLogMap = ClubLogMap::from(
                ClubLog::parse(&fs::read_to_string("data/clublog/cty.xml").unwrap()).unwrap()
            );
        }

        &*CLUBLOG
    }

    #[test]
    fn clublog_prefix_entity_invalid() {
        let calls = vec!["X5ABC", "X5ABC/P", "X5/W1AW", "X5/W1AW/P"];

        let clublog = read_clublog_xml();
        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call,
                &DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
                    .unwrap()
                    .into(),
            );
            assert_eq!(res, Err(CallsignError::BeginWithoutPrefix));
        }
    }

    #[test]
    fn clublog_special_appendix() {
        let calls = vec![
            ("KB5SIW/STS50", "2020-01-01T00:00:00Z"), // test for call exception record 2730
            ("ZY0RK", "1994-08-20T00:00:00Z"),        // test for callsign exception record 28169
        ];

        let clublog = read_clublog_xml();
        for call in calls.iter() {
            println!("Test for: {}", call.0);
            let res = analyze_callsign(
                clublog,
                call.0,
                &DateTime::parse_from_rfc3339(call.1).unwrap().into(),
            )
            .unwrap();
            assert!(res.is_special_entity());
        }
    }

    #[test]
    fn clublog_whitelist() {
        let params = vec![
            ("KH4AB", "1980-04-07T00:00:00Z", true), // Timestamp after start of whitelist and call is part of exception list
            ("KH4AB", "1981-01-01T00:00:00Z", false), // Timestamp after start of whitelist and call not part of exception list
        ];

        let clublog = read_clublog_xml();

        for param in params.iter() {
            println!("Test for: {}", param.0);
            let timestamp = &DateTime::parse_from_rfc3339(param.1).unwrap().into();
            let call = analyze_callsign(clublog, param.0, timestamp).unwrap();
            let res = check_whitelist(clublog, &call, timestamp);
            assert_eq!(param.2, res);
        }
    }

    #[test]
    fn special_appendix_ok() {
        let calls = vec![
            // AM
            "W1AW/AM",
            "W1AM/P/AM",
            "W1AW/AM/P",
            "W1AW/P/AM/7",
            // MM
            "W1AW/MM",
            "W1AM/P/MM",
            "W1AW/MM/P",
            "W1AW/P/MM/7",
            // SAT
            "W1AW/SAT",
            "W1AM/P/SAT",
            "W1AW/SAT/P",
            "W1AW/P/SAT/7",
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call,
                &DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
                    .unwrap()
                    .into(),
            )
            .unwrap();
            assert!(res.is_special_entity());
        }
    }

    #[test]
    fn special_appendix_err() {
        let calls = vec![
            // AM
            "W1AW/AM/SAT",
            "W1AM/AM/MM",
            "W1AW/AM/AM",
            "W1AW/AM/MM/P",
            "W1AW/P/AM/MM",
            // MM
            "W1AW/MM/SAT",
            "W1AM/MM/MM",
            "W1AW/MM/AM",
            "W1AW/MM/MM/P",
            "W1AW/P/MM/AM",
            // SAT
            "W1AW/SAT/SAT",
            "W1AM/SAT/MM",
            "W1AW/SAT/AM",
            "W1AW/SAT/MM/P",
            "W1AW/P/SAT/AM",
            // Multiple numbers
            "W1AW/8/9",
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call,
                &DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
                    .unwrap()
                    .into(),
            );
            assert_eq!(res, Err(CallsignError::MultipleSpecialAppendices));
        }
    }

    #[test]
    fn special_entity_prefix() {
        let calls = vec![
            ("SV1ABC/A", "2020-01-01T00:00:00Z", 180),    // Prefix SV/A
            ("SV2/W1AW/A", "2020-01-01T00:00:00Z", 180),  // Prefix SV/A
            ("3D2ABC/R", "2020-01-01T00:00:00Z", 460), // Prefix 3D2/R, where 3D2 and R are potential valid prefixes too
            ("3D2/W1ABC/R", "2020-01-01T00:00:00Z", 460), // Prefix 3D2/R, where 3D2 and R are potential valid prefixes too
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call.0,
                &DateTime::parse_from_rfc3339(call.1).unwrap().into(),
            )
            .unwrap();
            assert_eq!(res.adif, call.2);
        }
    }

    #[test]
    fn cqzone_exception() {
        let calls = vec![
            ("W1CBY/VE8", "1993-07-01T00:00:00Z", 1), // Record 548
            ("VE2BQB", "1992-01-01T00:00:00Z", 2),    // Record 35
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call.0,
                &DateTime::parse_from_rfc3339(call.1).unwrap().into(),
            )
            .unwrap();
            assert_eq!(res.cqzone.unwrap(), call.2);
        }
    }

    #[test]
    fn call_exceptions() {
        let calls = vec![
            ("AM70URE/8", "2019-05-01T00:00:00Z", 29),
            ("EA8VK/URE", "2021-01-01T00:00:00Z", 29),
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call.0,
                &DateTime::parse_from_rfc3339(call.1).unwrap().into(),
            )
            .unwrap();
            assert_eq!(res.adif, call.2);
        }
    }

    #[test]
    fn invalid_operation() {
        let calls = vec![
            ("T8T", "1995-05-01T01:00:00Z"),       // record 490
            ("3D2/N1GXE", "2021-01-01T00:00:00Z"), // record 1155
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call.0,
                &DateTime::parse_from_rfc3339(call.1).unwrap().into(),
            );
            assert_eq!(res, Err(CallsignError::InvalidOperation));
        }
    }

    #[test]
    fn genuine_calls() {
        let calls = vec![
            ("W1ABC", 291),     // Basic call
            ("9A1ABC", 497),    // Call beginning with a number
            ("A71AB", 376),     // Call with two digits, one belonging to the prefix
            ("LM2T70Y", 266),   // Call with two separated numbers
            ("UA9ABC", 15),     // Check that the call is not matched for the prefix U
            ("U1ABC", 54),      // Counterexample for the test call above
            ("SV0ABC/9", 40),   // SV is Greece, but SV9 is Crete
            ("UA0JL/6", 54),    // UA0 is Asiatic Russia, but UA6 is European Russia
            ("MM/W1AW", 279),   // MM is Scotland and not Maritime Mobile
            ("F/W1AW", 227),    // F is France
            ("CE0Y/W1ABC", 47), // CE0Y is Easter Island, but CE would be Chile
            ("W1ABC/CE0Y", 47), // CE0Y is Easter Island, but CE would be Chile
            ("RW0A", 15),       // Call is also a prefix
            ("LS4AA/F", 227),   // LS is Argentina but F is France
            ("VE3LYC/KL7", 6),  // KL is Alaska
        ];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            println!("Test for: {}", call.0);
            let res = analyze_callsign(
                clublog,
                call.0,
                &DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
                    .unwrap()
                    .into(),
            )
            .unwrap();
            assert_eq!(res.adif, call.1);
        }
    }

    #[test]
    fn invalid_format() {
        let calls = vec!["W1AW/", "/W1AW", "W1ABC.", "W1ABC/.", "W1<ABC>"];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call,
                &DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
                    .unwrap()
                    .into(),
            );
            assert_eq!(res, Err(CallsignError::BasicFormat));
        }
    }

    #[test]
    fn too_much_prefixes() {
        let calls = vec!["W/K/W1AW", "W1AW/K/W", "K/W1AW/W"];

        let clublog = read_clublog_xml();

        for call in calls.iter() {
            let res = analyze_callsign(
                clublog,
                call,
                &DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
                    .unwrap()
                    .into(),
            );
            assert_eq!(res, Err(CallsignError::TooMuchPrefixes));
        }
    }
}