memscope-rs 0.2.0

A memory tracking library for Rust applications.
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
//! Unified error handling system for memscope-rs
//!
//! This module provides a simplified, efficient error handling system that:
//! - Reduces string cloning overhead using `Arc<str>`
//! - Maintains all existing error information
//! - Provides error recovery mechanisms
//! - Ensures backward compatibility

use std::fmt;
use std::sync::Arc;
use std::time::SystemTime;

/// Error context information for tracking and debugging
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ErrorContext {
    /// Timestamp when the error occurred
    pub timestamp: SystemTime,
    /// File where the error originated
    pub file: Option<String>,
    /// Line number where the error originated
    pub line: Option<u32>,
    /// Additional context information
    pub extra: Option<String>,
}

impl Default for ErrorContext {
    fn default() -> Self {
        Self {
            timestamp: SystemTime::now(),
            file: None,
            line: None,
            extra: None,
        }
    }
}

impl ErrorContext {
    /// Create new error context
    pub fn new() -> Self {
        Self::default()
    }

    /// Create error context with file and line information
    pub fn with_location(file: impl Into<String>, line: u32) -> Self {
        Self {
            timestamp: SystemTime::now(),
            file: Some(file.into()),
            line: Some(line),
            extra: None,
        }
    }

    /// Add extra context information
    pub fn with_extra(mut self, extra: impl Into<String>) -> Self {
        self.extra = Some(extra.into());
        self
    }
}

/// Unified error type for the entire memscope-rs system
///
/// This replaces the complex TrackingError with a simpler, more efficient design
/// while maintaining all existing error information and backward compatibility.
#[derive(Debug, Clone)]
pub enum MemScopeError {
    /// Memory tracking operations (allocation, deallocation, association)
    Memory {
        operation: MemoryOperation,
        message: Arc<str>,
        context: Option<Arc<str>>,
        error_kind: ErrorKind,
        error_context: ErrorContext,
    },

    /// Analysis operations (fragmentation, lifecycle, etc.)
    Analysis {
        analyzer: Arc<str>,
        message: Arc<str>,
        recoverable: bool,
        error_kind: ErrorKind,
        error_context: ErrorContext,
    },

    /// Export operations (JSON, binary, HTML, etc.)
    Export {
        format: Arc<str>,
        message: Arc<str>,
        partial_success: bool,
        error_kind: ErrorKind,
        error_context: ErrorContext,
    },

    /// Configuration and initialization errors
    Configuration {
        component: Arc<str>,
        message: Arc<str>,
        error_kind: ErrorKind,
        error_context: ErrorContext,
    },

    /// System-level errors (IO, threading, etc.)
    System {
        error_type: SystemErrorType,
        message: Arc<str>,
        source_message: Option<Arc<str>>,
        error_kind: ErrorKind,
        error_context: ErrorContext,
    },

    /// Internal errors that should not normally occur
    Internal {
        message: Arc<str>,
        location: Option<Arc<str>>,
        error_kind: ErrorKind,
        error_context: ErrorContext,
    },
}

/// Memory operation types for better error categorization
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryOperation {
    Allocation,
    Deallocation,
    Association,
    Tracking,
    Validation,
}

/// System error types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SystemErrorType {
    Io,
    Threading,
    Locking,
    Channel,
    Serialization,
    Network,
    FileSystem,
}

/// Result type alias for MemScopeError
pub type Result<T> = std::result::Result<T, MemScopeError>;

/// Result type alias for MemScopeError (alternative name)
pub type MemScopeResult<T> = std::result::Result<T, MemScopeError>;

