scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
//! Cross-platform validation utilities for consistent behavior across operating systems and architectures.
//!
//! This module provides validation utilities that handle platform-specific differences
//! in numeric formats, file systems, memory models, and hardware capabilities to ensure
//! consistent behavior across Windows, macOS, Linux, and different CPU architectures.

use crate::error::{CoreError, CoreResult, ErrorContext};
use crate::validation::production::{
    ValidationContext, ValidationError, ValidationResult, ValidationSeverity,
};
use std::collections::HashMap;

/// Platform information detected at runtime
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlatformInfo {
    /// Operating system family
    pub os_family: OsFamily,
    /// CPU architecture
    pub arch: CpuArchitecture,
    /// Available SIMD instruction sets
    pub simd_support: SimdSupport,
    /// Endianness of the target platform
    pub endianness: Endianness,
    /// Native path separator
    pub path_separator: char,
    /// Maximum file path length
    pub max_path_length: usize,
    /// Default memory page size
    pub page_size: usize,
    /// Whether the platform supports memory-mapped files
    pub memory_mapping_support: bool,
    /// Default floating-point precision behavior
    pub fp_behavior: FloatingPointBehavior,
}

/// Operating system families
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OsFamily {
    Windows,
    Unix, // Linux, macOS, BSD, etc.
    Wasm, // WebAssembly runtime
    Unknown,
}

/// CPU architecture types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CpuArchitecture {
    X86_64,
    AArch64, // ARM64
    X86,     // 32-bit x86
    ARM,     // 32-bit ARM
    RISCV64,
    PowerPC64,
    Wasm32, // WebAssembly 32-bit
    Wasm64, // WebAssembly 64-bit
    Other(u32),
}

/// SIMD instruction set support
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimdSupport {
    /// SSE support levels (x86/x64)
    pub sse: Option<SseLevel>,
    /// AVX support levels (x86/x64)
    pub avx: Option<AvxLevel>,
    /// NEON support (ARM)
    pub neon: bool,
    /// SVE support (ARM)
    pub sve: bool,
    /// Vector extension support level
    pub vector_width: usize,
}

/// SSE instruction set levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SseLevel {
    Sse,
    Sse2,
    Sse3,
    Ssse3,
    Sse41,
    Sse42,
}

/// AVX instruction set levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AvxLevel {
    Avx,
    Avx2,
    Avx512f,
    Avx512bw,
    Avx512dq,
}

/// Platform endianness
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Endianness {
    Little,
    Big,
}

/// Floating-point behavior characteristics
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FloatingPointBehavior {
    /// Whether denormal numbers are supported
    pub denormals_supported: bool,
    /// Default rounding mode
    pub rounding_mode: RoundingMode,
    /// Whether NaN propagation is IEEE 754 compliant
    pub nan_propagation: bool,
    /// Whether infinity is supported
    pub infinity_supported: bool,
}

/// Floating-point rounding modes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RoundingMode {
    ToNearest,
    TowardZero,
    TowardPositiveInfinity,
    TowardNegativeInfinity,
}

/// Cross-platform validator with platform-aware validation rules
pub struct CrossPlatformValidator {
    /// Current platform information
    platform_info: PlatformInfo,
    /// Validation context
    #[allow(dead_code)]
    context: ValidationContext,
    /// Cached validation results for performance
    cache: HashMap<String, ValidationResult>,
}

impl CrossPlatformValidator {
    /// Create a new cross-platform validator
    pub fn new() -> CoreResult<Self> {
        let platform_info = Self::detect_platform_info()?;
        Ok(Self {
            platform_info,
            context: ValidationContext::default(),
            cache: HashMap::new(),
        })
    }

    /// Create a validator with custom context
    pub fn with_context(context: ValidationContext) -> CoreResult<Self> {
        let platform_info = Self::detect_platform_info()?;
        Ok(Self {
            platform_info,
            context,
            cache: HashMap::new(),
        })
    }

