Skip to main content

hedl_lint/fix/
error.rs

1// Dweve HEDL - Hierarchical Entity Data Language
2//
3// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
4//
5// SPDX-License-Identifier: Apache-2.0
6//
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License in the LICENSE file at the
10// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Error types for fix operations
19
20use thiserror::Error;
21
22/// Errors that can occur during fix application
23#[derive(Debug, Clone, Error)]
24pub enum FixError {
25    /// Invalid source range
26    #[error("Invalid source range: {0}")]
27    InvalidRange(String),
28
29    /// Parse failed after applying fix
30    #[error("Parse failed after fix: {0}")]
31    ParseFailure(String),
32
33    /// New violations introduced by fix
34    #[error("New violations introduced: {0}")]
35    NewViolations(String),
36
37    /// Fix dependency not satisfied
38    #[error("Dependency not satisfied: {0}")]
39    DependencyError(String),
40
41    /// Circular dependency detected
42    #[error("Circular dependency detected: {0}")]
43    CircularDependency(String),
44
45    /// Source text encoding error
46    #[error("Source encoding error: {0}")]
47    EncodingError(String),
48
49    /// Fix application failed
50    #[error("Fix application failed: {0}")]
51    ApplicationFailed(String),
52
53    /// Conflict resolution failed
54    #[error("Conflict resolution failed: {0}")]
55    ConflictResolutionFailed(String),
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_error_display() {
64        let err = FixError::InvalidRange("test range".to_string());
65        assert_eq!(err.to_string(), "Invalid source range: test range");
66    }
67
68    #[test]
69    fn test_parse_failure_error() {
70        let err = FixError::ParseFailure("syntax error".to_string());
71        assert!(err.to_string().contains("Parse failed"));
72        assert!(err.to_string().contains("syntax error"));
73    }
74
75    #[test]
76    fn test_new_violations_error() {
77        let err = FixError::NewViolations("rule violation".to_string());
78        assert!(err.to_string().contains("New violations"));
79    }
80
81    #[test]
82    fn test_dependency_error() {
83        let err = FixError::DependencyError("missing dep".to_string());
84        assert!(err.to_string().contains("Dependency not satisfied"));
85    }
86
87    #[test]
88    fn test_circular_dependency_error() {
89        let err = FixError::CircularDependency("A -> B -> A".to_string());
90        assert!(err.to_string().contains("Circular dependency"));
91    }
92
93    #[test]
94    fn test_encoding_error() {
95        let err = FixError::EncodingError("invalid UTF-8".to_string());
96        assert!(err.to_string().contains("encoding"));
97    }
98
99    #[test]
100    fn test_application_failed_error() {
101        let err = FixError::ApplicationFailed("unknown reason".to_string());
102        assert!(err.to_string().contains("application failed"));
103    }
104
105    #[test]
106    fn test_conflict_resolution_failed_error() {
107        let err = FixError::ConflictResolutionFailed("no strategy".to_string());
108        assert!(err.to_string().contains("Conflict resolution failed"));
109    }
110
111    #[test]
112    fn test_error_clone() {
113        let err1 = FixError::InvalidRange("test".to_string());
114        let err2 = err1.clone();
115        assert_eq!(err1.to_string(), err2.to_string());
116    }
117}