ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// SPRINT4-001: Educational platform implementation
// PMAT Complexity: <10 per function
use crate::notebook::testing::types::Notebook;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Assignment {
    pub id: String,
    pub title: String,
    pub description: String,
    pub notebook_template: Notebook,
    pub due_date: Option<chrono::DateTime<chrono::Utc>>,
    pub points: u32,
    pub rubric: Vec<RubricItem>,
    pub test_cases: Vec<TestCase>,
}
#[derive(Debug, Clone)]
pub struct RubricItem {
    pub id: String,
    pub description: String,
    pub points: u32,
    pub criteria: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct TestCase {
    pub id: String,
    pub cell_id: String,
    pub input: String,
    pub expected_output: String,
    pub points: u32,
    pub hidden: bool,
}
#[derive(Debug, Clone)]
pub struct StudentSubmission {
    pub student_id: String,
    pub assignment_id: String,
    pub notebook: Notebook,
    pub submitted_at: chrono::DateTime<chrono::Utc>,
    pub grade: Option<Grade>,
}
#[derive(Debug, Clone)]
pub struct Grade {
    pub total_points: u32,
    pub max_points: u32,
    pub percentage: f64,
    pub feedback: Vec<Feedback>,
    pub rubric_scores: HashMap<String, u32>,
}
#[derive(Debug, Clone)]
pub struct Feedback {
    pub cell_id: String,
    pub message: String,
    pub severity: FeedbackSeverity,
}
#[derive(Debug, Clone)]
pub enum FeedbackSeverity {
    Success,
    Warning,
    Error,
    Info,
}

/// Educational platform for notebook-based learning
pub struct EducationalPlatform {
    assignments: Vec<Assignment>,
    submissions: std::collections::HashMap<String, Vec<StudentSubmission>>,
    peer_reviews: Vec<PeerReview>,
    analytics: LearningAnalytics,
}

impl Default for EducationalPlatform {
    fn default() -> Self {
        Self::new()
    }
}

impl EducationalPlatform {
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::testing::educational::EducationalPlatform;
    ///
    /// let instance = EducationalPlatform::new();
    /// // Verify behavior
    /// ```
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::testing::educational::EducationalPlatform;
    ///
    /// let instance = EducationalPlatform::new();
    /// // Verify behavior
    /// ```
    pub fn new() -> Self {
        Self {
            assignments: Vec::new(),
            submissions: HashMap::new(),
            peer_reviews: Vec::new(),
            analytics: LearningAnalytics::new(),
        }
    }
    /// Create a new assignment
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::testing::educational::EducationalPlatform;
    ///
    /// let mut instance = EducationalPlatform::new();
    /// let result = instance.create_assignment();
    /// // Verify behavior
    /// ```
    pub fn create_assignment(&mut self, assignment: Assignment) -> Result<(), String> {
        if self.assignments.iter().any(|a| a.id == assignment.id) {
            return Err("Assignment ID already exists".to_string());
        }
        self.assignments.push(assignment);
        Ok(())
    }
    /// Get all assignments
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::get_assignments;
    ///
    /// let result = get_assignments(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn get_assignments(&self) -> &[Assignment] {
        &self.assignments
    }
    /// Submit an assignment
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::submit_assignment;
    ///
    /// let result = submit_assignment("example");
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn submit_assignment(
        &mut self,
        student_id: &str,
        mut submission: StudentSubmission,
    ) -> Result<(), String> {
        // Validate assignment exists
        if !self
            .assignments
            .iter()
            .any(|a| a.id == submission.assignment_id)
        {
            return Err("Assignment not found".to_string());
        }
        // Auto-grade if test cases exist
        if let Some(assignment) = self
            .assignments
            .iter()
            .find(|a| a.id == submission.assignment_id)
        {
            if !assignment.test_cases.is_empty() {
                submission.grade = Some(self.auto_grade(&submission));
            }
        }
        // Track submission
        self.submissions
            .entry(student_id.to_string())
            .or_default()
            .push(submission);
        // Track analytics
        self.analytics.track_event(LearningEvent {
            student_id: student_id.to_string(),
            event_type: EventType::AssignmentSubmit,
            cell_id: String::new(),
            timestamp: chrono::Utc::now(),
            success: true,
            duration_ms: 0,
        });
        Ok(())
    }
    /// Auto-grade a submission
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::testing::educational::EducationalPlatform;
    ///
    /// let mut instance = EducationalPlatform::new();
    /// let result = instance.auto_grade();
    /// // Verify behavior
    /// ```
    pub fn auto_grade(&self, submission: &StudentSubmission) -> Grade {
        let assignment = self
            .assignments
            .iter()
            .find(|a| a.id == submission.assignment_id)
            .expect("Assignment ID must exist in assignments list for auto-grading");
        let mut total_points = 0;
        let mut feedback = Vec::new();
        // Run test cases
        for test_case in &assignment.test_cases {
            if let Some(cell) = submission
                .notebook
                .cells
                .iter()
                .find(|c| c.id == test_case.cell_id)
            {
                // Simplified test execution
                if cell.source.contains(&test_case.expected_output) {
                    total_points += test_case.points;
                    feedback.push(Feedback {
                        cell_id: test_case.cell_id.clone(),
                        message: format!("Test {} passed", test_case.id),
                        severity: FeedbackSeverity::Success,
                    });
                } else {
                    feedback.push(Feedback {
                        cell_id: test_case.cell_id.clone(),
                        message: format!("Test {} failed", test_case.id),
                        severity: FeedbackSeverity::Error,
                    });
                }
            }
        }
        Grade {
            total_points,
            max_points: assignment.points,
            percentage: (f64::from(total_points) / f64::from(assignment.points)) * 100.0,
            feedback,
            rubric_scores: HashMap::new(),
        }
    }
    /// Submit a peer review
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::submit_peer_review;
    ///
    /// let result = submit_peer_review(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn submit_peer_review(&mut self, review: PeerReview) -> Result<(), String> {
        self.peer_reviews.push(review);
        Ok(())
    }
    /// Get analytics for the platform
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::get_analytics;
    ///
    /// let result = get_analytics(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn get_analytics(&self) -> &LearningAnalytics {
        &self.analytics
    }
}
/// Learning analytics system
#[derive(Debug, Clone)]
pub struct LearningAnalytics {
    events: Vec<LearningEvent>,
}
#[derive(Debug, Clone)]
pub struct LearningEvent {
    pub student_id: String,
    pub event_type: EventType,
    pub cell_id: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub success: bool,
    pub duration_ms: u64,
}
#[derive(Debug, Clone)]
pub enum EventType {
    CellExecution,
    TestRun,
    AssignmentSubmit,
    AssignmentComplete,
    TutorialStep,
    HintRequested,
}
#[derive(Debug, Clone)]
pub struct StudentMetrics {
    pub total_executions: usize,
    pub success_rate: f64,
    pub avg_execution_time_ms: u64,
    pub assignments_completed: usize,
}
#[derive(Debug, Clone)]
pub struct ClassMetrics {
    pub total_students: usize,
    pub completion_rate: f64,
    pub avg_score: f64,
}
impl Default for LearningAnalytics {
    fn default() -> Self {
        Self::new()
    }
}