impl MemScopeError {
    /// Unified error creation - external modules only need to provide:
    /// - module: which module the error occurred in (e.g., "variable_registry")
    /// - method: which method/function (e.g., "register_variable")
    /// - message: what went wrong
    ///
    /// Internal classification by module:
    /// - MemoryError: tracker, allocator, variable_registry, memory_tracker, capture
    /// - ExportError: export, render_engine, snapshot
    /// - AnalysisError: ffi_function_resolver, call_stack_normalizer, analysis,
    ///   lifecycle, rule_engine, pattern_matcher, detector, unsafe_ffi_tracker
    /// - ConfigurationError: config, initialization
    /// - IoError: system errors (io, threading, locking, network, filesystem)
    /// - InternalError: fallback for unknown modules
    pub fn error(module: &str, method: &str, message: impl Into<Arc<str>>) -> Self {
        let kind = match module {
            // Memory tracking modules
            "tracker" | "allocator" | "variable_registry" | "memory_tracker" | "capture"
            | "global_tracking" | "async_tracker" | "unified_tracker" | "core_tracker"
            | "backends" | "platform" | "stack_walker" | "symbol_resolver" | "memory_info"
            | "async_types" => ErrorKind::MemoryError,

            // Export modules
            "export" | "render_engine" | "snapshot" => ErrorKind::ExportError,

            // Analysis modules
            "ffi_function_resolver"
            | "call_stack_normalizer"
            | "analysis"
            | "lifecycle"
            | "lifecycle_analysis"
            | "rule_engine"
            | "pattern_matcher"
            | "detector"
            | "unsafe_ffi_tracker"
            | "classification"
            | "estimation"
            | "metrics"
            | "quality"
            | "circular_reference"
            | "memory_passport_tracker"
            | "borrow_analysis"
            | "async_analysis" => ErrorKind::AnalysisError,

            // Configuration modules
            "config" | "initialization" => ErrorKind::ConfigurationError,

            // System errors
            "system" | "io" | "threading" | "locking" | "network" | "filesystem"
            | "serialization" => ErrorKind::IoError,

            // Fallback
            _ => ErrorKind::InternalError,
        };
        let full_message = format!("{}::{} - {}", module, method, message.into());
        Self::new(kind, full_message)
    }

