packet_parser 2.0.2

A powerful and modular Rust crate for network packet parsing.
Documentation
// Copyright (c) 2026 Cyprien Avico avicocyprien@yahoo.com
//
// Licensed under the MIT License <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.

use std::fmt;

use crate::parse::application::protocols::dns::utils::{dns_class::DnsClass, dns_types::DnsType};

// more can be a list of this possible struct (those strcut may on may not be on the liste: "more"):
#[derive(Debug)]
pub struct Answer {
    name: String,           // Domain name
    answer_type: DnsType,   // Type of record (e.g., A, AAAA, MX, etc.)
    answer_class: DnsClass, // Class of record (typically IN for Internet)
    ttl: u32,               // Time to live
    data_length: u16,       // Length of the data
    address: Vec<u8>,       // Address or other data (variable length)
}

impl fmt::Display for Answer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Answer {{ name: {}, answer_type: {}, answer_class: {}, ttl: {}, data_length: {}, address: {:?} }}",
            self.name,
            self.answer_type,
            self.answer_class,
            self.ttl,
            self.data_length,
            self.address
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::application::protocols::dns::utils::{
        dns_class::DnsClasses, dns_types::DnsTypes,
    };

    #[test]
    fn test_display() {
        let record = Answer {
            name: "example.com".to_string(),
            answer_type: DnsTypes::A,
            answer_class: DnsClasses::IN,
            ttl: 300,
            data_length: 4,
            address: vec![93, 184, 216, 34],
        };

        let rendered = record.to_string();
        assert!(rendered.starts_with("Answer {"));
        assert!(rendered.contains("name: example.com"));
        assert!(rendered.contains("answer_type: A"));
        assert!(rendered.contains("answer_class: IN"));
        assert!(rendered.contains("ttl: 300"));
        assert!(rendered.contains("data_length: 4"));
        assert!(rendered.contains("address: [93, 184, 216, 34]"));
    }
}