lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0
//
// Data Classification
// Automatic tagging and policy-based placement with extensible rules.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

/// Data classification category
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DataClass {
    /// Unknown/unclassified
    Unknown,
    /// Documents (text, PDF, office files)
    Document,
    /// Media (images, audio, video)
    Media,
    /// Source code
    Code,
    /// Database files
    Database,
    /// System/configuration files
    System,
    /// Archives/compressed files
    Archive,
    /// Temporary files
    Temporary,
    /// Critical data (backups, important files)
    Critical,
}

impl DataClass {
    /// Get the human-readable name of the data classification
    pub fn name(&self) -> &'static str {
        match self {
            DataClass::Unknown => "Unknown",
            DataClass::Document => "Document",
            DataClass::Media => "Media",
            DataClass::Code => "Code",
            DataClass::Database => "Database",
            DataClass::System => "System",
            DataClass::Archive => "Archive",
            DataClass::Temporary => "Temporary",
            DataClass::Critical => "Critical",
        }
    }

    /// Get recommended compression level (0-9)
    pub fn compression_level(&self) -> u8 {
        match self {
            DataClass::Unknown => 5,
            DataClass::Document => 6, // Text compresses well
            DataClass::Media => 0,    // Already compressed
            DataClass::Code => 6,     // Text-based
            DataClass::Database => 3, // Balance speed/compression
            DataClass::System => 5,
            DataClass::Archive => 0,   // Already compressed
            DataClass::Temporary => 1, // Fast compression
            DataClass::Critical => 9,  // Maximum compression
        }
    }

    /// Get recommended replication factor
    pub fn replication_factor(&self) -> u8 {
        match self {
            DataClass::Unknown => 1,
            DataClass::Document => 2,
            DataClass::Media => 1,
            DataClass::Code => 2,
            DataClass::Database => 3, // Critical data
            DataClass::System => 2,
            DataClass::Archive => 1,
            DataClass::Temporary => 1, // No replication
            DataClass::Critical => 3,  // High redundancy
        }
    }

    /// Get recommended storage tier
    pub fn recommended_tier(&self) -> &'static str {
        match self {
            DataClass::Unknown => "Standard",
            DataClass::Document => "Standard",
            DataClass::Media => "Archive", // Large, infrequent access
            DataClass::Code => "Hot",      // Frequent access
            DataClass::Database => "Hot",  // Performance critical
            DataClass::System => "Hot",
            DataClass::Archive => "Cold",
            DataClass::Temporary => "Temp",
            DataClass::Critical => "Hot",
        }
    }
}

/// Classification rule
#[derive(Debug, Clone)]
pub struct ClassificationRule {
    /// Rule name
    pub name: String,
    /// Match pattern (extension, path pattern, etc.)
    pub pattern: String,
    /// Target classification
    pub class: DataClass,
    /// Priority (higher = checked first)
    pub priority: u8,
}

impl ClassificationRule {
    /// Create a new classification rule with default priority
    pub fn new(name: String, pattern: String, class: DataClass) -> Self {
        Self {
            name,
            pattern,
            class,
            priority: 50,
        }
    }

    /// Check if path matches pattern
    pub fn matches(&self, path: &str) -> bool {
        // Simple extension matching
        if self.pattern.starts_with("*.") {
            let ext = &self.pattern[2..];
            path.ends_with(ext)
        } else if self.pattern.starts_with('/') {
            // Path prefix matching
            path.starts_with(&self.pattern)
        } else {
            // Substring matching
            path.contains(&self.pattern)
        }
    }
}

/// File metadata tag
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tag {
    /// Tag key
    pub key: String,
    /// Tag value
    pub value: String,
}

impl Tag {
    /// Create a new tag with the given key-value pair
    pub fn new(key: String, value: String) -> Self {
        Self { key, value }
    }
}

/// Classified file
#[derive(Debug, Clone)]
pub struct ClassifiedFile {
    /// Dataset ID
    pub dataset_id: u64,
    /// File offset
    pub offset: u64,
    /// File path
    pub path: String,
    /// File size
    pub size: u64,
    /// Classification
    pub class: DataClass,
    /// Tags
    pub tags: Vec<Tag>,
    /// Classification timestamp
    pub classified_at: u64,
    /// Last access
    pub last_access: u64,
    /// Access count
    pub access_count: u64,
}

