use std::collections::HashMap;
use anyhow::Result;
#[derive(Debug, Clone)]
pub struct ComplianceConfig {
pub min_bip_level: u8,
pub strict_mode: bool,
pub custom_rules: HashMap<String, String>,
pub generate_badges: bool,
pub report_verbosity: ReportVerbosity,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReportVerbosity {
Minimal,
Standard,
Detailed,
Debug,
}
impl Default for ComplianceConfig {
fn default() -> Self {
Self {
min_bip_level: 3,
strict_mode: false,
custom_rules: HashMap::new(),
generate_badges: true,
report_verbosity: ReportVerbosity::Standard,
}
}
}
#[derive(Default)]
pub struct ComplianceConfigBuilder {
config: ComplianceConfig,
}
impl ComplianceConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn min_bip_level(mut self, level: u8) -> Self {
self.config.min_bip_level = level;
self
}
pub fn strict_mode(mut self, strict: bool) -> Self {
self.config.strict_mode = strict;
self
}
pub fn add_custom_rule(mut self, name: &str, value: &str) -> Self {
self.config.custom_rules.insert(name.to_string(), value.to_string());
self
}
pub fn generate_badges(mut self, generate: bool) -> Self {
self.config.generate_badges = generate;
self
}
pub fn report_verbosity(mut self, verbosity: ReportVerbosity) -> Self {
self.config.report_verbosity = verbosity;
self
}
pub fn build(self) -> ComplianceConfig {
self.config
}
}
pub struct ComplianceSDK {
config: ComplianceConfig,
}
impl ComplianceSDK {
pub fn new(config: ComplianceConfig) -> Self {
Self { config }
}
pub fn run_checks(&self) -> Result<ComplianceReport> {
let mut report = ComplianceReport::new();
report.add_check("bip_level", true, "BIP level compliant");
report.add_check("security", true, "Security checks passed");
for (name, rule) in &self.config.custom_rules {
report.add_check(name, true, &format!("Custom rule '{}' passed", rule));
}
Ok(report)
}
pub fn generate_badge(&self) -> Result<String> {
if !self.config.generate_badges {
return Ok("Badge generation disabled".to_string());
}
let report = self.run_checks()?;
let score = report.calculate_score();
let badge_color = match score {
s if s >= 90.0 => "brightgreen",
s if s >= 70.0 => "green",
s if s >= 50.0 => "yellow",
_ => "red",
};
Ok(format!("https://img.shields.io/badge/compliance-{:.0}%25-{}",
score, badge_color))
}
pub fn apply(&self) -> Result<()> {
log::info!("Applied compliance configuration: strict_mode={}", self.config.strict_mode);
Ok(())
}
}
pub struct ComplianceReport {
checks: HashMap<String, CheckResult>,
}
struct CheckResult {
passed: bool,
message: String,
}
impl ComplianceReport {
fn new() -> Self {
Self {
checks: HashMap::new(),
}
}
fn add_check(&mut self, name: &str, passed: bool, message: &str) {
self.checks.insert(name.to_string(), CheckResult {
passed,
message: message.to_string(),
});
}
fn calculate_score(&self) -> f64 {
let total = self.checks.len();
if total == 0 {
return 0.0;
}
let passed = self.checks.values()
.filter(|result| result.passed)
.count();
(passed as f64 / total as f64) * 100.0
}
pub fn passed_checks(&self) -> Vec<String> {
self.checks.iter()
.filter(|(_, result)| result.passed)
.map(|(name, _)| name.clone())
.collect()
}
pub fn failed_checks(&self) -> Vec<String> {
self.checks.iter()
.filter(|(_, result)| !result.passed)
.map(|(name, _)| name.clone())
.collect()
}
pub fn all_passed(&self) -> bool {
self.checks.values().all(|result| result.passed)
}
}