    /// Detect platform information at runtime
    fn detect_platform_info() -> CoreResult<PlatformInfo> {
        let os_family = if cfg!(target_family = "wasm") {
            OsFamily::Wasm
        } else if cfg!(windows) {
            OsFamily::Windows
        } else if cfg!(unix) {
            OsFamily::Unix
        } else {
            OsFamily::Unknown
        };

        let arch = if cfg!(target_arch = "wasm32") {
            CpuArchitecture::Wasm32
        } else if cfg!(target_arch = "wasm64") {
            CpuArchitecture::Wasm64
        } else if cfg!(target_arch = "x86_64") {
            CpuArchitecture::X86_64
        } else if cfg!(target_arch = "aarch64") {
            CpuArchitecture::AArch64
        } else if cfg!(target_arch = "x86") {
            CpuArchitecture::X86
        } else if cfg!(target_arch = "arm") {
            CpuArchitecture::ARM
        } else if cfg!(target_arch = "riscv64") {
            CpuArchitecture::RISCV64
        } else if cfg!(target_arch = "powerpc64") {
            CpuArchitecture::PowerPC64
        } else {
            CpuArchitecture::Other(0)
        };

        let endianness = if cfg!(target_endian = "little") {
            Endianness::Little
        } else {
            Endianness::Big
        };

        let path_separator = if cfg!(windows) {
            '\\'
        } else {
            '/' // Unix-style paths for all non-Windows platforms (including WASM)
        };

        let max_path_length = if cfg!(target_family = "wasm") {
            1024 // Conservative limit for WASM environments
        } else if cfg!(windows) {
            260 // MAX_PATH on Windows (unless long path support is enabled)
        } else {
            4096 // Common limit on Unix systems
        };

        // Detect SIMD support
        let simd_support = Self::detect_simd_support(arch);

        // Detect system page size
        let page_size = Self::detect_page_size();

        let memory_mapping_support = !cfg!(target_family = "wasm");

        let fp_behavior = FloatingPointBehavior {
            denormals_supported: true, // Most modern platforms support denormals
            rounding_mode: RoundingMode::ToNearest,
            nan_propagation: true,
            infinity_supported: true,
        };

        Ok(PlatformInfo {
            os_family,
            arch,
            simd_support,
            endianness,
            path_separator,
            max_path_length,
            page_size,
            memory_mapping_support,
            fp_behavior,
        })
    }

    /// Detect SIMD instruction set support
    fn detect_simd_support(arch: CpuArchitecture) -> SimdSupport {
        match arch {
            CpuArchitecture::X86_64 | CpuArchitecture::X86 => {
                // For x86/x64, we'd normally use cpuid to detect features
                // For now, provide conservative defaults
                SimdSupport {
                    sse: Some(SseLevel::Sse2), // SSE2 is guaranteed on x64
                    avx: if cfg!(target_feature = "avx2") {
                        Some(AvxLevel::Avx2)
                    } else if cfg!(target_feature = "avx") {
                        Some(AvxLevel::Avx)
                    } else {
                        None
                    },
                    neon: false,
                    sve: false,
                    vector_width: if cfg!(target_feature = "avx512f") {
                        512
                    } else if cfg!(target_feature = "avx2") {
                        256
                    } else {
                        128
                    },
                }
            }
            CpuArchitecture::AArch64 | CpuArchitecture::ARM => {
                SimdSupport {
                    sse: None,
                    avx: None,
                    neon: true,        // NEON is standard on ARM64
                    sve: false,        // SVE detection would require runtime checks
                    vector_width: 128, // Default ARM NEON width
                }
            }
            CpuArchitecture::Wasm32 | CpuArchitecture::Wasm64 => {
                SimdSupport {
                    sse: None,
                    avx: None,
                    neon: false,
                    sve: false,
                    vector_width: if cfg!(target_feature = "simd128") {
                        128 // WASM SIMD128 support
                    } else {
                        64 // No SIMD support
                    },
                }
            }
            _ => {
                SimdSupport {
                    sse: None,
                    avx: None,
                    neon: false,
                    sve: false,
                    vector_width: 64, // Conservative default
                }
            }
        }
    }

