asterix_parser 0.1.1

Playground do Protocolo ASTERIX
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
use std::collections::BTreeMap;
use std::fs::OpenOptions;
use std::io::Read;
use asterix_parser::asterix::uap::providers::Provider;
use asterix_parser::asterix::uap_json::enums::{AttributeItem, AttributeType, AttributeValueType, ConstraintType, DataItemType, DataItemValueType, DataType, DenominatorType, ElementType, ElementValueType, LsbType, Uap};
use asterix_parser::asterix::uap_json::structures::{DataItemRule, DataRecord};
use asterix_parser::asterix::uap_json::traits::Describe;
use bitvec::prelude::*;
use json::stringify;
use thiserror::Error;

use asterix_parser::asterix::category::{Category, CategoryIndex, CategoryKey};
use asterix_parser::asterix::Context;

#[derive(Error, Debug)]
pub enum ParseError {
    #[error("category not found")]
    CategoryNotFound { category_code: String },
    #[error("Message informed length [{informed}] doesn't match current length [{current}]")]
    BadMessageInformedLength { informed: usize, current: usize },
    #[error("UAP definition not found for {uap_name}")]
    UapDefinitionNotFound { uap_name: String },
    #[error("Parser unforeseen dependency relation")]
    ParserUnforeseenDependecyRelation,
    #[error("Could not open UAP JSON definition file")]
    OpenUapJsonDefinitionError
}

#[derive(Default, Clone)]
pub struct FSpecInfo { 
    pub len: u8, 
    pub fspec:BTreeMap<u8, bool> 
}

#[derive(Clone)]
pub struct AttributeConstraint {
    constraint_type: ConstraintType,
    limit_value: AttributeConstraintLimit 
}

#[derive(Clone)]
pub enum AttributeConstraintLimit {
    SignedInteger { value: i32 },
}

#[derive(Clone)]
pub enum AttributeLsb {
    Integer { value: i32 },
    Real { value: f64 }
}

#[derive(Default, Clone) ]
pub struct Attribute {
    pub name: String,
    pub title: String,
    pub bits: BitVec<u8, Msb0>,
    pub value_type: String,
    pub signed: Option<bool>,
    pub constraints: Option<Vec<AttributeConstraint>>,
    pub lsb: Option<AttributeLsb>,
    pub unit: Option<String>,
    pub description: Option<String>
}

#[test]
pub(crate) fn test_asterix_parse() -> anyhow::Result<()> {
    let _cat034_1_29_definition = "resources/uaps/cat034_1_29_definition.json";
    let _cat048_1_30_definition = "resources/uaps/cat048_1_30_definition.json";
    let _cat048_1_31_definition = "resources/uaps/cat048_1_31_definition.json";
    let my_file_name = _cat048_1_31_definition;

    let mut myfile = match OpenOptions::new()
        .read(true)
        .write(false)
        .create(false)
        .open(my_file_name) {
            Ok(f) => f,
            Err(e) => panic!("Error opening file {}", e),
        };

    let mut raw_json = String::new();
    myfile.read_to_string(&mut raw_json).unwrap();

    let trimmed_json = raw_json.trim();
    let deserialized:DataRecord  = match serde_json::from_str(&trimmed_json) {
        Ok(d) => d,
        Err(_) => { return Err(ParseError::OpenUapJsonDefinitionError.into())}
    };

    let mut indent_level = 0 as usize;

    let _description = deserialized.describe(&mut indent_level);

    let mut message_octets: Vec<u8> = vec![];
    
    if my_file_name.contains("048") {
        message_octets
            .append(&mut vec![
                0x30,                   // CAT048 
                0x00, 0x30,             // LEN: 48 octets 
                0xFD, 0xF7, 0x02,       // FSPEC 1, 2, 3 :    
                0x19, 0xC9,             // 010: SAC/SIC
                0x35, 0x6D, 0x4D,       // 140: Time of day
                0xA0,                   // 020: Target Report Desc
                0xC5, 0xAF, 0xF1, 0xE0, // 040: Position in Polar Co-ordinates
                0x02, 0x00,             // 070: Mode-3/A Code 
                0x05, 0x28,             // 090: Flight Leve
                0x3C, 0x66, 0x0C,       // 220: Aircraft Address
                0x10, 0xC2, 0x36, 0xD4, 
                0x18, 0x20,             // 240: Aircraft Identification
                0x01, 0xC0, 0x78, 0x00, 
                0x31, 0xBC, 0x00, 0x00, 
                0x40,                   // 250: BDS Register Data
                0x0D, 0xEB,             // 161: Track Number
                0x07, 0xB9, 0x58, 0x2E, // 200: Calculated Track Velocity
                0x41, 0x00,             // 170: Track Status 
                0x20, 0xF5              // 230: ACAS Capability and Flight Status
            ]);
    } else {
        message_octets
            .append(&mut vec![
                0x22,                   // CAT034
                0x00, 0x10,             // LEN: 48 octets 
                0xf6,                   // FSPEC 1
                0x19, 0x0e,             // 010, Data Source Identifier
                0x02,                   // 000, Message Type 
                0x3a, 0x69,0x2b,        // 030, Time of Day
                0x40,                   // 020, Sector Number
                0x88, 0x40, 0x40,       // 050, System Configuration and Status
                0x80, 0x00              // 060, System Processing Mode
            ]);                            
    }

    let message = parse_message(&deserialized, &message_octets)?;

    describe_message_attributes(message);
    // describe_message{message};


    Ok(())
    // println!("Message description: "); 
    // println!("{description}");
}

