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_losslessrust-analyzer cargo deb822-lossless 0.5.18 crate/::Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
8
9 fn mainrust-analyzer cargo deb822-lossless 0.5.18 main().() {
10 // Example 1: Missing colons
11 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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_recoveryrust-analyzer cargo deb822-lossless 0.5.18 demonstrate_error_recovery().("Missing colon after Maintainer", malformed_input1);
25
26 // Example 2: Orphaned text and missing field names
27 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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_recoveryrust-analyzer cargo deb822-lossless 0.5.18 demonstrate_error_recovery().("Orphaned text and colon", malformed_input2);
36
37 // Example 3: Consecutive field names (missing values)
38 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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_recoveryrust-analyzer cargo deb822-lossless 0.5.18 demonstrate_error_recovery().("Missing values for some fields", malformed_input3);
48
49 // Example 4: Mixed errors across paragraphs
50 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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_recoveryrust-analyzer cargo deb822-lossless 0.5.18 demonstrate_error_recovery().("Mixed errors across paragraphs", malformed_input4);
65
66 // Example 5: Malformed continuation lines
67 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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_recoveryrust-analyzer cargo deb822-lossless 0.5.18 demonstrate_error_recovery().("Malformed continuation lines", malformed_input5);
78 }
79
80 fn demonstrate_error_recoveryrust-analyzer cargo deb822-lossless 0.5.18 demonstrate_error_recovery().(title: &str, input: &str) {
81 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Input ({}):", title);
82 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", input);
83
84 // Parse with error recovery
85 let (deb822, errors) = Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#::from_str_relaxedrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822]from_str_relaxed().(input);
86
87 // Show errors found
88 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().errors.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
89 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Errors found:");
90 for (i, error) in errors.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
91 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" {}: {}", i +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, error);
92 }
93 } else {
94 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("No errors found.");
95 }
96
97 // Show positioned errors if available
98 let parsed = Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#::parserust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822]parse().(input);
99 let positioned_errors = parsed.positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]positioned_errors().();
100 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().positioned_errors.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
101 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nPositioned errors:");
102 for (i, error) in positioned_errors.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
103 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
104 " {}: {} (range: {:?}, code: {:?})",
105 i +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1,
106 error.messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message.,
107 error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range.,
108 error.coderust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#code.
109 );
110
111 // Show the problematic text
112 let start = error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
113 let end = error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
114 if end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() {
115 let error_text = &input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
116 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().error_text.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
117 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" Problematic text: {:?}", error_text);
118 }
119 }
120 }
121 }
122
123 // Show recovered structure
124 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nRecovered structure:");
125 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" Number of paragraphs: {}", deb822.paragraphsrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822]paragraphs().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().());
126
127 // Collect all valid fields from all paragraphs
128 let mut all_fields: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().();
129 for (i, paragraph) in deb822.paragraphsrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822]paragraphs().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
130 let fields: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = paragraph.keysrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Paragraph]keys().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
131 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().fields.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
132 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" Paragraph {}: {} fields", i +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, fields.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
133 for field in &fields {
134 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
135 " {}: {:?}",
136 field,
137 paragraph.getrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Paragraph]get().(field).unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().()
138 );
139 all_fields.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(field.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), paragraph.getrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Paragraph]get().(field).unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().());
140 }
141 } else {
142 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
143 " Paragraph {}: No valid fields (error recovery paragraph)",
144 i +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1
145 );
146 }
147 }
148
149 // Show that we can still work with the recovered data
150 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nUsable extracted data:");
151 for (key, value) in &all_fields {
152 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" {}: {}", key, value);
153 }
154
155 // Demonstrate that the structure is still lossless - we can reconstruct the original
156 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nReconstructed output (preserves original formatting):");
157 let reconstructed = deb822.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
158 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", reconstructed);
159
160 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", "=".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(60));
161 }
162
163 #[cfg(test)]
164 mod testsrust-analyzer cargo deb822-lossless 0.5.18 tests/ {
165 use superrust-analyzer cargo deb822-lossless 0.5.18 crate/::*;
166
167 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
168 fn test_error_recovery_examplesrust-analyzer cargo deb822-lossless 0.5.18 tests/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.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
184 let (deb822, errors) = Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#::from_str_relaxedrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822]from_str_relaxed().(example);
185
186 // Should have errors but still parse something useful
187 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().errors.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().(), "Example {} should have errors", i);
188
189 // Should still extract some valid fields
190 let mut all_fields = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().();
191 for paragraph in deb822.paragraphsrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822]paragraphs().() {
192 for (key, value) in paragraph.itemsrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Paragraph]items().() {
193 all_fields.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(key, value);
194 }
195 }
196
197 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
198 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().all_fields.is_emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]is_empty().(),
199 "Example {} should extract some valid fields",
200 i
201 );
202 }
203 }
204 }