    /// Detect system page size
    fn detect_page_size() -> usize {
        #[cfg(unix)]
        {
            // Most Unix systems use 4KB pages, with some using 64KB (especially ARM64)
            // For simplicity, we'll use 4KB as default since it's most common
            4096
        }
        #[cfg(windows)]
        {
            // Windows typically uses 4KB pages, but can be 64KB on some systems
            // For simplicity, use the common default
            4096
        }
        #[cfg(not(any(unix, windows)))]
        {
            4096 // Default page size
        }
    }

    /// Validate a file path for the current platform
    pub fn validate_file_path(&mut self, path: &str) -> ValidationResult {
        let mut result = ValidationResult {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            metrics: crate::validation::production::ValidationMetrics::default(),
        };

        // Check path length limits
        if path.len() > self.platform_info.max_path_length {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "PATH_TOO_LONG".to_string(),
                message: format!(
                    "Path length {} exceeds platform maximum of {}",
                    path.len(),
                    self.platform_info.max_path_length
                ),
                field: Some(path.to_string()),
                suggestion: Some("Use shorter path or enable long path support ".to_string()),
                severity: ValidationSeverity::Error,
            });
        }

        // Platform-specific path validation
        match self.platform_info.os_family {
            OsFamily::Windows => self.validate_windows_path(path, &mut result),
            OsFamily::Unix => self.validate_unix_path(path, &mut result),
            OsFamily::Wasm => self.validate_wasm_path(path, &mut result),
            OsFamily::Unknown => {
                result
                    .warnings
                    .push("Unknown platform - basic validation only ".to_string());
            }
        }

        // Check for null bytes (invalid on all platforms)
        if path.contains('\0') {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "NULL_BYTE_IN_PATH".to_string(),
                message: "Path contains null byte ".to_string(),
                field: Some(path.to_string()),
                suggestion: Some("Remove null bytes from path ".to_string()),
                severity: ValidationSeverity::Critical,
            });
        }

        result
    }

    /// Validate Windows-specific path constraints
    fn validate_windows_path(&self, path: &str, result: &mut ValidationResult) {
        // Check for invalid characters
        let invalid_chars = r#"<>:"|?*"#.chars().collect::<Vec<_>>();
        for &ch in &invalid_chars {
            if path.contains(ch) {
                result.is_valid = false;
                result.errors.push(ValidationError {
                    code: "INVALID_WINDOWS_CHAR".to_string(),
                    message: format!("Character '{ch}' is invalid in Windows paths"),
                    field: Some(path.to_string()),
                    suggestion: Some("Remove or replace invalid characters".to_string()),
                    severity: ValidationSeverity::Error,
                });
                break;
            }
        }

        // Check for reserved names
        let reserved_names = [
            "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
            "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
        ];

        let path_upper = path.to_uppercase();
        for &reserved in &reserved_names {
            if path_upper == reserved || path_upper.starts_with(&format!("{reserved}.")) {
                result.is_valid = false;
                result.errors.push(ValidationError {
                    code: "RESERVED_WINDOWS_NAME".to_string(),
                    message: format!("'{reserved}' is a reserved name on Windows"),
                    field: Some(path.to_string()),
                    suggestion: Some("Use a different filename".to_string()),
                    severity: ValidationSeverity::Error,
                });
                break;
            }
        }

        // Check for trailing spaces or periods
        if path.ends_with(' ') || path.ends_with('.') {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "INVALID_WINDOWS_ENDING".to_string(),
                message: "Windows paths cannot end with spaces or periods".to_string(),
                field: Some(path.to_string()),
                suggestion: Some("Remove trailing spaces or periods".to_string()),
                severity: ValidationSeverity::Error,
            });
        }
    }

    /// Validate Unix-specific path constraints
    fn validate_unix_path(&self, path: &str, result: &mut ValidationResult) {
        // Unix paths are generally more permissive, but check for some edge cases

        // Check for double slashes (while technically valid, often unintended)
        if path.contains("//") {
            result
                .warnings
                .push("Path contains double slashes".to_string());
        }

        // Check if path starts with /dev/, /proc/, or /sys/ - potentially dangerous
        let system_prefixes = ["/dev/", "/proc/", "/sys/"];
        for &prefix in &system_prefixes {
            if path.starts_with(prefix) {
                result.warnings.push(format!(
                    "Path accesses system directory '{prefix}' - ensure this is intended"
                ));
                break;
            }
        }

        // Check for very long path components (while Unix supports long names,
        // some filesystems have limits)
        for component in path.split('/') {
            if component.len() > 255 {
                result
                    .warnings
                    .push("Path component exceeds 255 characters".to_string());
                break;
            }
        }
    }

    /// Validate WebAssembly-specific path constraints
    fn validate_wasm_path(&self, path: &str, result: &mut ValidationResult) {
        // WebAssembly has very limited file system access

        // Check if path is attempting to access outside the sandbox
        if path.starts_with("../") || path.contains("/../") {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "WASM_SANDBOX_VIOLATION".to_string(),
                message: "WebAssembly paths cannot escape sandbox with '..'".to_string(),
                field: Some(path.to_string()),
                suggestion: Some("Use paths relative to the WASM module".to_string()),
                severity: ValidationSeverity::Critical,
            });
        }

        // Check for absolute paths (typically not allowed in WASM)
        if path.starts_with('/') {
            result.warnings.push(
                "Absolute paths may not be accessible in WebAssembly environment".to_string(),
            );
        }

        // Check for special protocols that might not work in WASM
        let special_prefixes = ["file://", "http://", "https://", "ftp://"];
        for &prefix in &special_prefixes {
            if path.starts_with(prefix) {
                result.warnings.push(format!(
                    "Protocol '{prefix}' may not be accessible in WebAssembly environment"
                ));
                break;
            }
        }

        // WASM has stricter limits on path components
        for component in path.split('/') {
            if component.len() > 128 {
                result.warnings.push(
                    "Very long path components may not be supported in WebAssembly".to_string(),
                );
                break;
            }
        }

        // Check for WASM-specific virtual file system conventions
        if path.starts_with("/tmp/") || path.starts_with("/temp/") {
            result.warnings.push(
                "Temporary directories may have limited persistence in WebAssembly".to_string(),
            );
        }

        // General warning about WASM file system limitations
        result
            .warnings
            .push("WebAssembly environment has limited file system access".to_string());
    }

    /// Validate numeric value considering platform-specific floating-point behavior
    pub fn validate_numeric_cross_platform<T>(
        &mut self,
        value: T,
        fieldname: &str,
    ) -> ValidationResult
    where
        T: PartialOrd + Copy + std::fmt::Debug + std::fmt::Display + 'static,
    {
        let mut result = ValidationResult {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            metrics: crate::validation::production::ValidationMetrics::default(),
        };

        // Check for platform-specific numeric issues
        if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>()
            || std::any::TypeId::of::<T>() == std::any::TypeId::of::<f64>()
        {
            self.validate_floating_point_value(&value, fieldname, &mut result);
        }

        // Check for endianness-sensitive operations
        if self.platform_info.endianness == Endianness::Big {
            result.warnings.push(
                "Running on big-endian platform - verify binary data compatibility".to_string(),
            );
        }

        result
    }

    /// Validate floating-point value considering platform behavior
    fn validate_floating_point_value<T>(
        &self,
        value: &T,
        fieldname: &str,
        result: &mut ValidationResult,
    ) where
        T: std::fmt::Debug + std::fmt::Display,
    {
        // This is a simplified check - in practice we'd need unsafe transmutation
        // to properly inspect the floating-point representation
        let value_str = format!("{value:?}");

        if value_str.contains("inf") && !self.platform_info.fp_behavior.infinity_supported {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "INFINITY_NOT_SUPPORTED".to_string(),
                message: format!("Infinity values not supported on this platform for {fieldname}"),
                field: Some(fieldname.to_string()),
                suggestion: Some("Use finite values only".to_string()),
                severity: ValidationSeverity::Error,
            });
        }

        if value_str.contains("nan") && !self.platform_info.fp_behavior.nan_propagation {
            result.warnings.push(format!(
                "NaN value in {fieldname} - platform may not handle NaN propagation correctly"
            ));
        }
    }

    /// Validate SIMD operation compatibility
    pub fn validate_simd_operation(
        &mut self,
        operation: &str,
        _data_size: usize,
        vector_size: usize,
    ) -> ValidationResult {
        let mut result = ValidationResult {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            metrics: crate::validation::production::ValidationMetrics::default(),
        };

        // Check if requested vector size is supported
        if vector_size > self.platform_info.simd_support.vector_width {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "SIMD_VECTOR_TOO_LARGE".to_string(),
                message: format!(
                    "Requested vector size {} exceeds platform maximum of {}",
                    vector_size, self.platform_info.simd_support.vector_width
                ),
                field: Some(vector_size.to_string()),
                suggestion: Some(format!(
                    "Use vector size <= {}",
                    self.platform_info.simd_support.vector_width
                )),
                severity: ValidationSeverity::Error,
            });
        }

        // Check operation-specific requirements
        if operation.contains("avx") && self.platform_info.simd_support.avx.is_none() {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "AVX_NOT_SUPPORTED".to_string(),
                message: "AVX instructions not supported on this platform".to_string(),
                field: Some(operation.to_string()),
                suggestion: Some("Use SSE fallback or check platform capabilities".to_string()),
                severity: ValidationSeverity::Error,
            });
        }

        if operation.contains("neon") && !self.platform_info.simd_support.neon {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "NEON_NOT_SUPPORTED".to_string(),
                message: "NEON instructions not supported on this platform".to_string(),
                field: Some(operation.to_string()),
                suggestion: Some("Use scalar fallback".to_string()),
                severity: ValidationSeverity::Error,
            });
        }

        result
    }

    /// Validate memory allocation size considering platform limits
    pub fn validate_memory_allocation(&mut self, size: usize, purpose: &str) -> ValidationResult {
        let mut result = ValidationResult {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            metrics: crate::validation::production::ValidationMetrics::default(),
        };

        // Check if allocation is aligned to page size for optimal performance
        if size > self.platform_info.page_size && size % self.platform_info.page_size != 0 {
            result.warnings.push(format!(
                "Allocation size {} is not page-aligned (page size: {})",
                size, self.platform_info.page_size
            ));
        }

        // Platform-specific memory limits
        let max_alloc_size = match self.platform_info.arch {
            CpuArchitecture::X86 => 2usize.pow(31), // 2GB limit for 32-bit
            CpuArchitecture::ARM => 2usize.pow(31),
            CpuArchitecture::Wasm32 => 2usize.pow(31), // WASM32 has 32-bit address space
            CpuArchitecture::Wasm64 => {
                // WASM64 is limited by browser memory constraints
                4usize.pow(30) // 1GB conservative limit for WASM64
            }
            _ => usize::MAX, // 64-bit platforms
        };

        if size > max_alloc_size {
            result.is_valid = false;
            result.errors.push(ValidationError {
                code: "ALLOCATION_TOO_LARGE".to_string(),
                message: format!(
                    "Allocation size {size} exceeds platform maximum of {max_alloc_size} for {purpose}"
                ),
                field: Some(size.to_string()),
                suggestion: Some("Reduce allocation size or use memory mapping".to_string()),
                severity: ValidationSeverity::Error,
            });
        }

        // Check if memory mapping is needed but not supported
        if size > 100_000_000 && !self.platform_info.memory_mapping_support {
            result.warnings.push(format!(
                "Large allocation ({size} bytes) for {purpose} but memory mapping not supported"
            ));
        }

        result
    }

    /// Get current platform information
    pub fn platform_info(&self) -> &PlatformInfo {
        &self.platform_info
    }

    /// Clear validation cache
    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }

    /// Check if a specific platform feature is available
    pub fn is_feature_available(&self, feature: PlatformFeature) -> bool {
        match feature {
            PlatformFeature::MemoryMapping => self.platform_info.memory_mapping_support,
            PlatformFeature::Avx => self.platform_info.simd_support.avx.is_some(),
            PlatformFeature::Neon => self.platform_info.simd_support.neon,
            PlatformFeature::LongPaths => {
                // This would require more sophisticated detection in practice
                matches!(self.platform_info.os_family, OsFamily::Unix)
            }
            PlatformFeature::DenormalNumbers => self.platform_info.fp_behavior.denormals_supported,
            PlatformFeature::WasmSimd128 => {
                matches!(
                    self.platform_info.arch,
                    CpuArchitecture::Wasm32 | CpuArchitecture::Wasm64
                ) && self.platform_info.simd_support.vector_width >= 128
            }
            PlatformFeature::ThreadSupport => {
                // WASM traditionally doesn't support threads, but some environments do
                !matches!(self.platform_info.os_family, OsFamily::Wasm)
            }
            PlatformFeature::FileSystemAccess => {
                // WASM has very limited file system access
                !matches!(self.platform_info.os_family, OsFamily::Wasm)
            }
        }
    }
}