impl ClassifiedFile {
    /// Create a new classified file with Unknown classification and no tags
    pub fn new(dataset_id: u64, offset: u64, path: String, size: u64, timestamp: u64) -> Self {
        Self {
            dataset_id,
            offset,
            path,
            size,
            class: DataClass::Unknown,
            tags: Vec::new(),
            classified_at: timestamp,
            last_access: timestamp,
            access_count: 0,
        }
    }

    /// Add tag
    pub fn add_tag(&mut self, key: String, value: String) {
        // Update if exists, otherwise add
        if let Some(tag) = self.tags.iter_mut().find(|t| t.key == key) {
            tag.value = value;
        } else {
            self.tags.push(Tag::new(key, value));
        }
    }

    /// Get tag value
    pub fn get_tag(&self, key: &str) -> Option<&str> {
        self.tags
            .iter()
            .find(|t| t.key == key)
            .map(|t| t.value.as_str())
    }

    /// Has tag
    pub fn has_tag(&self, key: &str) -> bool {
        self.tags.iter().any(|t| t.key == key)
    }
}

/// Classification statistics
#[derive(Debug, Clone, Default)]
pub struct ClassificationStats {
    /// Total files classified
    pub files_classified: u64,
    /// Files by class
    pub by_class: BTreeMap<DataClass, u64>,
    /// Total tags applied
    pub tags_applied: u64,
    /// Rules matched
    pub rules_matched: u64,
}

/// Data classifier
pub struct DataClassifier {
    /// Classification rules (sorted by priority)
    rules: Vec<ClassificationRule>,
    /// Classified files
    files: BTreeMap<(u64, u64), ClassifiedFile>,
    /// Statistics
    stats: ClassificationStats,
}

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

impl DataClassifier {
    /// Create a new data classifier with default classification rules
    pub fn new() -> Self {
        let mut classifier = Self {
            rules: Vec::new(),
            files: BTreeMap::new(),
            stats: ClassificationStats::default(),
        };

        // Add default rules
        classifier.add_default_rules();
        classifier
    }

    /// Add default classification rules
    fn add_default_rules(&mut self) {
        // Documents
        self.add_rule(ClassificationRule::new(
            "text".into(),
            "*.txt".into(),
            DataClass::Document,
        ));
        self.add_rule(ClassificationRule::new(
            "pdf".into(),
            "*.pdf".into(),
            DataClass::Document,
        ));
        self.add_rule(ClassificationRule::new(
            "doc".into(),
            "*.doc".into(),
            DataClass::Document,
        ));
        self.add_rule(ClassificationRule::new(
            "docx".into(),
            "*.docx".into(),
            DataClass::Document,
        ));

        // Media
        self.add_rule(ClassificationRule::new(
            "jpeg".into(),
            "*.jpg".into(),
            DataClass::Media,
        ));
        self.add_rule(ClassificationRule::new(
            "png".into(),
            "*.png".into(),
            DataClass::Media,
        ));
        self.add_rule(ClassificationRule::new(
            "mp4".into(),
            "*.mp4".into(),
            DataClass::Media,
        ));
        self.add_rule(ClassificationRule::new(
            "mp3".into(),
            "*.mp3".into(),
            DataClass::Media,
        ));

        // Code
        self.add_rule(ClassificationRule::new(
            "rust".into(),
            "*.rs".into(),
            DataClass::Code,
        ));
        self.add_rule(ClassificationRule::new(
            "c".into(),
            "*.c".into(),
            DataClass::Code,
        ));
        self.add_rule(ClassificationRule::new(
            "cpp".into(),
            "*.cpp".into(),
            DataClass::Code,
        ));
        self.add_rule(ClassificationRule::new(
            "python".into(),
            "*.py".into(),
            DataClass::Code,
        ));

        // Database
        self.add_rule(ClassificationRule::new(
            "sqlite".into(),
            "*.db".into(),
            DataClass::Database,
        ));
        self.add_rule(ClassificationRule::new(
            "sql".into(),
            "*.sql".into(),
            DataClass::Database,
        ));

        // System
        self.add_rule(ClassificationRule::new(
            "config".into(),
            "*.conf".into(),
            DataClass::System,
        ));
        self.add_rule(ClassificationRule::new(
            "ini".into(),
            "*.ini".into(),
            DataClass::System,
        ));
        self.add_rule(ClassificationRule::new(
            "etc".into(),
            "/etc/".into(),
            DataClass::System,
        ));

        // Archive
        self.add_rule(ClassificationRule::new(
            "zip".into(),
            "*.zip".into(),
            DataClass::Archive,
        ));
        self.add_rule(ClassificationRule::new(
            "tar".into(),
            "*.tar".into(),
            DataClass::Archive,
        ));
        self.add_rule(ClassificationRule::new(
            "gz".into(),
            "*.gz".into(),
            DataClass::Archive,
        ));

        // Temporary
        self.add_rule(ClassificationRule::new(
            "tmp".into(),
            "*.tmp".into(),
            DataClass::Temporary,
        ));
        self.add_rule(ClassificationRule::new(
            "temp_dir".into(),
            "/tmp/".into(),
            DataClass::Temporary,
        ));

        // Critical (backups)
        self.add_rule(ClassificationRule::new(
            "backup".into(),
            "*.bak".into(),
            DataClass::Critical,
        ));
        self.add_rule(ClassificationRule::new(
            "backup_dir".into(),
            "/backup/".into(),
            DataClass::Critical,
        ));
    }

