reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! # Cross-Platform Design Validation
//!
//! Validates designs against platform-specific guidelines for Web, iOS, and Android.

use super::types::*;
use std::collections::HashMap;

/// Cross-platform design validator
pub struct CrossPlatformValidator;

impl CrossPlatformValidator {
    /// Validate design across multiple platforms
    pub fn validate(input: &DesignInput, platforms: &[Platform]) -> CrossPlatformResult {
        let mut platform_results = HashMap::new();
        let mut all_issues = Vec::new();

        for platform in platforms {
            let result = Self::validate_for_platform(input, *platform);
            all_issues.extend(result.issues.iter().map(|i| CrossPlatformIssue {
                severity: IssueSeverity::Minor,
                platforms_affected: vec![*platform],
                description: i.clone(),
                suggestion: format!(
                    "Review {} design guidelines",
                    Self::platform_name(*platform)
                ),
            }));
            platform_results.insert(*platform, result);
        }

        // Calculate consistency across platforms
        let consistency_score = Self::calculate_consistency(&platform_results);

        // Overall score
        let avg_compliance: f64 = platform_results
            .values()
            .map(|r| r.compliance_score)
            .sum::<f64>()
            / platform_results.len() as f64;

        let score = (avg_compliance + consistency_score) / 2.0;

        CrossPlatformResult {
            score,
            platform_results,
            consistency_score,
            issues: all_issues,
        }
    }

    /// Validate for a specific platform
    fn validate_for_platform(input: &DesignInput, platform: Platform) -> PlatformSpecificResult {
        let validator: Box<dyn PlatformGuidelines> = match platform {
            Platform::Web => Box::new(WebGuidelines),
            Platform::IOS => Box::new(IOSGuidelines),
            Platform::Android => Box::new(AndroidGuidelines),
            Platform::Desktop => Box::new(DesktopGuidelines),
        };

        validator.validate(input)
    }

    /// Calculate consistency score across platforms
    fn calculate_consistency(results: &HashMap<Platform, PlatformSpecificResult>) -> f64 {
        if results.len() < 2 {
            return 1.0;
        }

        let scores: Vec<f64> = results.values().map(|r| r.compliance_score).collect();

        let avg = scores.iter().sum::<f64>() / scores.len() as f64;
        let variance: f64 =
            scores.iter().map(|&s| (s - avg).powi(2)).sum::<f64>() / scores.len() as f64;

        // Lower variance = higher consistency
        (1.0 - variance.sqrt()).max(0.0)
    }

    fn platform_name(platform: Platform) -> &'static str {
        match platform {
            Platform::Web => "Web",
            Platform::IOS => "iOS HIG",
            Platform::Android => "Material Design",
            Platform::Desktop => "Desktop",
        }
    }
}

/// Platform guidelines trait
trait PlatformGuidelines {
    fn validate(&self, input: &DesignInput) -> PlatformSpecificResult;
}

/// Web platform guidelines
struct WebGuidelines;

impl PlatformGuidelines for WebGuidelines {
    fn validate(&self, input: &DesignInput) -> PlatformSpecificResult {
        let mut issues = Vec::new();
        let mut compliance_score = 0.85;
        let mut guideline_adherence = 0.88;
        let conventions_score = 0.90;

        // Check for responsive design patterns
        if let Some(tokens) = &input.design_tokens {
            if tokens.spacing.scale.is_empty() {
                issues.push("No spacing scale defined - may affect responsiveness".to_string());
                compliance_score -= 0.05;
            }
        }

        // Component-specific checks
        match input.component_type {
            ComponentType::Navigation => {
                guideline_adherence = 0.92; // Navigation is well-defined for web
            }
            ComponentType::Form => {
                guideline_adherence = 0.85;
                issues.push("Ensure form labels are visible (not placeholders only)".to_string());
            }
            _ => {}
        }

        PlatformSpecificResult {
            platform: Platform::Web,
            compliance_score,
            design_guideline_adherence: guideline_adherence,
            platform_conventions_score: conventions_score,
            issues,
        }
    }
}

/// iOS Human Interface Guidelines validator
struct IOSGuidelines;