/// Platform features that can be queried
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlatformFeature {
    MemoryMapping,
    Avx,
    Neon,
    LongPaths,
    DenormalNumbers,
    WasmSimd128,
    ThreadSupport,
    FileSystemAccess,
}

impl Default for CrossPlatformValidator {
    fn default() -> Self {
        Self::new().expect("Failed to create cross-platform validator")
    }
}

/// Convenience functions for common cross-platform validations
/// Validate that a path is appropriate for the current platform
#[allow(dead_code)]
pub fn validate_path(path: &str) -> CoreResult<()> {
    let mut validator = CrossPlatformValidator::new()?;
    let result = validator.validate_file_path(path);

    if result.is_valid {
        Ok(())
    } else {
        Err(CoreError::ValidationError(ErrorContext::new(format!(
            "Path validation failed: {:?}",
            result.errors
        ))))
    }
}

/// Validate SIMD capability for an operation
#[allow(dead_code)]
pub fn validate_simd_capability(operation: &str, size: usize) -> CoreResult<()> {
    let mut validator = CrossPlatformValidator::new()?;
    let result = validator.validate_simd_operation(operation, size, 128);

    if result.is_valid {
        Ok(())
    } else {
        Err(CoreError::ValidationError(ErrorContext::new(format!(
            "SIMD validation failed: {:?}",
            result.errors
        ))))
    }
}