fn describe_message_attributes(message: BTreeMap<String, Attribute>) {
    for attribute in message {
        println!();
        println!("key:[{}], bits length[{:02}] - bits:{}]", attribute.0, attribute.1.bits.len(), attribute.1.bits);
        print!("          Title: {:?}", attribute.1.title);
        if attribute.1.description.is_some()
        {
            print!(", description{:?}", attribute.1.description);
        }
        println!();

        if attribute.1.signed.is_some() {
            println!("          Signed: {}", attribute.1.signed.unwrap());
        }

        if attribute.1.unit.is_some() {
            println!("          Unit: {}", attribute.1.unit.unwrap());
        }

        if attribute.1.constraints.is_some() {
            match  attribute.1.constraints {
                Some(c) => {
                    for constraint in c  {
                        let constraint_str =
                            match constraint.constraint_type {
                                ConstraintType::Less => { stringify!(ConstraintType::Less ) },
                                ConstraintType::Great =>  { stringify!(ConstraintType::Great ) },
                                ConstraintType::LessOrEqual =>  { stringify!(ConstraintType::LessOrEqual ) },
                                ConstraintType::GreatOrEqual =>  { stringify!(ConstraintType::GreatOrEqual ) },
                            };
                        let constraint_value = 
                            match constraint.limit_value {
                                AttributeConstraintLimit::SignedInteger { value } => value.to_string(),
                            };
                        println!("          Constraint: {} {} ", constraint_str, constraint_value);
                    }
                },
                None => (),
            }
        } 
        if attribute.1.lsb.is_some() {
            match  attribute.1.lsb {
                Some(e) => {
                    let lsb_value = match e {
                        AttributeLsb::Integer { value } => value.to_string(),
                        AttributeLsb::Real { value } => format!("{:15.7}", value),
                    };

                    println!("          LSB: {} ", lsb_value);
                },
                None => (),
            }
        }
    }
}


