Debian Codegraph @

examples/error_recovery.rs

raw symbols SCIP
1 //! Example demonstrating error recovery in the deb822-lossless parser.
2 //!
3 //! This example shows how the parser can handle malformed deb822 files gracefully,
4 //! continuing to parse valid content even when errors are encountered.
5
6 use deb822_lossless::Deb822;
7 use std::collections::HashMap;
8
9 fn main() {
10 // Example 1: Missing colons
11 println!("=== Example 1: Missing Colons ===");
12 let malformed_input1 = r#"Source: test-package
13 Maintainer John Doe <[email protected]>
14 Section: utils
15 Version: 1.0.0
16
17 Package: test-package
18 Architecture: all
19 Description: A test package
20 This is a multi-line description
21 that spans multiple lines.
22 "#;
23
24 demonstrate_error_recovery("Missing colon after Maintainer", malformed_input1);
25
26 // Example 2: Orphaned text and missing field names
27 println!("\n=== Example 2: Orphaned Text and Missing Field Names ===");
28 let malformed_input2 = r#"Package: my-package
29 some random text without a field name
30 : orphaned colon without field name
31 Version: 2.0.0
32 Description: Valid description
33 "#;
34
35 demonstrate_error_recovery("Orphaned text and colon", malformed_input2);
36
37 // Example 3: Consecutive field names (missing values)
38 println!("\n=== Example 3: Consecutive Field Names ===");
39 let malformed_input3 = r#"Package: another-package
40 Maintainer
41 Priority
42 Section: devel
43 Description: Another package
44 Version: 3.0.0
45 "#;
46
47 demonstrate_error_recovery("Missing values for some fields", malformed_input3);
48
49 // Example 4: Mixed errors across paragraphs
50 println!("\n=== Example 4: Mixed Errors Across Paragraphs ===");
51 let malformed_input4 = r#"Source broken-source
52 Maintainer: Valid Maintainer <[email protected]>
53
54 completely broken paragraph here
55 : orphaned stuff
56 random text everywhere
57
58 Package: recovered-package
59 Architecture: all
60 Depends: some-dep
61 Description: This package was recovered after errors
62 "#;
63
64 demonstrate_error_recovery("Mixed errors across paragraphs", malformed_input4);
65
66 // Example 5: Malformed continuation lines
67 println!("\n=== Example 5: Malformed Continuation Lines ===");
68 let malformed_input5 = r#"Package: continuation-test
69 Description: Short description
70 Proper continuation line
71 invalid continuation without indent
72 Another proper continuation
73 Last proper continuation
74 Version: 1.0
75 "#;
76
77 demonstrate_error_recovery("Malformed continuation lines", malformed_input5);
78 }
79
80 fn demonstrate_error_recovery(title: &str, input: &str) {
81 println!("Input ({}):", title);
82 println!("{}", input);
83
84 // Parse with error recovery
85 let (deb822, errors) = Deb822::from_str_relaxed(input);
86
87 // Show errors found
88 if !errors.is_empty() {
89 println!("Errors found:");
90 for (i, error) in errors.iter().enumerate() {
91 println!(" {}: {}", i + 1, error);
92 }
93 } else {
94 println!("No errors found.");
95 }
96
97 // Show positioned errors if available
98 let parsed = Deb822::parse(input);
99 let positioned_errors = parsed.positioned_errors();
100 if !positioned_errors.is_empty() {
101 println!("\nPositioned errors:");
102 for (i, error) in positioned_errors.iter().enumerate() {
103 println!(
104 " {}: {} (range: {:?}, code: {:?})",
105 i + 1,
106 error.message,
107 error.range,
108 error.code
109 );
110
111 // Show the problematic text
112 let start = error.range.start().into();
113 let end = error.range.end().into();
114 if end <= input.len() {
115 let error_text = &input[start..end];
116 if !error_text.is_empty() {
117 println!(" Problematic text: {:?}", error_text);
118 }
119 }
120 }
121 }
122
123 // Show recovered structure
124 println!("\nRecovered structure:");
125 println!(" Number of paragraphs: {}", deb822.paragraphs().count());
126
127 // Collect all valid fields from all paragraphs
128 let mut all_fields: HashMap<String, String> = HashMap::new();
129 for (i, paragraph) in deb822.paragraphs().enumerate() {
130 let fields: Vec<String> = paragraph.keys().collect();
131 if !fields.is_empty() {
132 println!(" Paragraph {}: {} fields", i + 1, fields.len());
133 for field in &fields {
134 println!(
135 " {}: {:?}",
136 field,
137 paragraph.get(field).unwrap_or_default()
138 );
139 all_fields.insert(field.clone(), paragraph.get(field).unwrap_or_default());
140 }
141 } else {
142 println!(
143 " Paragraph {}: No valid fields (error recovery paragraph)",
144 i + 1
145 );
146 }
147 }
148
149 // Show that we can still work with the recovered data
150 println!("\nUsable extracted data:");
151 for (key, value) in &all_fields {
152 println!(" {}: {}", key, value);
153 }
154
155 // Demonstrate that the structure is still lossless - we can reconstruct the original
156 println!("\nReconstructed output (preserves original formatting):");
157 let reconstructed = deb822.to_string();
158 println!("{}", reconstructed);
159
160 println!("{}", "=".repeat(60));
161 }
162
163 #[cfg(test)]
164 mod tests {
165 use super::*;
166
167 #[test]
168 fn test_error_recovery_examples() {
169 // Test that all examples in main() actually work
170 let examples = [
171 r#"Source: test
172 Maintainer Bad Format
173 Section: utils"#,
174 r#"Package: test
175 orphaned text
176 Version: 1.0"#,
177 r#"Package: test
178 Field1
179 Field2
180 Valid: field"#,
181 ];
182
183 for (i, example) in examples.iter().enumerate() {
184 let (deb822, errors) = Deb822::from_str_relaxed(example);
185
186 // Should have errors but still parse something useful
187 assert!(!errors.is_empty(), "Example {} should have errors", i);
188
189 // Should still extract some valid fields
190 let mut all_fields = HashMap::new();
191 for paragraph in deb822.paragraphs() {
192 for (key, value) in paragraph.items() {
193 all_fields.insert(key, value);
194 }
195 }
196
197 assert!(
198 !all_fields.is_empty(),
199 "Example {} should extract some valid fields",
200 i
201 );
202 }
203 }
204 }