/// Get platform information
#[allow(dead_code)]
pub fn get_platform_info() -> CoreResult<PlatformInfo> {
    CrossPlatformValidator::detect_platform_info()
}

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

    #[test]
    fn test_platform_detection() {
        let info = CrossPlatformValidator::detect_platform_info().expect("Operation failed");

        // Basic sanity checks
        assert_ne!(info.os_family, OsFamily::Unknown);
        assert!(info.page_size > 0);
        assert!(info.max_path_length > 0);
        assert!(info.simd_support.vector_width > 0);
    }

    #[test]
    fn test_path_validation() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Valid path
        let result = validator.validate_file_path("/home/user/data.txt");
        assert!(result.is_valid);

        // Path with null byte
        let result = validator.validate_file_path("/home/user\0/data.txt");
        assert!(!result.is_valid);
    }

    #[cfg(windows)]
    #[test]
    fn test_windows_path_validation() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Valid Windows path
        let result = validator.validate_file_path("C:\\Users\\user\\data.txt");
        assert!(result.is_valid);

        // Invalid character
        let result = validator.validate_file_path("C:\\Users\\user<data.txt");
        assert!(!result.is_valid);

        // Reserved name
        let result = validator.validate_file_path("CON");
        assert!(!result.is_valid);
    }

    #[cfg(unix)]
    #[test]
    fn test_unix_path_validation() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Valid Unix path
        let result = validator.validate_file_path("/home/user/data.txt");
        assert!(result.is_valid);

        // System directory warning
        let result = validator.validate_file_path("/dev/null");
        assert!(result.is_valid);
        assert!(!result.warnings.is_empty());
    }

    #[test]
    fn test_simd_validation() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Valid vector size
        let result = validator.validate_simd_operation("add", 128, 128);
        assert!(result.is_valid);

        // Too large vector size
        let result = validator.validate_simd_operation("add", 10000, 10000);
        assert!(!result.is_valid);
    }

    #[test]
    fn test_memory_allocation_validation() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Normal allocation
        let result = validator.validate_memory_allocation(1024, "test");
        assert!(result.is_valid);

        // Very large allocation
        let result = validator.validate_memory_allocation(usize::MAX - 1, "test");
        // Result depends on platform - 32-bit will fail, 64-bit might succeed
    }

    #[test]
    fn test_feature_availability() {
        let validator = CrossPlatformValidator::new().expect("Operation failed");

        // These should return boolean values without panicking
        let memory_mapping = validator.is_feature_available(PlatformFeature::MemoryMapping);
        let avx = validator.is_feature_available(PlatformFeature::Avx);
        let neon = validator.is_feature_available(PlatformFeature::Neon);
    }

    #[test]
    fn test_convenience_functions() {
        // These should not panic
        let _ = validate_path("/tmp/test.txt");
        let _ = validate_simd_capability("add", 128);
        let _ = get_platform_info();
    }

    #[test]
    fn test_wasm_specific_features() {
        let validator = CrossPlatformValidator::new().expect("Operation failed");

        // Test WASM-specific feature detection
        let wasm_simd = validator.is_feature_available(PlatformFeature::WasmSimd128);
        let thread_support = validator.is_feature_available(PlatformFeature::ThreadSupport);
        let fs_access = validator.is_feature_available(PlatformFeature::FileSystemAccess);

        // These should return boolean values without panicking
        // Test passes if we reach here without panicking
    }

    #[test]
    fn test_wasm_path_validation() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Simulate WASM environment for testing
        // Note: This test will behave differently on actual WASM vs native platforms

        // Test relative path (should be okay in WASM)
        let result = validator.validate_file_path("data/input.txt");
        // Should be valid but may have warnings in WASM

        // Test sandbox escape attempt
        let result = validator.validate_file_path("../../../etc/passwd");
        // This would be rejected in actual WASM validation

        // Just ensure these don't panic
        // Test passes if we reach here without panicking
    }

    #[test]
    fn test_platform_memory_limits() {
        let validator = CrossPlatformValidator::new().expect("Operation failed");

        // Test that memory allocation validation considers platform architecture
        let small_alloc = validator.platform_info().page_size * 2;
        let large_alloc = 2usize.pow(30); // 1GB

        // These should not panic
        let mut validator_mut = CrossPlatformValidator::new().expect("Operation failed");
        let small_result = validator_mut.validate_memory_allocation(small_alloc, "test");
        let large_result = validator_mut.validate_memory_allocation(large_alloc, "test");

        // Test passes if we reach here without panicking
    }

    #[test]
    fn test_simd_capabilities_cross_platform() {
        let mut validator = CrossPlatformValidator::new().expect("Operation failed");

        // Test SIMD validation across different architectures
        let result = validator.validate_simd_operation("generic_add", 64, 64);
        assert!(result.is_valid); // Should be supported on all platforms

        let result = validator.validate_simd_operation("avx2_multiply", 256, 256);
        // Result depends on platform - should not panic

        let result = validator.validate_simd_operation("neon_add", 128, 128);
        // Result depends on platform - should not panic

        // Test passes if we reach here without panicking
    }
}