fn parse_message(data_record: &DataRecord, octets: &Vec<u8>) -> anyhow::Result<BTreeMap<String, Attribute>> {
    let mut attributes_map = BTreeMap::<String, Attribute>::new();
    let asterix_context = Context::new()?;
    let cat_index = match CategoryIndex::from_u8(data_record.number) {
        Some(c) => c,
        None =>  return Err(ParseError::CategoryNotFound { category_code: data_record.number.to_string() }.into())
    };

    let category_key = CategoryKey { 
        index: cat_index, 
        edition: data_record.edition.clone(), 
        provider: Provider::Standard };
    let category = get_category(asterix_context, category_key, octets[0])?;
    
    let mut category_attribute = Attribute::default();
    category_attribute.name = category.key.index.as_string();
    category_attribute.description = Some(category.description);
    attributes_map.insert(stringify("message_category"), category_attribute);

    let octets_self_intormed_length = octets[1] as usize * 256 + octets[2] as usize;
    if octets_self_intormed_length != octets.len() {
        return Err(ParseError::BadMessageInformedLength { 
                informed: octets_self_intormed_length,  
                current: octets.len()
            }.into());
    }
    
    let category_prefix = format!("{}", category.key.index.as_str());

    let mut current_index = 3 as usize;
    let expected_frns = data_record.catalogue.len() as u8;
    let fspec_info: FSpecInfo = get_fspec_array(octets, current_index, expected_frns);
    current_index +=  fspec_info.len as usize;

    describe_fspec(&fspec_info);
    
    let data_items_map = find_present_data_item_definitions(
        &fspec_info.fspec, 
        &data_record.uap, 
        &data_record.catalogue)?;

    
    list_present_data_item_definitions(&data_items_map);
    
    let mut frn_bits = BTreeMap::<u8, BitVec<u8,Msb0>>::new();

    let mut calc_len_current_index = usize::from(current_index);
    for data_item_entry in &data_items_map {
        let data_item_length = 
            match &data_item_entry.1.rule {
                DataItemType::ContextFree { value } => {
                    let primary_field_length = match value {
                        DataItemValueType::Element { rule:_, size } => usize::from(*size),
                        DataItemValueType::Group { items } => {
                            find_group_length_in_bits(items)?
                        },
                        DataItemValueType::Extended { items } => {
                            find_extended_length_in_bits(items, octets, calc_len_current_index)?
                        }, 
                        DataItemValueType::Repetitive { rep, variation} => {
                            find_repetitive_length_in_bits(rep, variation, octets, calc_len_current_index)?
                        }, 
                        DataItemValueType::Compound { fspec, items } =>  {
                            find_compound_length_in_bits(fspec, items, octets, calc_len_current_index)?
                        }, 
                        DataItemValueType::Explicit { expl:_ } => 0_usize, // to calculate,
                    };

                    primary_field_length
                },
        };
        assert_eq!(0, data_item_length%8);

        let start_octet = calc_len_current_index;
        let end_octet = start_octet + (data_item_length/8);
        let bits = octets[start_octet..end_octet].view_bits::<Msb0>().to_bitvec();
        
        // println!("Found bits for data item [{}], bits{}", data_item_entry.1.name, bits.to_string());
        frn_bits.insert(*data_item_entry.0, bits);

        calc_len_current_index += data_item_length/8;
    }

    assert_eq!(octets_self_intormed_length, calc_len_current_index);

    attributes_map = find_message_attributes(&category_prefix, &data_items_map, &frn_bits)?;

    Ok(attributes_map)
}

fn find_message_attributes(
    category_prefix: &String, 
    data_items_map: &BTreeMap<u8, &DataItemRule>, 
    frn_bits: &BTreeMap<u8, BitVec<u8, Msb0>>) -> anyhow::Result<BTreeMap<String, Attribute>> {
    let mut found_attributes = BTreeMap::<String, Attribute>::new();
    for data_item_entry in data_items_map {
        let data_item_prefix = format!("I{}", &data_item_entry.1.name);
        let data_item_bits = frn_bits[data_item_entry.0].to_bitvec();
        let mut frn_consumed_bits = 0_usize;
        match &data_item_entry.1.rule {
            DataItemType::ContextFree { value } => match value {
                DataItemValueType::Element { rule, size }
                    => {
                        let mut attribute = Attribute::default();
                        // Direct Element on a dataitem has no name, so we will give one based on the title
                        attribute.name = format!("{}_{}_{}", *category_prefix,  data_item_prefix.as_str() ,
                                         data_item_entry.1.title.replace(" ", "_").to_uppercase().as_str());
                        let last_bit = frn_consumed_bits + *size as usize;
                        attribute.bits = data_item_bits[frn_consumed_bits..last_bit].to_bitvec();
                        fill_dataitem_element_attributes(rule, &mut attribute);
                        found_attributes.insert(attribute.name.to_owned(), attribute);
                    },
                DataItemValueType::Group { items } 
                    => {
                        let group_attributes =
                            fill_group_attributes(&category_prefix, &data_item_prefix, items,  &mut frn_consumed_bits, data_item_bits)?;
                        for attribute in group_attributes {
                            found_attributes.insert(attribute.name.to_owned(), attribute);
                        }
                },
                DataItemValueType::Extended { items:_ } => (),
                DataItemValueType::Repetitive { rep:_, variation:_ } => (),
                DataItemValueType::Compound { fspec:_, items:_ } => (),
                DataItemValueType::Explicit { expl:_ } => (),
            },
        }
    }

    Ok(found_attributes)
}