impl PlatformGuidelines for IOSGuidelines {
    fn validate(&self, input: &DesignInput) -> PlatformSpecificResult {
        let mut issues = Vec::new();
        let mut compliance_score = 0.85;

        // iOS-specific checks
        if let Some(tokens) = &input.design_tokens {
            // Check for iOS system colors
            if let Some(primary) = &tokens.colors.primary {
                // iOS prefers specific blue: #007AFF
                if primary != "#007AFF" && primary != "#0A84FF" {
                    issues.push(
                        "Consider using iOS system blue (#007AFF) for primary actions".to_string(),
                    );
                }
            }

            // Check font
            if let Some(font) = &tokens.typography.font_family_primary {
                if !font.contains("SF Pro") && !font.contains("system") {
                    issues.push("iOS recommends SF Pro or system font".to_string());
                    compliance_score -= 0.05;
                }
            }
        }

        // Touch target sizes
        match input.component_type {
            ComponentType::Button => {
                issues.push("Ensure minimum 44x44pt touch target".to_string());
            }
            ComponentType::Navigation => {
                issues.push("Tab bar should have 49pt height on iPhone".to_string());
            }
            _ => {}
        }

        // iOS guideline adherence scoring
        let guideline_adherence = match input.component_type {
            ComponentType::Navigation => 0.90,
            ComponentType::Button => 0.88,
            ComponentType::Modal => 0.92, // iOS modals are well-defined
            _ => 0.85,
        };

        PlatformSpecificResult {
            platform: Platform::IOS,
            compliance_score,
            design_guideline_adherence: guideline_adherence,
            platform_conventions_score: 0.87,
            issues,
        }
    }
}

/// Material Design (Android) guidelines validator
struct AndroidGuidelines;

impl PlatformGuidelines for AndroidGuidelines {
    fn validate(&self, input: &DesignInput) -> PlatformSpecificResult {
        let mut issues = Vec::new();
        let mut compliance_score = 0.85;

        // Material Design checks
        if let Some(tokens) = &input.design_tokens {
            // Check for Material color scheme
            let has_elevation = !tokens.shadows.shadows.is_empty();
            if !has_elevation {
                issues.push("Material Design uses elevation/shadows for depth".to_string());
                compliance_score -= 0.05;
            }

            // Check spacing follows 8dp grid
            if let Some(base) = tokens.spacing.base_unit {
                if (base % 8.0).abs() > 0.01 {
                    issues.push("Material Design uses 8dp grid system".to_string());
                }
            }

            // Check for Roboto font
            if let Some(font) = &tokens.typography.font_family_primary {
                if !font.contains("Roboto") {
                    issues.push("Material Design recommends Roboto font".to_string());
                }
            }
        }

        // Touch targets (48dp minimum)
        match input.component_type {
            ComponentType::Button => {
                issues.push("Ensure minimum 48dp touch target".to_string());
            }
            ComponentType::Form => {
                issues.push("Text fields should use outlined or filled style".to_string());
            }
            _ => {}
        }

        let guideline_adherence = match input.component_type {
            ComponentType::Button => 0.90, // FAB is well-defined
            ComponentType::Card => 0.92,   // Cards are core to Material
            ComponentType::Navigation => 0.88,
            _ => 0.85,
        };

        PlatformSpecificResult {
            platform: Platform::Android,
            compliance_score,
            design_guideline_adherence: guideline_adherence,
            platform_conventions_score: 0.86,
            issues,
        }
    }
}

/// Desktop platform guidelines
struct DesktopGuidelines;

impl PlatformGuidelines for DesktopGuidelines {
    fn validate(&self, input: &DesignInput) -> PlatformSpecificResult {
        let mut issues = Vec::new();

        // Desktop-specific considerations
        match input.component_type {
            ComponentType::Navigation => {
                issues.push("Consider keyboard shortcuts for navigation".to_string());
            }
            ComponentType::Form => {
                issues.push("Tab order should be logical for keyboard users".to_string());
            }
            _ => {}
        }

        // Hover states are important on desktop
        issues.push("Ensure all interactive elements have hover states".to_string());

        PlatformSpecificResult {
            platform: Platform::Desktop,
            compliance_score: 0.88,
            design_guideline_adherence: 0.85,
            platform_conventions_score: 0.87,
            issues,
        }
    }
}