    /// Add classification rule
    pub fn add_rule(&mut self, rule: ClassificationRule) {
        self.rules.push(rule);
        // Sort by priority (descending)
        self.rules.sort_by(|a, b| b.priority.cmp(&a.priority));
    }

    /// Classify file
    pub fn classify_file(
        &mut self,
        dataset_id: u64,
        offset: u64,
        path: String,
        size: u64,
        timestamp: u64,
    ) -> Result<DataClass, &'static str> {
        let mut file = ClassifiedFile::new(dataset_id, offset, path.clone(), size, timestamp);

        // Find matching rule
        for rule in &self.rules {
            if rule.matches(&path) {
                file.class = rule.class;
                self.stats.rules_matched += 1;

                // Add automatic tags
                file.add_tag("rule".into(), rule.name.clone());
                file.add_tag(
                    "compression".into(),
                    alloc::format!("{}", rule.class.compression_level()),
                );
                file.add_tag(
                    "replication".into(),
                    alloc::format!("{}", rule.class.replication_factor()),
                );
                file.add_tag("tier".into(), rule.class.recommended_tier().into());

                self.stats.tags_applied += 4;

                crate::lcpfs_println!(
                    "[ CLASSIFY ] {} -> {} (rule: {})",
                    path,
                    rule.class.name(),
                    rule.name
                );

                break;
            }
        }

        let class = file.class;
        self.files.insert((dataset_id, offset), file);
        self.stats.files_classified += 1;
        *self.stats.by_class.entry(class).or_insert(0) += 1;

        Ok(class)
    }

    /// Add tag to file
    pub fn add_tag(
        &mut self,
        dataset_id: u64,
        offset: u64,
        key: String,
        value: String,
    ) -> Result<(), &'static str> {
        let file = self
            .files
            .get_mut(&(dataset_id, offset))
            .ok_or("File not found")?;

        file.add_tag(key, value);
        self.stats.tags_applied += 1;

        Ok(())
    }

    /// Get file classification
    pub fn get_file(&self, dataset_id: u64, offset: u64) -> Option<&ClassifiedFile> {
        self.files.get(&(dataset_id, offset))
    }

    /// Get files by class
    pub fn get_files_by_class(&self, class: DataClass) -> Vec<&ClassifiedFile> {
        self.files.values().filter(|f| f.class == class).collect()
    }

    /// Get files by tag
    pub fn get_files_by_tag(&self, key: &str, value: &str) -> Vec<&ClassifiedFile> {
        self.files
            .values()
            .filter(|f| f.get_tag(key) == Some(value))
            .collect()
    }

    /// Get statistics
    pub fn get_stats(&self) -> ClassificationStats {
        self.stats.clone()
    }

    /// Get storage breakdown by class
    pub fn storage_by_class(&self) -> BTreeMap<DataClass, u64> {
        let mut breakdown = BTreeMap::new();
        for file in self.files.values() {
            *breakdown.entry(file.class).or_insert(0) += file.size;
        }
        breakdown
    }

    /// Get recommended placement policy
    pub fn get_placement_policy(&self, dataset_id: u64, offset: u64) -> Option<PlacementPolicy> {
        let file = self.files.get(&(dataset_id, offset))?;

        Some(PlacementPolicy {
            compression_level: file.class.compression_level(),
            replication_factor: file.class.replication_factor(),
            tier: file.class.recommended_tier().into(),
            class: file.class,
        })
    }
}

