kawaiifi 0.2.0

Wi-Fi scanning library for Linux, macOS, and Windows.
Documentation
use std::{
    ops::Deref,
    str::{self, Utf8Error},
};

use deku::{DekuRead, DekuWrite};
use serde::{Deserialize, Serialize};

use crate::ies::{Field, IeId};

#[derive(Debug, Clone, PartialEq, Eq, Hash, DekuRead, DekuWrite, Serialize, Deserialize)]
#[deku(ctx = "len: usize")]
pub struct Ssid {
    #[deku(count = "len")]
    ssid: Vec<u8>,
}

impl Ssid {
    pub const NAME: &'static str = "SSID";
    pub const ID: u8 = 0;
    pub const ID_EXT: Option<u8> = None;
    pub(crate) const IE_ID: IeId = IeId::new(Self::ID, Self::ID_EXT);

    pub fn as_str(&self) -> Result<&str, Utf8Error> {
        str::from_utf8(&self.ssid)
    }

    pub fn to_string_lossy(&self) -> String {
        String::from_utf8_lossy(&self.ssid).into_owned()
    }

    pub fn summary(&self) -> String {
        self.to_string_lossy()
    }

    pub fn fields(&self) -> Vec<Field> {
        vec![
            Field::builder()
                .title("SSID")
                .value(self.to_string_lossy())
                .bytes(self.ssid.clone())
                .build(),
        ]
    }
}

impl Deref for Ssid {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.ssid
    }
}