use std::fmt;

use super::*;
use crate::Formatter;

/// Network namespace event section.
#[event_section(SectionId::Netns)]
pub struct NetnsEvent {
    /// Unique identifier of the network namespace, generated by the kernel. See
    /// `bpf_get_netns_cookie` in `man 7 bpf-helpers`. It might not be available
    /// on older kernels.
    pub cookie: Option<u64>,
    /// Inum of the network namespace associated with the packet, from the
    /// device or the associated socket (in that order).
    pub inum: u32,
}

impl EventFmt for NetnsEvent {
    fn event_fmt(&self, f: &mut Formatter, _: &DisplayFormat) -> fmt::Result {
        if let Some(cookie) = &self.cookie {
            write!(f, "ns {:#x}/{}", cookie, self.inum)?;
        } else {
            write!(f, "ns {}", self.inum)?;
        }

        Ok(())
    }
}