/// Responsive design validator
pub struct ResponsiveValidator;

impl ResponsiveValidator {
    /// Validate responsive design patterns
    pub fn validate(input: &DesignInput) -> ResponsiveValidationResult {
        let breakpoints = Self::check_breakpoints(input);
        let fluid_typography = Self::check_fluid_typography(input);
        let flexible_layouts = Self::check_flexible_layouts(input);
        let touch_targets = Self::check_touch_targets(input);

        let score = (breakpoints + fluid_typography + flexible_layouts + touch_targets) / 4.0;

        ResponsiveValidationResult {
            score,
            breakpoints_score: breakpoints,
            fluid_typography_score: fluid_typography,
            flexible_layouts_score: flexible_layouts,
            touch_targets_score: touch_targets,
            recommendations: Self::generate_recommendations(
                breakpoints,
                fluid_typography,
                flexible_layouts,
                touch_targets,
            ),
        }
    }

    fn check_breakpoints(_input: &DesignInput) -> f64 {
        // Would check CSS for breakpoints
        0.85
    }

    fn check_fluid_typography(input: &DesignInput) -> f64 {
        if let Some(tokens) = &input.design_tokens {
            // Check for relative units in font sizes
            let has_relative = tokens
                .typography
                .font_sizes
                .values()
                .any(|s| s.contains("rem") || s.contains("em") || s.contains("vw"));

            if has_relative {
                0.90
            } else {
                0.70
            }
        } else {
            0.75
        }
    }

    fn check_flexible_layouts(input: &DesignInput) -> f64 {
        if let Some(tokens) = &input.design_tokens {
            if tokens.spacing.base_unit.is_some() || !tokens.spacing.scale.is_empty() {
                0.88
            } else {
                0.70
            }
        } else {
            0.75
        }
    }

    fn check_touch_targets(_input: &DesignInput) -> f64 {
        0.85 // Would check min-height/width of interactive elements
    }

    fn generate_recommendations(
        breakpoints: f64,
        typography: f64,
        layouts: f64,
        touch: f64,
    ) -> Vec<String> {
        let mut recs = Vec::new();

        if breakpoints < 0.80 {
            recs.push("Define breakpoints for sm, md, lg, xl viewports".to_string());
        }
        if typography < 0.80 {
            recs.push("Use relative units (rem) for font sizes".to_string());
        }
        if layouts < 0.80 {
            recs.push("Implement a consistent spacing scale".to_string());
        }
        if touch < 0.80 {
            recs.push("Ensure 44px minimum touch targets on mobile".to_string());
        }

        recs
    }
}

/// Responsive validation result
#[derive(Debug, Clone)]
pub struct ResponsiveValidationResult {
    pub score: f64,
    pub breakpoints_score: f64,
    pub fluid_typography_score: f64,
    pub flexible_layouts_score: f64,
    pub touch_targets_score: f64,
    pub recommendations: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cross_platform_validation() {
        let input = DesignInput {
            data: DesignData::Html("<button>Test</button>".to_string()),
            platform: Platform::Web,
            context: None,
            component_type: ComponentType::Button,
            design_tokens: Some(DesignTokens {
                colors: ColorTokens::reasonkit_brand(),
                typography: TypographyTokens::reasonkit_brand(),
                ..Default::default()
            }),
        };

        let result = CrossPlatformValidator::validate(
            &input,
            &[Platform::Web, Platform::IOS, Platform::Android],
        );

        assert!(result.score > 0.0);
        assert_eq!(result.platform_results.len(), 3);
    }

    #[test]
    fn test_responsive_validation() {
        let input = DesignInput {
            data: DesignData::Css("body { font-size: 1rem; }".to_string()),
            platform: Platform::Web,
            context: None,
            component_type: ComponentType::Page,
            design_tokens: Some(DesignTokens::default()),
        };

        let result = ResponsiveValidator::validate(&input);
        assert!(result.score > 0.0);
    }
}