fn fill_group_attributes(
    category_prefix: &str, 
    data_item_prefix: &str, 
    items: &Vec<Option<AttributeItem>>, 
    frn_consumed_bits: &mut usize,
    data_item_bits: BitVec<u8, Msb0>) -> anyhow::Result<Vec<Attribute>> {
    let mut group_attributes = Vec::<Attribute>::new();
    for item in items {
        let inner_item = match item {
            Some(e) => e,
            None => continue,
        };

        
        match inner_item {
            AttributeItem::SpareEntry { length, spare:_ } => { 
                *frn_consumed_bits += length; 
                continue 
            }  ,
            AttributeItem::AttributeEntry(v) => { 
                let mut attribute = Attribute::default();
                attribute.name = format!("{}_{}_{}", category_prefix, data_item_prefix, v.name);
                attribute.title = v.title.to_owned();
                attribute.description = v.description.to_owned();
                match &v.rule {
                    AttributeType::ContextFree { value } => match value {
                        AttributeValueType::Element { rule:_, size } => {
                            let start_bit = *frn_consumed_bits;
                            let last_bit = *frn_consumed_bits + *size as usize;
                            attribute.bits = data_item_bits[start_bit..last_bit].to_bitvec();
                            *frn_consumed_bits = last_bit;
                        },
                        _ => { return Err(ParseError::ParserUnforeseenDependecyRelation.into())}
                    },
                }
                group_attributes.push(attribute);

            }, 
        };
    }

    Ok(group_attributes)
}

fn fill_dataitem_element_attributes(
    rule: &ElementType, 
    attribute: &mut Attribute) {
    match rule {
        ElementType::ContextFree { value } => match value {
            ElementValueType::Fx => (),
            ElementValueType::Integer { constraints:_,  signed:_} => (),
            ElementValueType::Quantity { constraints, lsb, signed, unit } 
                => {
                    let mut found_constraints = Vec::<AttributeConstraint>::new();
                    for constraint in constraints {
                        let constraint_value = match constraint.value {
                            DataType::Integer { value } => value,
                        };
                        let attribute_constraint = AttributeConstraint {
                            constraint_type: constraint.constraint_type,
                            limit_value: AttributeConstraintLimit::SignedInteger { value: constraint_value }
                        };
                        found_constraints.push(attribute_constraint);
                    }
                    if found_constraints.len() > 0 {
                        attribute.constraints = Some(found_constraints);
                    }
                    attribute.signed = Some(*signed);
                    let lsb = 
                        match lsb {
                            LsbType::Integer { value } => AttributeLsb::Integer { value: *value },
                            LsbType::Div { denominator, numerator } => {
                                let num = match numerator {
                                    DataType::Integer { value } => *value as f64,
                                };
                                let den = match denominator {
                                    DenominatorType::Pow { base, exponent } => {
                                    base.powf(*exponent)
                                    },
                                };
                                AttributeLsb::Real { value: num/den }
                            },
                        };
                    attribute.lsb = Some(lsb);
                    attribute.unit = Some(unit.to_owned());
                },
            ElementValueType::Raw => (),
            ElementValueType::Regular { size:_ } => (),
            ElementValueType::String { variation:_ } => (),
            ElementValueType::Table { values:_ } => (),
        },
    }
}