/// Placement policy based on classification
#[derive(Debug, Clone)]
pub struct PlacementPolicy {
    /// Compression level (0-9)
    pub compression_level: u8,
    /// Replication factor
    pub replication_factor: u8,
    /// Storage tier
    pub tier: String,
    /// Data class
    pub class: DataClass,
}

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

    #[test]
    fn test_data_class_properties() {
        assert_eq!(DataClass::Media.compression_level(), 0); // Already compressed
        assert_eq!(DataClass::Code.compression_level(), 6); // Text compresses well
        assert_eq!(DataClass::Critical.replication_factor(), 3); // High redundancy
        assert_eq!(DataClass::Temporary.replication_factor(), 1); // No replication
    }

    #[test]
    fn test_classification_rule_extension() {
        let rule = ClassificationRule::new("rust".into(), "*.rs".into(), DataClass::Code);

        assert!(rule.matches("main.rs"));
        assert!(rule.matches("/src/lib.rs"));
        assert!(!rule.matches("main.c"));
    }

    #[test]
    fn test_classification_rule_path() {
        let rule = ClassificationRule::new("etc".into(), "/etc/".into(), DataClass::System);

        assert!(rule.matches("/etc/config.conf"));
        assert!(rule.matches("/etc/ssh/sshd_config"));
        assert!(!rule.matches("/home/user/file.txt"));
    }

    #[test]
    fn test_tag_operations() {
        let mut file = ClassifiedFile::new(1, 0x1000, "test.txt".into(), 1024, 1000);

        file.add_tag("owner".into(), "alice".into());
        file.add_tag("project".into(), "demo".into());

        assert_eq!(file.tags.len(), 2);
        assert_eq!(file.get_tag("owner"), Some("alice"));
        assert_eq!(file.get_tag("project"), Some("demo"));
        assert_eq!(file.get_tag("missing"), None);

        // Update existing tag
        file.add_tag("owner".into(), "bob".into());
        assert_eq!(file.tags.len(), 2);
        assert_eq!(file.get_tag("owner"), Some("bob"));
    }

    #[test]
    fn test_classifier_creation() {
        let classifier = DataClassifier::new();

        // Should have default rules
        assert!(!classifier.rules.is_empty());
    }

    #[test]
    fn test_classify_rust_file() {
        let mut classifier = DataClassifier::new();

        let class = classifier
            .classify_file(1, 0x1000, "main.rs".into(), 1024, 1000)
            .expect("test: operation should succeed");
        assert_eq!(class, DataClass::Code);

        let file = classifier
            .get_file(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(file.class, DataClass::Code);
        assert_eq!(file.get_tag("rule"), Some("rust"));
    }

    #[test]
    fn test_classify_document() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "report.pdf".into(), 50000, 1000)
            .expect("test: operation should succeed");

        let file = classifier
            .get_file(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(file.class, DataClass::Document);
        assert_eq!(file.get_tag("compression"), Some("6"));
    }

    #[test]
    fn test_classify_media() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "video.mp4".into(), 1_000_000, 1000)
            .expect("test: operation should succeed");

        let file = classifier
            .get_file(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(file.class, DataClass::Media);
        assert_eq!(file.get_tag("compression"), Some("0")); // No compression
    }

    #[test]
    fn test_classify_system() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "/etc/fstab".into(), 512, 1000)
            .expect("test: operation should succeed");

        let file = classifier
            .get_file(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(file.class, DataClass::System);
    }

    #[test]
    fn test_classify_temporary() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "/tmp/cache.tmp".into(), 2048, 1000)
            .expect("test: operation should succeed");

        let file = classifier
            .get_file(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(file.class, DataClass::Temporary);
        assert_eq!(file.get_tag("replication"), Some("1"));
    }

    #[test]
    fn test_get_files_by_class() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "main.rs".into(), 1024, 1000)
            .expect("test: operation should succeed");
        classifier
            .classify_file(2, 0x2000, "lib.rs".into(), 2048, 1000)
            .expect("test: operation should succeed");
        classifier
            .classify_file(3, 0x3000, "video.mp4".into(), 50000, 1000)
            .expect("test: operation should succeed");

        let code_files = classifier.get_files_by_class(DataClass::Code);
        assert_eq!(code_files.len(), 2);

        let media_files = classifier.get_files_by_class(DataClass::Media);
        assert_eq!(media_files.len(), 1);
    }

    #[test]
    fn test_get_files_by_tag() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "main.rs".into(), 1024, 1000)
            .expect("test: operation should succeed");
        classifier
            .add_tag(1, 0x1000, "project".into(), "myapp".into())
            .expect("test: operation should succeed");

        classifier
            .classify_file(2, 0x2000, "lib.rs".into(), 2048, 1000)
            .expect("test: operation should succeed");
        classifier
            .add_tag(2, 0x2000, "project".into(), "myapp".into())
            .expect("test: operation should succeed");

        classifier
            .classify_file(3, 0x3000, "test.rs".into(), 512, 1000)
            .expect("test: operation should succeed");
        classifier
            .add_tag(3, 0x3000, "project".into(), "other".into())
            .expect("test: operation should succeed");

        let myapp_files = classifier.get_files_by_tag("project", "myapp");
        assert_eq!(myapp_files.len(), 2);
    }

    #[test]
    fn test_storage_breakdown() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "main.rs".into(), 1024, 1000)
            .expect("test: operation should succeed");
        classifier
            .classify_file(2, 0x2000, "lib.rs".into(), 2048, 1000)
            .expect("test: operation should succeed");
        classifier
            .classify_file(3, 0x3000, "video.mp4".into(), 50000, 1000)
            .expect("test: operation should succeed");

        let breakdown = classifier.storage_by_class();
        assert_eq!(breakdown.get(&DataClass::Code), Some(&3072)); // 1024 + 2048
        assert_eq!(breakdown.get(&DataClass::Media), Some(&50000));
    }

    #[test]
    fn test_placement_policy() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "backup.bak".into(), 100000, 1000)
            .expect("test: operation should succeed");

        let policy = classifier
            .get_placement_policy(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(policy.class, DataClass::Critical);
        assert_eq!(policy.compression_level, 9);
        assert_eq!(policy.replication_factor, 3);
        assert_eq!(policy.tier, "Hot");
    }

    #[test]
    fn test_statistics() {
        let mut classifier = DataClassifier::new();

        classifier
            .classify_file(1, 0x1000, "main.rs".into(), 1024, 1000)
            .expect("test: operation should succeed");
        classifier
            .classify_file(2, 0x2000, "video.mp4".into(), 50000, 1000)
            .expect("test: operation should succeed");

        let stats = classifier.get_stats();
        assert_eq!(stats.files_classified, 2);
        assert_eq!(stats.rules_matched, 2);
        assert!(stats.tags_applied > 0);

        assert_eq!(stats.by_class.get(&DataClass::Code), Some(&1));
        assert_eq!(stats.by_class.get(&DataClass::Media), Some(&1));
    }

    #[test]
    fn test_custom_rule() {
        let mut classifier = DataClassifier::new();

        let mut rule = ClassificationRule::new(
            "critical_data".into(),
            "/important/".into(),
            DataClass::Critical,
        );
        rule.priority = 100; // High priority

        classifier.add_rule(rule);

        classifier
            .classify_file(1, 0x1000, "/important/data.bin".into(), 1024, 1000)
            .expect("test: operation should succeed");

        let file = classifier
            .get_file(1, 0x1000)
            .expect("test: operation should succeed");
        assert_eq!(file.class, DataClass::Critical);
    }
}