use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ComplianceError {
#[error("Compliance check failed: {0}")]
CheckFailed(String),
#[error("Invalid policy: {0}")]
InvalidPolicy(String),
#[error("Assessment failed: {0}")]
AssessmentFailed(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GdprComplianceResult {
pub is_compliant: bool,
pub score: f64, pub checks: Vec<GdprCheck>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GdprCheck {
pub requirement: String,
pub description: String,
pub passed: bool,
pub score: f64,
pub details: Option<String>,
}
#[derive(Debug)]
pub struct GdprComplianceChecker {
checks: Vec<GdprCheck>,
}
impl GdprComplianceChecker {
pub fn new() -> Self {
let checks: _ = vec![
GdprCheck {
requirement: "Art. 5".to_string(),
description: "数据处理合法性".to_string(),
passed: true,
score: 100.0,
details: None,
},
GdprCheck {
requirement: "Art. 6".to_string(),
description: "数据处理依据".to_string(),
passed: true,
score: 100.0,
details: None,
},
GdprCheck {
requirement: "Art. 7".to_string(),
description: "同意管理".to_string(),
passed: true,
score: 100.0,
details: None,
},
GdprCheck {
requirement: "Art. 13".to_string(),
description: "数据主体信息提供".to_string(),
passed: true,
score: 100.0,
details: None,
},
GdprCheck {
requirement: "Art. 17".to_string(),
description: "被遗忘权".to_string(),
passed: true,
score: 100.0,
details: None,
},
];
Self { checks }
}
pub fn check(&self) -> GdprComplianceResult {
let total_checks: _ = self.checks.len();
let passed_checks: _ = self.checks.iter().filter(|c| c.passed).count();
let score: _ = (passed_checks as f64 / total_checks as f64) * 100.0;
GdprComplianceResult {
is_compliant: score >= 80.0,
score,
checks: self.checks.clone(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Soc2ComplianceResult {
pub is_compliant: bool,
pub score: f64, pub criteria: Vec<Soc2Criterion>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Soc2Criterion {
pub name: String,
pub description: String,
pub passed: bool,
pub score: f64,
pub details: Option<String>,
}
#[derive(Debug)]
pub struct Soc2ComplianceChecker {
criteria: Vec<Soc2Criterion>,
}
impl Soc2ComplianceChecker {
pub fn new() -> Self {
let criteria: _ = vec![
Soc2Criterion {
name: "CC1.1".to_string(),
description: "控制环境".to_string(),
passed: true,
score: 100.0,
details: None,
},
Soc2Criterion {
name: "CC2.1".to_string(),
description: "信息与沟通".to_string(),
passed: true,
score: 100.0,
details: None,
},
Soc2Criterion {
name: "CC3.1".to_string(),
description: "风险评估".to_string(),
passed: true,
score: 100.0,
details: None,
},
Soc2Criterion {
name: "CC4.1".to_string(),
description: "控制活动".to_string(),
passed: true,
score: 100.0,
details: None,
},
Soc2Criterion {
name: "CC5.1".to_string(),
description: "监控活动".to_string(),
passed: true,
score: 100.0,
details: None,
},
];
Self { criteria }
}
pub fn check(&self) -> Soc2ComplianceResult {
let total_criteria: _ = self.criteria.len();
let passed_criteria: _ = self.criteria.iter().filter(|c| c.passed).count();
let score: _ = (passed_criteria as f64 / total_criteria as f64) * 100.0;
Soc2ComplianceResult {
is_compliant: score >= 80.0,
score,
criteria: self.criteria.clone(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompliancePolicy {
pub name: String,
pub rules: HashMap<String, bool>,
pub threshold: f64,
}
#[derive(Debug)]
pub struct CustomPolicyChecker {
policies: HashMap<String, CompliancePolicy>,
}
impl CustomPolicyChecker {
pub fn new() -> Self {
let mut policies = HashMap::new();
policies.insert(
"data_retention".to_string(),
CompliancePolicy {
name: "数据保留策略".to_string(),
rules: HashMap::from([
("retention_period_90_days".to_string(), true),
("auto_deletion_enabled".to_string(), true),
]),
threshold: 80.0,
},
);
Self { policies }
}
pub fn add_policy(&mut self, policy: CompliancePolicy) {
self.policies.insert(policy.name.clone(), policy);
}
pub fn check_policy(&self, name: &str) -> Result<bool, ComplianceError> {
let policy: _ = self
.policies
.get(name)
.ok_or_else(|| ComplianceError::InvalidPolicy(name.to_string()))?;
let total_rules: _ = policy.rules.len();
let passed_rules: _ = policy.rules.values().filter(|&&v| v).count();
let score: _ = (passed_rules as f64 / total_rules as f64) * 100.0;
Ok(score >= policy.threshold)
}
}
impl Default for GdprComplianceChecker {
fn default() -> Self {
Self::new()
}
}
impl Default for Soc2ComplianceChecker {
fn default() -> Self {
Self::new()
}
}
impl Default for CustomPolicyChecker {
fn default() -> Self {
Self::new()
}
}