use bytemuck::{Pod, Zeroable};
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct DrvHeader {
pub magic: [u8; 4],
pub version: u16,
pub flags: u16,
pub section_count: u32,
pub checksum: u32,
}
impl DrvHeader {
pub fn new(section_count: u32) -> Self {
Self {
magic: *b"DRV\0",
version: super::DRV_VERSION,
flags: 0,
section_count,
checksum: 0,
}
}
pub fn validate(&self) -> crate::Result<()> {
if &self.magic != b"DRV\0" {
return Err(crate::DrivenError::InvalidBinary(
"Invalid magic bytes".to_string(),
));
}
if self.version > super::DRV_VERSION {
return Err(crate::DrivenError::InvalidBinary(format!(
"Unsupported version: {} (max supported: {})",
self.version,
super::DRV_VERSION
)));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum RuleCategory {
Style = 0,
Naming = 1,
ErrorHandling = 2,
Testing = 3,
Documentation = 4,
Security = 5,
Performance = 6,
Architecture = 7,
Imports = 8,
Git = 9,
Api = 10,
Other = 255,
}
impl From<u8> for RuleCategory {
fn from(value: u8) -> Self {
match value {
0 => RuleCategory::Style,
1 => RuleCategory::Naming,
2 => RuleCategory::ErrorHandling,
3 => RuleCategory::Testing,
4 => RuleCategory::Documentation,
5 => RuleCategory::Security,
6 => RuleCategory::Performance,
7 => RuleCategory::Architecture,
8 => RuleCategory::Imports,
9 => RuleCategory::Git,
10 => RuleCategory::Api,
_ => RuleCategory::Other,
}
}
}
#[derive(Debug, Clone)]
pub struct RuleEntry {
pub category: RuleCategory,
pub priority: u8,
pub description_idx: u32,
pub pattern_idx: u32,
}
#[derive(Debug, Clone, Default)]
pub struct PersonaSection {
pub name_idx: u32,
pub role_idx: u32,
pub identity_idx: u32,
pub style_idx: u32,
pub traits: Vec<u32>,
pub principles: Vec<u32>,
}
#[derive(Debug, Clone, Default)]
pub struct StandardsSection {
pub rules: Vec<RuleEntry>,
}
#[derive(Debug, Clone, Default)]
pub struct ContextSection {
pub include_patterns: Vec<u32>,
pub exclude_patterns: Vec<u32>,
pub focus_areas: Vec<u32>,
pub dependencies: Vec<(u32, u32)>,
}
#[derive(Debug, Clone)]
pub struct WorkflowStep {
pub name_idx: u32,
pub description_idx: u32,
pub condition_idx: u32,
pub actions: Vec<u32>,
}
#[derive(Debug, Clone, Default)]
pub struct WorkflowSection {
pub name_idx: u32,
pub steps: Vec<WorkflowStep>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_header_size() {
assert_eq!(std::mem::size_of::<DrvHeader>(), 16);
}
#[test]
fn test_header_validation() {
let valid = DrvHeader::new(3);
assert!(valid.validate().is_ok());
let invalid = DrvHeader {
magic: *b"BAD\0",
..valid
};
assert!(invalid.validate().is_err());
}
#[test]
fn test_rule_category_roundtrip() {
for i in 0..=10 {
let cat = RuleCategory::from(i);
assert_eq!(cat as u8, i);
}
assert_eq!(RuleCategory::from(100), RuleCategory::Other);
}
}