    /// Create a new error with the specified kind and message
    pub fn new(kind: ErrorKind, message: impl Into<Arc<str>>) -> Self {
        match kind {
            ErrorKind::MemoryError => Self::Memory {
                operation: MemoryOperation::Tracking,
                message: message.into(),
                context: None,
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
            ErrorKind::ValidationError => Self::Memory {
                operation: MemoryOperation::Validation,
                message: message.into(),
                context: None,
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
            ErrorKind::ConfigurationError => Self::Configuration {
                component: "general".into(),
                message: message.into(),
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
            ErrorKind::IoError | ErrorKind::CacheError => Self::System {
                error_type: SystemErrorType::Io,
                message: message.into(),
                source_message: None,
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
            ErrorKind::SymbolResolutionError
            | ErrorKind::StackTraceError
            | ErrorKind::AnalysisError => Self::Analysis {
                analyzer: match kind {
                    ErrorKind::SymbolResolutionError => "symbol_resolution",
                    ErrorKind::StackTraceError => "stack_trace",
                    _ => "general",
                }
                .into(),
                message: message.into(),
                recoverable: true,
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
            ErrorKind::ExportError => Self::Export {
                format: "general".into(),
                message: message.into(),
                partial_success: false,
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
            ErrorKind::InternalError => Self::Internal {
                message: message.into(),
                location: None,
                error_kind: kind,
                error_context: ErrorContext::new(),
            },
        }
    }

    /// Create a new error with the specified kind, severity, and context
    pub fn with_context(
        kind: ErrorKind,
        _severity: ErrorSeverity,
        message: impl Into<Arc<str>>,
        context: impl Into<Arc<str>>,
    ) -> Self {
        let mut error = Self::new(kind, message);
        // Update error context with the provided context information
        match &mut error {
            Self::Memory { error_context, .. }
            | Self::Analysis { error_context, .. }
            | Self::Export { error_context, .. }
            | Self::Configuration { error_context, .. }
            | Self::System { error_context, .. }
            | Self::Internal { error_context, .. } => {
                error_context.extra = Some(context.into().to_string());
            }
        }
        error
    }

    /// Create a memory operation error
    pub fn memory(operation: MemoryOperation, message: impl Into<Arc<str>>) -> Self {
        Self::Memory {
            operation,
            message: message.into(),
            context: None,
            error_kind: ErrorKind::MemoryError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create a memory operation error with context
    pub fn memory_with_context(
        operation: MemoryOperation,
        message: impl Into<Arc<str>>,
        context: impl Into<Arc<str>>,
    ) -> Self {
        Self::Memory {
            operation,
            message: message.into(),
            context: Some(context.into()),
            error_kind: ErrorKind::MemoryError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create an analysis error
    pub fn analysis(analyzer: impl Into<Arc<str>>, message: impl Into<Arc<str>>) -> Self {
        Self::Analysis {
            analyzer: analyzer.into(),
            message: message.into(),
            recoverable: true,
            error_kind: ErrorKind::AnalysisError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create a non-recoverable analysis error
    pub fn analysis_fatal(analyzer: impl Into<Arc<str>>, message: impl Into<Arc<str>>) -> Self {
        Self::Analysis {
            analyzer: analyzer.into(),
            message: message.into(),
            recoverable: false,
            error_kind: ErrorKind::AnalysisError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create an export error
    pub fn export(format: impl Into<Arc<str>>, message: impl Into<Arc<str>>) -> Self {
        Self::Export {
            format: format.into(),
            message: message.into(),
            partial_success: false,
            error_kind: ErrorKind::ExportError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create an export error with partial success
    pub fn export_partial(format: impl Into<Arc<str>>, message: impl Into<Arc<str>>) -> Self {
        Self::Export {
            format: format.into(),
            message: message.into(),
            partial_success: true,
            error_kind: ErrorKind::ExportError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create an export error with source information
    pub fn export_with_source(
        format: impl Into<Arc<str>>,
        message: impl Into<Arc<str>>,
        source: impl AsRef<str>,
    ) -> Self {
        Self::Export {
            format: format.into(),
            message: format!("{} (source: {})", message.into(), source.as_ref()).into(),
            partial_success: false,
            error_kind: ErrorKind::ExportError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create a configuration error
    pub fn config(component: impl Into<Arc<str>>, message: impl Into<Arc<str>>) -> Self {
        Self::Configuration {
            component: component.into(),
            message: message.into(),
            error_kind: ErrorKind::ConfigurationError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create a system error
    pub fn system(error_type: SystemErrorType, message: impl Into<Arc<str>>) -> Self {
        Self::System {
            error_type,
            message: message.into(),
            source_message: None,
            error_kind: ErrorKind::IoError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create a system error with source
    pub fn system_with_source(
        error_type: SystemErrorType,
        message: impl Into<Arc<str>>,
        source: impl std::error::Error,
    ) -> Self {
        Self::System {
            error_type,
            message: message.into(),
            source_message: Some(Arc::from(source.to_string())),
            error_kind: ErrorKind::IoError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create an internal error
    pub fn internal(message: impl Into<Arc<str>>) -> Self {
        Self::Internal {
            message: message.into(),
            location: None,
            error_kind: ErrorKind::InternalError,
            error_context: ErrorContext::new(),
        }
    }

    /// Create an internal error with location
    pub fn internal_at(message: impl Into<Arc<str>>, location: impl Into<Arc<str>>) -> Self {
        Self::Internal {
            message: message.into(),
            location: Some(location.into()),
            error_kind: ErrorKind::InternalError,
            error_context: ErrorContext::new(),
        }
    }

    /// Check if this error is recoverable
    pub fn is_recoverable(&self) -> bool {
        match self {
            Self::Memory { .. } => true,
            Self::Analysis { recoverable, .. } => *recoverable,
            Self::Export {
                partial_success, ..
            } => *partial_success,
            Self::Configuration { .. } => false,
            Self::System { error_type, .. } => matches!(
                error_type,
                SystemErrorType::Io | SystemErrorType::Network | SystemErrorType::Locking
            ),
            Self::Internal { .. } => false,
        }
    }

    /// Get error kind for categorization and statistics
    pub fn kind(&self) -> ErrorKind {
        match self {
            Self::Memory { error_kind, .. } => *error_kind,
            Self::Analysis { error_kind, .. } => *error_kind,
            Self::Export { error_kind, .. } => *error_kind,
            Self::Configuration { error_kind, .. } => *error_kind,
            Self::System { error_kind, .. } => *error_kind,
            Self::Internal { error_kind, .. } => *error_kind,
        }
    }

    /// Get error severity level
    pub fn severity(&self) -> ErrorSeverity {
        match self {
            Self::Memory { .. } => ErrorSeverity::Error,
            Self::Analysis {
                recoverable: false, ..
            } => ErrorSeverity::Critical,
            Self::Analysis { .. } => ErrorSeverity::Warning,
            Self::Export {
                partial_success: true,
                ..
            } => ErrorSeverity::Warning,
            Self::Export { .. } => ErrorSeverity::Error,
            Self::Configuration { .. } => ErrorSeverity::Error,
            Self::System { .. } => ErrorSeverity::Error,
            Self::Internal { .. } => ErrorSeverity::Fatal,
        }
    }

    /// Get a user-friendly error message
    pub fn user_message(&self) -> &str {
        match self {
            Self::Memory { message, .. } => message,
            Self::Analysis { message, .. } => message,
            Self::Export { message, .. } => message,
            Self::Configuration { message, .. } => message,
            Self::System { message, .. } => message,
            Self::Internal { message, .. } => message,
        }
    }

    /// Get error category for logging/metrics
    pub fn category(&self) -> &'static str {
        match self {
            Self::Memory { .. } => "memory",
            Self::Analysis { .. } => "analysis",
            Self::Export { .. } => "export",
            Self::Configuration { .. } => "config",
            Self::System { .. } => "system",
            Self::Internal { .. } => "internal",
        }
    }

    /// Get error context information
    pub fn context(&self) -> &ErrorContext {
        match self {
            Self::Memory { error_context, .. } => error_context,
            Self::Analysis { error_context, .. } => error_context,
            Self::Export { error_context, .. } => error_context,
            Self::Configuration { error_context, .. } => error_context,
            Self::System { error_context, .. } => error_context,
            Self::Internal { error_context, .. } => error_context,
        }
    }

    /// Get a detailed error description
    pub fn description(&self) -> String {
        format!("{}", self)
    }
}

/// Error kind classification for error tracking and statistics
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorKind {
    /// Memory-related errors
    MemoryError,
    /// Configuration errors
    ConfigurationError,
    /// I/O errors
    IoError,
    /// Symbol resolution errors
    SymbolResolutionError,
    /// Stack trace errors
    StackTraceError,
    /// Cache errors
    CacheError,
    /// Internal errors
    InternalError,
    /// Validation errors
    ValidationError,
    /// Analysis errors
    AnalysisError,
    /// Export errors
    ExportError,
}

impl ErrorKind {
    /// Get the default severity for this error kind
    pub fn default_severity(self) -> ErrorSeverity {
        match self {
            ErrorKind::MemoryError => ErrorSeverity::Error,
            ErrorKind::ConfigurationError => ErrorSeverity::Error,
            ErrorKind::IoError => ErrorSeverity::Error,
            ErrorKind::SymbolResolutionError => ErrorSeverity::Warning,
            ErrorKind::StackTraceError => ErrorSeverity::Warning,
            ErrorKind::CacheError => ErrorSeverity::Warning,
            ErrorKind::InternalError => ErrorSeverity::Fatal,
            ErrorKind::ValidationError => ErrorSeverity::Error,
            ErrorKind::AnalysisError => ErrorSeverity::Warning,
            ErrorKind::ExportError => ErrorSeverity::Error,
        }
    }
}

/// Error severity levels
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum ErrorSeverity {
    /// Non-critical issues that don't prevent operation
    Warning,
    /// Errors that prevent normal operation but may be recoverable
    Error,
    /// Critical errors that require immediate attention
    Critical,
    /// Fatal errors that cannot be recovered from
    Fatal,
}

impl fmt::Display for MemScopeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Memory {
                operation,
                message,
                context,
                error_kind: _,
                error_context: _,
            } => {
                write!(
                    f,
                    "Memory {} error: {}",
                    match operation {
                        MemoryOperation::Allocation => "allocation",
                        MemoryOperation::Deallocation => "deallocation",
                        MemoryOperation::Association => "association",
                        MemoryOperation::Tracking => "tracking",
                        MemoryOperation::Validation => "validation",
                    },
                    message
                )?;
                if let Some(ctx) = context {
                    write!(f, " (context: {ctx})")?;
                }
                Ok(())
            }
            Self::Analysis {
                analyzer,
                message,
                error_kind: _,
                ..
            } => {
                write!(f, "Analysis error in {analyzer}: {message}")
            }
            Self::Export {
                format,
                message,
                partial_success,
                error_kind: _,
                error_context: _,
            } => {
                if *partial_success {
                    write!(f, "Partial export error ({format}): {message}")
                } else {
                    write!(f, "Export error ({format}): {message}")
                }
            }
            Self::Configuration {
                component,
                message,
                error_kind: _,
                error_context: _,
            } => {
                write!(f, "Configuration error in {component}: {message}")
            }
            Self::System {
                error_type,
                message,
                source_message,
                error_kind: _,
                error_context: _,
            } => {
                write!(
                    f,
                    "{} error: {}",
                    match error_type {
                        SystemErrorType::Io => "I/O",
                        SystemErrorType::Threading => "Threading",
                        SystemErrorType::Locking => "Locking",
                        SystemErrorType::Channel => "Channel",
                        SystemErrorType::Serialization => "Serialization",
                        SystemErrorType::Network => "Network",
                        SystemErrorType::FileSystem => "File system",
                    },
                    message
                )?;
                if let Some(source) = source_message {
                    write!(f, " (source: {source})")?;
                }
                Ok(())
            }
            Self::Internal {
                message,
                location,
                error_kind: _,
                error_context: _,
            } => {
                write!(f, "Internal error: {message}")?;
                if let Some(loc) = location {
                    write!(f, " at {loc}")?;
                }
                Ok(())
            }
        }
    }
}

impl std::error::Error for MemScopeError {}

// Conversion from standard library errors
impl From<std::io::Error> for MemScopeError {
    fn from(err: std::io::Error) -> Self {
        Self::system_with_source(
            SystemErrorType::Io,
            format!("I/O operation failed: {err}"),
            err,
        )
    }
}

impl From<serde_json::Error> for MemScopeError {
    fn from(err: serde_json::Error) -> Self {
        Self::system_with_source(
            SystemErrorType::Serialization,
            format!("JSON serialization failed: {err}"),
            err,
        )
    }
}

impl From<crate::render_engine::export::ExportError> for MemScopeError {
    fn from(err: crate::render_engine::export::ExportError) -> Self {
        Self::error("export", "unknown", err.to_string())
    }
}

/// Error recovery strategies
#[derive(Debug, Clone)]
pub enum RecoveryAction {
    /// Retry the operation with the same parameters
    Retry { max_attempts: u32, delay_ms: u64 },
    /// Use a default value and continue
    UseDefault { value: String },
    /// Skip this operation and continue
    Skip,
    /// Abort the current operation
    Abort,
    /// Try an alternative approach
    Fallback { strategy: String },
}

/// Error recovery trait for implementing recovery strategies
pub trait ErrorRecovery {
    /// Determine if an error can be recovered from
    fn can_recover(&self, error: &MemScopeError) -> bool;

    /// Get the recovery action for an error
    fn get_recovery_action(&self, error: &MemScopeError) -> Option<RecoveryAction>;

    /// Execute recovery action
    fn execute_recovery(&self, action: &RecoveryAction) -> MemScopeResult<()>;
}

/// Default error recovery implementation
pub struct DefaultErrorRecovery {
    max_retries: u32,
    retry_delay_ms: u64,
}

impl DefaultErrorRecovery {
    pub fn new() -> Self {
        Self {
            max_retries: 3,
            retry_delay_ms: 100,
        }
    }

    pub fn with_config(max_retries: u32, retry_delay_ms: u64) -> Self {
        Self {
            max_retries,
            retry_delay_ms,
        }
    }
}

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

impl ErrorRecovery for DefaultErrorRecovery {
    fn can_recover(&self, error: &MemScopeError) -> bool {
        error.is_recoverable() && error.severity() != ErrorSeverity::Critical
    }

    fn get_recovery_action(&self, error: &MemScopeError) -> Option<RecoveryAction> {
        if !self.can_recover(error) {
            return Some(RecoveryAction::Abort);
        }

        match error {
            MemScopeError::Memory { .. } => Some(RecoveryAction::Retry {
                max_attempts: self.max_retries,
                delay_ms: self.retry_delay_ms,
            }),
            MemScopeError::Analysis { .. } => Some(RecoveryAction::UseDefault {
                value: "{}".to_string(), // Empty JSON object as default
            }),
            MemScopeError::Export {
                partial_success: true,
                ..
            } => Some(RecoveryAction::Skip),
            MemScopeError::Export { .. } => Some(RecoveryAction::Fallback {
                strategy: "minimal_export".to_string(),
            }),
            MemScopeError::System {
                error_type: SystemErrorType::Locking,
                ..
            } => Some(RecoveryAction::Retry {
                max_attempts: 1,
                delay_ms: 50,
            }),
            _ => Some(RecoveryAction::Abort),
        }
    }

    fn execute_recovery(&self, action: &RecoveryAction) -> MemScopeResult<()> {
        match action {
            RecoveryAction::Retry { delay_ms, .. } => {
                if *delay_ms > 0 {
                    std::thread::sleep(std::time::Duration::from_millis(*delay_ms));
                }
                Ok(())
            }
            RecoveryAction::UseDefault { .. } => Ok(()),
            RecoveryAction::Skip => Ok(()),
            RecoveryAction::Fallback { .. } => Ok(()),
            RecoveryAction::Abort => Err(MemScopeError::internal("Recovery aborted")),
        }
    }
}

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

    // Test all error creation methods
    #[test]
    fn test_memory_error_creation() {
        let err = MemScopeError::memory(MemoryOperation::Allocation, "allocation failed");
        assert!(matches!(
            err,
            MemScopeError::Memory {
                operation: MemoryOperation::Allocation,
                ..
            }
        ));
        assert_eq!(err.category(), "memory");
        assert_eq!(err.user_message(), "allocation failed");
        assert!(err.is_recoverable());
        assert_eq!(err.severity(), ErrorSeverity::Error);

        let err_with_context = MemScopeError::memory_with_context(
            MemoryOperation::Deallocation,
            "deallocation failed",
            "in cleanup",
        );
        assert!(matches!(
            err_with_context,
            MemScopeError::Memory {
                operation: MemoryOperation::Deallocation,
                ..
            }
        ));
        assert_eq!(err_with_context.category(), "memory");
    }

    #[test]
    fn test_analysis_error_creation() {
        let err = MemScopeError::analysis("fragmentation", "analysis failed");
        assert!(matches!(
            err,
            MemScopeError::Analysis {
                recoverable: true,
                ..
            }
        ));
        assert_eq!(err.category(), "analysis");
        assert!(err.is_recoverable());
        assert_eq!(err.severity(), ErrorSeverity::Warning);

        let fatal_err = MemScopeError::analysis_fatal("lifecycle", "critical analysis failed");
        assert!(matches!(
            fatal_err,
            MemScopeError::Analysis {
                recoverable: false,
                ..
            }
        ));
        assert_eq!(fatal_err.category(), "analysis");
        assert!(!fatal_err.is_recoverable());
        assert_eq!(fatal_err.severity(), ErrorSeverity::Critical);
    }

    #[test]
    fn test_export_error_creation() {
        let err = MemScopeError::export("json", "export failed");
        assert!(matches!(
            err,
            MemScopeError::Export {
                partial_success: false,
                ..
            }
        ));
        assert_eq!(err.category(), "export");
        assert!(!err.is_recoverable());
        assert_eq!(err.severity(), ErrorSeverity::Error);

        let partial_err = MemScopeError::export_partial("html", "partial export");
        assert!(matches!(
            partial_err,
            MemScopeError::Export {
                partial_success: true,
                ..
            }
        ));
        assert_eq!(partial_err.category(), "export");
        assert!(partial_err.is_recoverable());
        assert_eq!(partial_err.severity(), ErrorSeverity::Warning);
    }

    #[test]
    fn test_config_error_creation() {
        let err = MemScopeError::config("tracker", "invalid configuration");
        assert!(matches!(err, MemScopeError::Configuration { .. }));
        assert_eq!(err.category(), "config");
        assert!(!err.is_recoverable());
        assert_eq!(err.severity(), ErrorSeverity::Error);
    }

    #[test]
    fn test_system_error_creation() {
        let err = MemScopeError::system(SystemErrorType::Io, "io error");
        assert!(matches!(
            err,
            MemScopeError::System {
                error_type: SystemErrorType::Io,
                ..
            }
        ));
        assert_eq!(err.category(), "system");
        assert!(err.is_recoverable());
        assert_eq!(err.severity(), ErrorSeverity::Error);

        let io_err = io::Error::other("test io error");
        let converted_err: MemScopeError = io_err.into();
        assert!(matches!(
            converted_err,
            MemScopeError::System {
                error_type: SystemErrorType::Io,
                ..
            }
        ));
        assert_eq!(converted_err.category(), "system");
    }

    #[test]
    fn test_internal_error_creation() {
        let err = MemScopeError::internal("internal error");
        assert!(matches!(err, MemScopeError::Internal { .. }));
        assert_eq!(err.category(), "internal");
        assert!(!err.is_recoverable());
        assert_eq!(err.severity(), ErrorSeverity::Fatal);

        let err_with_location = MemScopeError::internal_at("internal error", "test_function");
        assert!(matches!(err_with_location, MemScopeError::Internal { .. }));
        assert_eq!(err_with_location.category(), "internal");
    }

    #[test]
    fn test_error_display_formatting() {
        // Test memory error display
        let mem_err = MemScopeError::memory_with_context(
            MemoryOperation::Allocation,
            "allocation failed",
            "in test function",
        );
        let mem_display = format!("{mem_err}");
        assert!(mem_display.contains("Memory allocation error"));
        assert!(mem_display.contains("allocation failed"));
        assert!(mem_display.contains("context: in test function"));

        // Test analysis error display
        let analysis_err = MemScopeError::analysis("fragmentation", "analysis failed");
        let analysis_display = format!("{analysis_err}");
        assert!(analysis_display.contains("Analysis error in fragmentation: analysis failed"));

        // Test export error display
        let export_err = MemScopeError::export("json", "export failed");
        let export_display = format!("{export_err}");
        assert!(export_display.contains("Export error (json): export failed"));

        let partial_export_err = MemScopeError::export_partial("html", "partial export");
        let partial_export_display = format!("{partial_export_err}");
        assert!(partial_export_display.contains("Partial export error (html): partial export"));

        // Test configuration error display
        let config_err = MemScopeError::config("tracker", "invalid config");
        let config_display = format!("{config_err}");
        assert!(config_display.contains("Configuration error in tracker: invalid config"));

        // Test system error display
        let system_err = MemScopeError::system(SystemErrorType::Io, "io error");
        let system_display = format!("{system_err}");
        assert!(system_display.contains("I/O error: io error"));

        // Test internal error display
        let internal_err = MemScopeError::internal_at("internal error", "test_function");
        let internal_display = format!("{internal_err}");
        assert!(internal_display.contains("Internal error: internal error at test_function"));
    }

    #[test]
    fn test_error_severity_ordering() {
        assert!(ErrorSeverity::Warning < ErrorSeverity::Error);
        assert!(ErrorSeverity::Error < ErrorSeverity::Critical);
        assert!(ErrorSeverity::Critical < ErrorSeverity::Fatal);
    }

    #[test]
    fn test_memory_operation_variants() {
        let operations = [
            MemoryOperation::Allocation,
            MemoryOperation::Deallocation,
            MemoryOperation::Association,
            MemoryOperation::Tracking,
            MemoryOperation::Validation,
        ];

        for op in operations {
            let err = MemScopeError::memory(op, "test");
            assert_eq!(err.category(), "memory");
        }
    }

    #[test]
    fn test_system_error_type_variants() {
        let error_types = [
            SystemErrorType::Io,
            SystemErrorType::Threading,
            SystemErrorType::Locking,
            SystemErrorType::Channel,
            SystemErrorType::Serialization,
            SystemErrorType::Network,
            SystemErrorType::FileSystem,
        ];

        for error_type in error_types {
            let err = MemScopeError::system(error_type, "test");
            assert_eq!(err.category(), "system");
        }
    }

    #[test]
    fn test_error_recovery_strategies() {
        let recovery = DefaultErrorRecovery::new();

        // Test memory error recovery
        let mem_err = MemScopeError::memory(MemoryOperation::Allocation, "test error");
        assert!(recovery.can_recover(&mem_err));
        let action = recovery.get_recovery_action(&mem_err);
        assert!(matches!(action, Some(RecoveryAction::Retry { .. })));

        // Test analysis error recovery
        let analysis_err = MemScopeError::analysis("test", "analysis error");
        assert!(recovery.can_recover(&analysis_err));
        let action = recovery.get_recovery_action(&analysis_err);
        assert!(matches!(action, Some(RecoveryAction::UseDefault { .. })));

        // Test partial export recovery
        let partial_export_err = MemScopeError::export_partial("json", "partial export");
        assert!(recovery.can_recover(&partial_export_err));
        let action = recovery.get_recovery_action(&partial_export_err);
        assert!(matches!(action, Some(RecoveryAction::Skip)));

        // Test full export recovery
        let export_err = MemScopeError::export("json", "export error");
        // Export errors are not recoverable according to is_recoverable() implementation
        assert!(!export_err.is_recoverable());
        assert!(!recovery.can_recover(&export_err)); // Not recoverable
        let action = recovery.get_recovery_action(&export_err);
        assert!(matches!(action, Some(RecoveryAction::Abort)));

        // Test internal error recovery
        let internal_err = MemScopeError::internal("internal error");
        assert!(!recovery.can_recover(&internal_err)); // Not recoverable
        let action = recovery.get_recovery_action(&internal_err);
        assert!(matches!(action, Some(RecoveryAction::Abort)));
    }

    #[test]
    fn test_recovery_execution() {
        let recovery = DefaultErrorRecovery::new();

        // Test retry execution
        let retry_action = RecoveryAction::Retry {
            max_attempts: 1,
            delay_ms: 1, // Minimal delay for testing
        };
        assert!(recovery.execute_recovery(&retry_action).is_ok());

        // Test use default execution
        let default_action = RecoveryAction::UseDefault {
            value: "test".to_string(),
        };
        assert!(recovery.execute_recovery(&default_action).is_ok());

        // Test skip execution
        let skip_action = RecoveryAction::Skip;
        assert!(recovery.execute_recovery(&skip_action).is_ok());

        // Test fallback execution
        let fallback_action = RecoveryAction::Fallback {
            strategy: "test".to_string(),
        };
        assert!(recovery.execute_recovery(&fallback_action).is_ok());

        // Test abort execution
        let abort_action = RecoveryAction::Abort;
        assert!(recovery.execute_recovery(&abort_action).is_err());
    }

    #[test]
    fn test_result_type_alias() {
        fn test_function() -> MemScopeResult<()> {
            Ok(())
        }

        assert!(test_function().is_ok());
    }

    #[test]
    fn test_serde_conversion() {
        let io_err = std::io::Error::other("test io error");
        let json_err = serde_json::Error::io(io_err);
        let memscope_err: MemScopeError = json_err.into();
        assert!(matches!(
            memscope_err,
            MemScopeError::System {
                error_type: SystemErrorType::Serialization,
                ..
            }
        ));
        assert_eq!(memscope_err.category(), "system");
    }
}