fn find_compound_length_in_bits(
    _fspec: &Option<u8>, 
    items: &Vec<Option<AttributeItem>>, 
    octets: &Vec<u8>, 
    calc_len_current_index: usize) -> anyhow::Result<usize> {
        
    // let mut local_len_current_index = calc_len_current_index;

    let minimum_expected_frns = 7_u8;
    let fspec_info: FSpecInfo = get_fspec_array(octets, calc_len_current_index, minimum_expected_frns);
    // local_len_current_index += fspec_info.len as usize;

    describe_fspec(&fspec_info);

    let mut item_count = 1_u8;
    let mut items_length =  usize::from(fspec_info.len)*8; // 8 bits in an fspec octet;
    for item in items {
        match item {
            Some(_) => (),
            None => {
                item_count += 1;
                continue;
            },
        };

        if fspec_info.fspec[&item_count] == false {
            item_count += 1;
            continue;
        }

        item_count += 1;
        items_length += find_attribute_item_length_in_bits(item)?
    }

    Ok(items_length)
}

fn find_repetitive_length_in_bits(
    rep: &ElementValueType, 
    variation: &AttributeValueType, 
    octets: &Vec<u8>, 
    calc_len_current_index: usize) -> anyhow::Result<usize> {
    
    let multiplier_bits = match rep {
        ElementValueType::Regular { size } => usize::from(*size),
        _ => { return Err(ParseError::ParserUnforeseenDependecyRelation.into())},
    };

    assert_eq!(0, multiplier_bits%8);
    let start_octet = calc_len_current_index;
    let end_octet = start_octet + (multiplier_bits/8);
    let bitslice = octets[start_octet..end_octet].view_bits::<Msb0>();
    let multiplier = bitslice.load::<usize>();
    let repetitive_len = 
        match variation {
            AttributeValueType::Group { items } => {
                find_group_length_in_bits(items)?
            }
            _ => { return Err(ParseError::ParserUnforeseenDependecyRelation.into())},
        };

        Ok(multiplier_bits + repetitive_len * multiplier)
}

fn find_extended_length_in_bits(
    items: &Vec<Option<AttributeItem>>, 
    octets: &Vec<u8>, 
    calc_len_current_index: usize) -> anyhow::Result<usize> {
    let mut local_current_index = calc_len_current_index;
    let mut item_len = 0_usize;
    let mut fx_set = true;
    for item in items {
        if !fx_set {
            break
        }
        item_len += find_attribute_item_length_in_bits(&item)?;

        if item_len%8 == 0 {
            // end of octed, will check fx value to continue or not
            let bits =  octets[local_current_index].view_bits::<Msb0>().to_bitvec();
            if bits[7] == true {
                local_current_index += 1;
            } else {
                fx_set = false;
            }
        }
    }    

    Ok(item_len)
}

fn find_attribute_item_length_in_bits(item: &Option<AttributeItem>) -> anyhow::Result<usize> {
    let item_length = match item {
        Some(i) => {
            let entry_len = match i {
                AttributeItem::SpareEntry { length, spare } => {
                    if *spare {*length } else { 0_usize }
                },
                AttributeItem::AttributeEntry(e) => {
                    let a_len = match &e.rule {
                        AttributeType::ContextFree { value } => {
                            let len = match value {
                                AttributeValueType::Element { rule:_, size } => usize::from(*size),
                                AttributeValueType::Group { items }  => find_group_length_in_bits(items)?, // to calculate,
                                _ => { return Err(ParseError::ParserUnforeseenDependecyRelation.into())}
                            };

                            len
                        },
                    };

                    a_len
                },
            };

            entry_len
        },
        None => 1_usize // would be 0_usize, but it happens that in exteded data item, the fx bit is falling into this category, 
                        // because it is null in .json definition file
    };

    Ok(item_length)
}

