asterix_parser 0.1.1

Playground do Protocolo ASTERIX
Documentation
use std::fmt;

use super::uap_json::structures::Edition;
use super::uap::providers::Provider;

#[derive(Debug, Clone)]
pub struct CustomCategoryError {
    message: String
}

impl fmt::Display for CustomCategoryError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Ord, PartialOrd)]
pub enum  CategoryIndex {
    _034,
    _048,
}

impl CategoryIndex {
    pub fn as_str(&self) -> &'static str {
        match self {
            CategoryIndex::_034 => "034",
            CategoryIndex::_048 => "048",
        }
    }

    pub fn as_string(&self) -> String {
        self.as_str().to_string()
    }

    pub fn as_u8(&self) -> u8 {
        match self {
            CategoryIndex::_034 => 34,
            CategoryIndex::_048 => 48,
       }
    }
    
    pub fn from_u8(category: u8) -> Option<CategoryIndex> {
        if category == 34 { 
            return Some(CategoryIndex::_034);
        } else { 
            if category == 48 { 
                return Some(CategoryIndex::_048);
            } 
        }
        None
    }

    pub fn from_str(category_str: &str) -> Option<&Self> {
        match category_str {
            "034" => Some(&CategoryIndex::_034),
            "048" => Some(&CategoryIndex::_048),
            _ => None
        }
    }
}

/// The data exchanged over the communication medium between the different users shall be
/// organised in Data Categories.
/// Those Data Categories describe the data to be exchanged and the format to be applied. The Data
/// Categories shall be standardised and applied by all users of ASTERIX.
/// <br>
/// The purpose of such a classification shall be to:<br>
/// • allow easy identification and subsequent processing of the data;<br>
/// • facilitate the dispatching of the data to the appropriate application task in the receiving unit;<br>
/// 
/// The Category symbolic reference shall consist of a six-character reference of the form CATnnn.<br>
/// 
/// where:<br>
/// • CAT: is a keyword meaning Category;<br>
/// • nnn: is a three digit decimal number which indicates the Data  Category (000 to 255).<br>
/// (from document:  [EUROCONTROL-SPEC-0149](https://www.eurocontrol.int/asterix))<br>
/// 
///  Note: For this library, only implemented categories will be available 
/// 
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Category {
    pub key: CategoryKey,
    pub description: String
}

impl Category {
    pub fn new(key: CategoryKey, description: &str) -> Category {
        Category {
            key,
            description: description.to_string(),
        }        
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CategoryKey {
    pub index: CategoryIndex,
    pub edition: Edition,
    pub provider: Provider
}