crabscore_core/
lib.rs

1//! Core functionality for CrabScore - The Rust Efficiency Standard
2//!
3//! This crate provides the fundamental data structures and algorithms for
4//! calculating and analyzing software efficiency metrics in Rust projects.
5
6#![warn(missing_docs)]
7#![warn(rustdoc::missing_crate_level_docs)]
8#![forbid(unsafe_code)]
9
10use chrono::{DateTime, Utc};
11use serde::{Deserialize, Serialize};
12
13pub mod analysis;
14pub mod error;
15pub mod metrics;
16pub mod profiles;
17pub mod scoring;
18
19pub use profiles::ProfileWeights;
20
21/// Main result type for the CrabScore library
22pub type Result<T> = std::result::Result<T, error::CrabScoreError>;
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn test_industry_profile_weights() {
30        let profile = IndustryProfile::WebServices;
31        let weights = profile.weights();
32        assert!((weights.performance + weights.energy + weights.cost - 1.0).abs() < f64::EPSILON);
33    }
34}
35
36/// Represents a complete CrabScore assessment
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct CrabScore {
39    /// Overall score (0-100)
40    pub overall: f64,
41    /// Performance component score (0-100)
42    pub performance: f64,
43    /// Energy efficiency component score (0-100)
44    pub energy: f64,
45    /// Cost efficiency component score (0-100)
46    pub cost: f64,
47    /// Bonus points from various optimizations
48    pub bonuses: f64,
49    /// Certification level achieved
50    pub certification: Certification,
51    /// When this score was calculated
52    pub timestamp: DateTime<Utc>,
53    /// Additional metadata about the score
54    pub metadata: ScoreMetadata,
55}
56
57/// Metadata about the score calculation
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct ScoreMetadata {
60    /// Name of the project
61    pub project_name: String,
62    /// Project version
63    pub version: String,
64    /// Profile used for scoring
65    pub profile: IndustryProfile,
66    /// Summary of measurements taken
67    pub measurements: MeasurementSummary,
68}
69
70/// Summary of measurements used in scoring
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct MeasurementSummary {
73    /// Duration of the measurement period
74    pub duration: std::time::Duration,
75    /// Number of iterations used in measurements
76    pub iterations: u64,
77    /// Environment where measurements were taken
78    pub environment: Environment,
79}
80
81/// Description of the execution environment
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct Environment {
84    /// Operating system
85    pub os: String,
86    /// CPU model
87    pub cpu: String,
88    /// Total memory in GB
89    pub memory_gb: f32,
90    /// Rust version used
91    pub rust_version: String,
92}
93
94/// Certification levels for CrabScore
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
96pub enum Certification {
97    /// No certification
98    None,
99    /// Basic verification passed
100    Verified,
101    /// Meets all certification requirements
102    Certified,
103    /// Top-tier performance and efficiency
104    Elite,
105    /// Cutting-edge optimizations
106    Pioneer,
107    /// Exceptional energy efficiency
108    Sustainable,
109}
110
111/// Industry profile for certification
112#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
113pub enum IndustryProfile {
114    /// Web services and APIs (40% performance, 30% energy, 30% cost)
115    WebServices,
116    /// IoT and embedded systems (20% performance, 60% energy, 20% cost)
117    IotEmbedded,
118    /// Financial applications (50% performance, 20% energy, 30% cost)
119    Financial,
120    /// Game development (60% performance, 20% energy, 20% cost)
121    Gaming,
122    /// Enterprise software (30% performance, 30% energy, 40% cost)
123    Enterprise,
124}
125
126// Implement default weights for standard profiles
127impl Default for IndustryProfile {
128    fn default() -> Self {
129        Self::WebServices
130    }
131}
132
133impl IndustryProfile {
134    /// Get the weights for this profile
135    pub fn weights(&self) -> ProfileWeights {
136        match self {
137            Self::WebServices => ProfileWeights::new(0.4, 0.3, 0.3),
138            Self::IotEmbedded => ProfileWeights::new(0.2, 0.6, 0.2),
139            Self::Financial => ProfileWeights::new(0.5, 0.2, 0.3),
140            Self::Gaming => ProfileWeights::new(0.6, 0.2, 0.2),
141            Self::Enterprise => ProfileWeights::new(0.3, 0.3, 0.4),
142        }
143    }
144}