nomad-common 0.1.0

Shared types and protocol for Nomad web extraction engine
Documentation
use serde::{Deserialize, Serialize};

// ── Viewport ──

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Viewport {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

// ── Style bitfield ──

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style(pub u64);

impl Style {
    pub const fn text_color_idx(self) -> u8 {
        (self.0 & 0xFF) as u8
    }
    pub const fn bg_color_idx(self) -> u8 {
        ((self.0 >> 8) & 0xFF) as u8
    }
    pub const fn font_size(self) -> u8 {
        ((self.0 >> 16) & 0xFF) as u8
    }
    pub const fn text_align(self) -> u8 {
        ((self.0 >> 24) & 0x3F) as u8
    }
    pub const fn display_type(self) -> u8 {
        ((self.0 >> 30) & 0x3F) as u8
    }
    pub const fn position_type(self) -> u8 {
        ((self.0 >> 36) & 0x3F) as u8
    }
    /// Role as u8 index (0 = no role). Bits 42-49.
    pub const fn role_idx(self) -> u8 {
        ((self.0 >> 42) & 0xFF) as u8
    }
    /// Build Style with a role index.
    pub const fn with_role(self, role: u8) -> Self {
        Style((self.0 & !(0xFF << 42)) | ((role as u64) << 42))
    }
    pub const fn flags(self) -> u8 {
        ((self.0 >> 56) & 0xFF) as u8
    }

    pub const fn is_link(self) -> bool {
        (self.flags() & 1) != 0
    }
    pub const fn is_image(self) -> bool {
        (self.flags() & 2) != 0
    }
    pub const fn is_input(self) -> bool {
        (self.flags() & 4) != 0
    }
    pub const fn is_clickable(self) -> bool {
        (self.flags() & 8) != 0
    }
}

// ── Display / Position constants ──

pub mod display {
    pub const BLOCK: u8 = 0;
    pub const INLINE: u8 = 1;
    pub const INLINE_BLOCK: u8 = 2;
    pub const NONE: u8 = 3;
}

pub mod position {
    pub const STATIC: u8 = 0;
    pub const RELATIVE: u8 = 1;
    pub const ABSOLUTE: u8 = 2;
    pub const FIXED: u8 = 3;
}

// ── State tags for delta compression ──

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub struct StateTag(pub u8);

impl StateTag {
    pub const STATIC: StateTag  = StateTag(0);
    pub const MOVED: StateTag   = StateTag(1);
    pub const CHANGED: StateTag = StateTag(2);
    pub const REMOVED: StateTag = StateTag(3);
}

// ── Default palette indices ──

pub const PALETTE_TEXT_DEFAULT: u8 = 0;
pub const PALETTE_BG_DEFAULT: u8 = 1;
pub const PALETTE_LINK: u8 = 2;
pub const PALETTE_CHROME_BG: u8 = 3;
pub const PALETTE_URL_BAR_BG: u8 = 4;
pub const PALETTE_ACTIVE_TAB: u8 = 5;
pub const PALETTE_INACTIVE_TAB: u8 = 6;
pub const PALETTE_SEPARATOR: u8 = 7;

pub const PALETTE: &[u32; 16] = &[
    0x000000, // TEXT_DEFAULT
    0xFFFFFF, // BG_DEFAULT
    0x0000FF, // LINK
    0xE0E0E0, // CHROME_BG
    0xFFFFFF, // URL_BAR_BG
    0xFFFFFF, // ACTIVE_TAB
    0xD0D0D0, // INACTIVE_TAB
    0xA0A0A0, // SEPARATOR
    0x000000, // 8+
    0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
];

pub fn palette_color(idx: u8) -> u32 {
    PALETTE[(idx as usize).min(15)]
}

// ── Defaults ──

pub const DEFAULT_FONT_SIZE: u8 = 16;