fn find_group_length_in_bits(items: &Vec<Option<AttributeItem>>) -> anyhow::Result<usize> {
    let mut len = 0_usize;
    for item in items {
        match item {
            Some(aitem) => {
                match aitem {
                    AttributeItem::SpareEntry { length, spare:_ } => { len += length; },
                    AttributeItem::AttributeEntry(aentry) => {
                        match &aentry.rule {
                            AttributeType::ContextFree { value } => match value {
                                AttributeValueType::Element { rule: _, size } => { len += usize::from(*size); },
                                _ => { return Err(ParseError::ParserUnforeseenDependecyRelation.into())}
                            },
                        }
                    },
                }
            },
            None => (),
        }
    }

    Ok(len)
}


fn get_category(context:Context, category_key: CategoryKey, category_octet: u8) -> anyhow::Result<Category> {
    let category = match context.categories.get(&category_key) {
        Some(c) => c.clone(),
        None => return Err((ParseError::CategoryNotFound { category_code: category_octet.to_string()}).into())
    };

    Ok(category.clone())
}

fn get_fspec_array(octets: &[u8], current_index: usize, expected_frns: u8) -> FSpecInfo {
    let mut fx = true;
    let mut frn:u8 = 0;
    let mut fspec_info = FSpecInfo::default();
    let mut local_current_index = current_index;
    fspec_info.len = 1;
    while fx {
        let octet = octets[local_current_index];
        let fx_octet = octet.view_bits::<Msb0>().to_bitvec();  

        let mut bit_count = 0;
        for bit in fx_octet {
            if bit_count == 7 {
                // if fx bit is true then we have more data_items to check
                fx = bit;
                if bit {
                    local_current_index += 1;
                    fspec_info.len += 1;
                }                
            }
            else {
                frn += 1;
                fspec_info.fspec.insert(frn, bit);
            }
            
            // println!("Bit[{bit_count}]:{bit}");
            bit_count += 1;
        }
    }

    while frn < expected_frns {
        frn += 1;
        fspec_info.fspec.insert(frn, false);
    }

    /* for fspecitem in &fspec_info.fspec {
        println!("{}: {}", fspecitem.0, fspecitem.1)
    } */

    fspec_info
}


fn describe_fspec(fspec_info: &FSpecInfo) {
    println!("FRN configuration: ");

    let mut frnh = String::new();
    let mut frnx = String::new();
    let mut frnl: String = String::new();
    for frn in &fspec_info.fspec {
        frnh += format!("| {:02}", frn.0).as_str();
        frnx += format!("| {:2}", if *frn.1 {"X"} else {" "}).as_str();
        frnl += "+---";
    }
    frnl += "+";
    frnh += "|";
    frnx += "|";
    
    println!("{}", frnl);
    println!("{}", frnh);
    println!("{}", frnl);
    println!("{}", frnx);
    println!("{}", frnl);
}

fn find_present_data_item_definitions<'a> (
    fspec: &'a BTreeMap<u8, bool>, 
    uap: &Uap,
    catalogue: &'a [DataItemRule]) -> anyhow::Result<BTreeMap<u8, &'a DataItemRule>> {
    let mut data_items_map = BTreeMap::<u8, &DataItemRule>::new();
    let uap_items = match &uap {
        Uap::Uap { items } => items,
    };

    /* let mut data_item_code = match uap_items.first() {
        Some(c) => c.clone(),
        None => { return Err(ParseError::UapDefinitionsEmpty.into()); },
    }; */

    for frn in fspec.into_iter().filter(| x | *x.1 ) {
        let index = (frn.0 - 1) as usize;
        let uap_data_item = format!("{}", &uap_items[usize::from(index)]);

        let catalog_entry 
            = catalogue.into_iter()
                .find(|x| x.name == uap_data_item);

        let data_item_def 
            = match catalog_entry {
                Some(d) => d,
                None => { return Err(ParseError::UapDefinitionNotFound { uap_name: uap_data_item}.into()); },
            };

        data_items_map.insert(*frn.0, data_item_def);
    }

    Ok(data_items_map)
}

fn list_present_data_item_definitions(data_items_map: &BTreeMap<u8, &DataItemRule>) {
    println!();
    println!("Data items present in the given message");
    for present_data_item in data_items_map {
        let p_frn = present_data_item.0;
        let p_data_item_rule = present_data_item.1;
        println!("FRN: {:02}: {}, {}", p_frn, p_data_item_rule.name, p_data_item_rule.title );
    }
    println!();
}