impl LearningAnalytics {
    pub fn new() -> Self {
        Self { events: Vec::new() }
    }
    /// Track a learning event
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::track_event;
    ///
    /// let result = track_event(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn track_event(&mut self, event: LearningEvent) {
        self.events.push(event);
    }
    /// Get metrics for a specific student
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::get_student_metrics;
    ///
    /// let result = get_student_metrics("example");
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn get_student_metrics(&self, student_id: &str) -> StudentMetrics {
        let student_events: Vec<_> = self
            .events
            .iter()
            .filter(|e| e.student_id == student_id)
            .collect();
        let total = student_events.len();
        let successful = student_events.iter().filter(|e| e.success).count();
        let completions = student_events
            .iter()
            .filter(|e| matches!(e.event_type, EventType::AssignmentComplete))
            .count();
        let avg_time = if total > 0 {
            let total_time: u64 = student_events.iter().map(|e| e.duration_ms).sum();
            total_time / total as u64
        } else {
            0
        };
        StudentMetrics {
            total_executions: total,
            success_rate: if total > 0 {
                successful as f64 / total as f64
            } else {
                0.0
            },
            avg_execution_time_ms: avg_time,
            assignments_completed: completions,
        }
    }
    /// Get metrics for the entire class
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::notebook::testing::educational::get_class_metrics;
    ///
    /// let result = get_class_metrics(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn get_class_metrics(&self) -> ClassMetrics {
        let unique_students: std::collections::HashSet<_> =
            self.events.iter().map(|e| &e.student_id).collect();
        let completions = self
            .events
            .iter()
            .filter(|e| matches!(e.event_type, EventType::AssignmentComplete))
            .count();
        ClassMetrics {
            total_students: unique_students.len(),
            completion_rate: if unique_students.is_empty() {
                0.0
            } else {
                completions as f64 / unique_students.len() as f64
            },
            avg_score: 0.0, // Would calculate from grades
        }
    }
}
/// Peer review system
#[derive(Debug, Clone)]
pub struct PeerReview {
    pub id: String,
    pub assignment_id: String,
    pub reviewer_id: String,
    pub reviewee_id: String,
    pub feedback: Vec<ReviewComment>,
    pub rating: u8,
}
#[derive(Debug, Clone)]
pub struct ReviewComment {
    pub cell_id: String,
    pub line_number: Option<usize>,
    pub comment: String,
    pub category: CommentCategory,
}
#[derive(Debug, Clone)]
pub enum CommentCategory {
    Style,
    Correctness,
    Efficiency,
    Positive,
    Suggestion,
}
impl Default for Assignment {
    fn default() -> Self {
        Self {
            id: String::new(),
            title: String::new(),
            description: String::new(),
            notebook_template: Notebook {
                cells: vec![],
                metadata: None,
            },
            due_date: None,
            points: 100,
            rubric: vec![],
            test_cases: vec![],
        }
    }
}

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

    // EXTREME TDD: Comprehensive test coverage for educational platform

    #[test]
    fn test_assignment_creation() {
        let assignment = Assignment {
            id: "assign1".to_string(),
            title: "Test Assignment".to_string(),
            description: "Learn basics".to_string(),
            notebook_template: Notebook {
                cells: vec![],
                metadata: None,
            },
            due_date: Some(Utc::now()),
            points: 100,
            rubric: vec![],
            test_cases: vec![],
        };

        assert_eq!(assignment.id, "assign1");
        assert_eq!(assignment.title, "Test Assignment");
        assert_eq!(assignment.points, 100);
        assert!(assignment.due_date.is_some());
    }

    #[test]
    fn test_rubric_item() {
        let rubric = RubricItem {
            id: "rubric1".to_string(),
            description: "Code quality".to_string(),
            points: 25,
            criteria: vec!["Clean code".to_string(), "Proper naming".to_string()],
        };

        assert_eq!(rubric.id, "rubric1");
        assert_eq!(rubric.points, 25);
        assert_eq!(rubric.criteria.len(), 2);
    }

    #[test]
    fn test_test_case_visible() {
        let test = TestCase {
            id: "test1".to_string(),
            cell_id: "cell1".to_string(),
            input: "2 + 2".to_string(),
            expected_output: "4".to_string(),
            points: 10,
            hidden: false,
        };

        assert_eq!(test.id, "test1");
        assert!(!test.hidden);
        assert_eq!(test.points, 10);
    }

    #[test]
    fn test_test_case_hidden() {
        let test = TestCase {
            id: "test2".to_string(),
            cell_id: "cell2".to_string(),
            input: "secret".to_string(),
            expected_output: "result".to_string(),
            points: 20,
            hidden: true,
        };

        assert!(test.hidden);
        assert_eq!(test.points, 20);
    }

    #[test]
    fn test_student_submission() {
        let submission = StudentSubmission {
            student_id: "student1".to_string(),
            assignment_id: "assign1".to_string(),
            notebook: Notebook {
                cells: vec![],
                metadata: None,
            },
            submitted_at: Utc::now(),
            grade: None,
        };

        assert_eq!(submission.student_id, "student1");
        assert_eq!(submission.assignment_id, "assign1");
        assert!(submission.grade.is_none());
    }

    #[test]
    fn test_grade_creation() {
        let mut rubric_scores = HashMap::new();
        rubric_scores.insert("rubric1".to_string(), 20);

        let grade = Grade {
            total_points: 85,
            max_points: 100,
            percentage: 85.0,
            feedback: vec![],
            rubric_scores,
        };

        assert_eq!(grade.total_points, 85);
        assert_eq!(grade.percentage, 85.0);
        assert_eq!(grade.rubric_scores.len(), 1);
    }

    #[test]
    fn test_feedback_types() {
        let severities = vec![
            FeedbackSeverity::Success,
            FeedbackSeverity::Warning,
            FeedbackSeverity::Error,
            FeedbackSeverity::Info,
        ];

        for severity in severities {
            let feedback = Feedback {
                cell_id: "cell1".to_string(),
                message: "Test feedback".to_string(),
                severity: severity.clone(),
            };

            match feedback.severity {
                FeedbackSeverity::Success => {}
                FeedbackSeverity::Warning => {}
                FeedbackSeverity::Error => {}
                FeedbackSeverity::Info => {}
            }
        }
    }

    #[test]
    fn test_educational_platform_new() {
        let platform = EducationalPlatform::new();
        assert!(platform.assignments.is_empty());
        assert!(platform.submissions.is_empty());
        assert!(platform.peer_reviews.is_empty());
    }

    #[test]
    fn test_educational_platform_default() {
        let platform = EducationalPlatform::default();
        assert!(platform.assignments.is_empty());
    }

    #[test]
    fn test_learning_analytics_new() {
        let analytics = LearningAnalytics::new();
        assert!(analytics.events.is_empty());
    }

    #[test]
    fn test_peer_review() {
        let review = PeerReview {
            id: "review1".to_string(),
            assignment_id: "assign1".to_string(),
            reviewer_id: "student2".to_string(),
            reviewee_id: "student1".to_string(),
            feedback: vec![],
            rating: 4,
        };

        assert_eq!(review.id, "review1");
        assert_eq!(review.assignment_id, "assign1");
        assert_eq!(review.rating, 4);
    }

    #[test]
    fn test_assignment_default() {
        let assignment = Assignment::default();
        assert_eq!(assignment.id, "");
        assert_eq!(assignment.points, 100);
        assert!(assignment.due_date.is_none());
    }

    #[test]
    fn test_clone_implementations() {
        let assignment = Assignment::default();
        let cloned = assignment.clone();
        assert_eq!(cloned.id, assignment.id);

        let rubric = RubricItem {
            id: "r1".to_string(),
            description: "desc".to_string(),
            points: 10,
            criteria: vec![],
        };
        let cloned_rubric = rubric.clone();
        assert_eq!(cloned_rubric.id, rubric.id);
    }
}