/*
fn process_items(attribute_prefix: &str, attributes_map: &mut BTreeMap<String, Attribute>, octets: &Vec<u8>, current_index: &mut usize, data_items_map: &BTreeMap<u8, &DataItemRule>) {
     for data_item_def in data_items_map {
        let attribute_key = format!("{}{}", attribute_prefix, data_item_def.1.name);
        let attributes = get_data_items_attributes(current_index, octets, data_item_def.1);
        for attribute in attributes {
            let key = format!("{}_{}_{}", attribute_prefix, data_item_def.1.name, attribute.name);
            attributes_map.insert(key, attribute);
        }
     }
}
fn get_data_items_attributes(current_index: &mut usize, octets: &Vec<u8>, data_item_def: &DataItemRule) -> Vec<Attribute> {
    let attributes: Vec<Attribute> =
        match &data_item_def.rule {
            DataItemType::ContextFree { value } => {
                let attributes: Vec<Attribute> =
                    match value {
                        DataItemValueType::Element { rule, size } => todo!(),
                        DataItemValueType::Group { items } 
                            => get_group_attributes(current_index, octets, value, &data_item_def.name),
                        DataItemValueType::Extended { items } => todo!(),
                        DataItemValueType::Repetitive { rep, variation } => todo!(),
                        DataItemValueType::Compound { fspec, items } => todo!(),
                        DataItemValueType::Explicit { expl } => todo!(),
                    };
                attributes
            }
        };

    attributes
}

fn get_group_attributes(current_index: &mut usize, octets: &Vec<u8>, value: &DataItemValueType, data_item_name: &String) -> Vec<Attribute> {
    let attributes =  Vec::<Attribute>::new();
        match value {
            DataItemValueType::Element { rule, size } 
                => process_element_attributes(current_index, octets, rule, *size as usize, data_item_name),
            DataItemValueType::Group { items } => todo!(),
            DataItemValueType::Extended { items } => todo!(),
            DataItemValueType::Repetitive { rep, variation } => todo!(),
            DataItemValueType::Compound { fspec, items } => todo!(),
            DataItemValueType::Explicit { expl } => todo!(),
        };

    attributes
}


fn process_element_attributes(current_index: &mut usize, octets: &Vec<u8>, rule: &ElementType, size: usize, data_item_name: &String) -> Vec<Attribute> {
    let mut attributes = Vec::<Attribute>::new();
    match rule {
        ElementType::ContextFree { value } => match value {
            ElementValueType::Fx =>  (), // todo: currently could not identify anything to return 
            // ElementValueType::Group { items } => todo!(),
            ElementValueType::Integer { constraints, signed } => todo!(),
            ElementValueType::Quantity { constraints, lsb, signed, unit } => todo!(),
            ElementValueType::Raw => {
                let last_octet_index = *current_index + (size/8) - 1;
                let element_octets = octets[*current_index..last_octet_index].to_vec();
                let extracted_bits = element_octets.view_bits::<Msb0>().to_bitvec();
                let attribute = Attribute {
                    name: data_item_name.clone(),
                    bits: extracted_bits,
                    value_type: "Raw".to_owned(),
                    unit: None,
                    description: None,
                    lsb: None,
                    title: String,
                    signed: None,
                    constraints: todo!(),
                };
                attributes.push(attribute)
            },
            ElementValueType::Regular { size } => todo!(),
            ElementValueType::String { variation } => todo!(),
            ElementValueType::Table { values } => todo!(),
        },
    }

    attributes
}

fn process_attribute_items(octets: &Vec<u8>, current_index: &mut usize, items: &Vec<AttributeRule>, data_item_name: &String) {
    for item in items {
        match &item.rule {
            AttributeType::ContextFree { value } => match value {
                AttributeValueType::Element { rule, size } => todo!(),
                AttributeValueType::Group { items } => todo!(),
                AttributeValueType::Repetitive { rep, variation } => todo!(),
                AttributeValueType::Table { values } => todo!(),
            }
        }
    }
} */