Skip to content

Commit 09f9c14

Browse files
committed
chore: slightly less code duplication in location mapping
1 parent dfcccf5 commit 09f9c14

File tree

3 files changed

+179
-181
lines changed

3 files changed

+179
-181
lines changed

acdc-parser/src/grammar/location_mapping.rs

Lines changed: 139 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[allow(unused_imports)]
22
use crate::{
33
Bold, CurvedApostrophe, CurvedQuotation, Form, Highlight, InlineNode, Italic, Location,
4-
Monospace, Plain, ProcessedContent, StandaloneCurvedApostrophe, Subscript, Superscript
4+
Monospace, Plain, ProcessedContent, StandaloneCurvedApostrophe, Subscript, Superscript,
55
};
66

77
use super::document::ParserState;
@@ -24,67 +24,147 @@ pub(crate) trait FormattedInline {
2424

2525
// Implementations for all formatted inline types
2626
impl FormattedInline for Bold {
27-
fn location(&self) -> &Location { &self.location }
28-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
29-
fn content(&self) -> &Vec<InlineNode> { &self.content }
30-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
31-
fn form(&self) -> &Form { &self.form }
27+
fn location(&self) -> &Location {
28+
&self.location
29+
}
30+
fn location_mut(&mut self) -> &mut Location {
31+
&mut self.location
32+
}
33+
fn content(&self) -> &Vec<InlineNode> {
34+
&self.content
35+
}
36+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
37+
&mut self.content
38+
}
39+
fn form(&self) -> &Form {
40+
&self.form
41+
}
3242
}
3343

3444
impl FormattedInline for Italic {
35-
fn location(&self) -> &Location { &self.location }
36-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
37-
fn content(&self) -> &Vec<InlineNode> { &self.content }
38-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
39-
fn form(&self) -> &Form { &self.form }
45+
fn location(&self) -> &Location {
46+
&self.location
47+
}
48+
fn location_mut(&mut self) -> &mut Location {
49+
&mut self.location
50+
}
51+
fn content(&self) -> &Vec<InlineNode> {
52+
&self.content
53+
}
54+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
55+
&mut self.content
56+
}
57+
fn form(&self) -> &Form {
58+
&self.form
59+
}
4060
}
4161

4262
impl FormattedInline for Monospace {
43-
fn location(&self) -> &Location { &self.location }
44-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
45-
fn content(&self) -> &Vec<InlineNode> { &self.content }
46-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
47-
fn form(&self) -> &Form { &self.form }
63+
fn location(&self) -> &Location {
64+
&self.location
65+
}
66+
fn location_mut(&mut self) -> &mut Location {
67+
&mut self.location
68+
}
69+
fn content(&self) -> &Vec<InlineNode> {
70+
&self.content
71+
}
72+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
73+
&mut self.content
74+
}
75+
fn form(&self) -> &Form {
76+
&self.form
77+
}
4878
}
4979

5080
impl FormattedInline for Highlight {
51-
fn location(&self) -> &Location { &self.location }
52-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
53-
fn content(&self) -> &Vec<InlineNode> { &self.content }
54-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
55-
fn form(&self) -> &Form { &self.form }
81+
fn location(&self) -> &Location {
82+
&self.location
83+
}
84+
fn location_mut(&mut self) -> &mut Location {
85+
&mut self.location
86+
}
87+
fn content(&self) -> &Vec<InlineNode> {
88+
&self.content
89+
}
90+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
91+
&mut self.content
92+
}
93+
fn form(&self) -> &Form {
94+
&self.form
95+
}
5696
}
5797

5898
impl FormattedInline for Subscript {
59-
fn location(&self) -> &Location { &self.location }
60-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
61-
fn content(&self) -> &Vec<InlineNode> { &self.content }
62-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
63-
fn form(&self) -> &Form { &self.form }
99+
fn location(&self) -> &Location {
100+
&self.location
101+
}
102+
fn location_mut(&mut self) -> &mut Location {
103+
&mut self.location
104+
}
105+
fn content(&self) -> &Vec<InlineNode> {
106+
&self.content
107+
}
108+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
109+
&mut self.content
110+
}
111+
fn form(&self) -> &Form {
112+
&self.form
113+
}
64114
}
65115

66116
impl FormattedInline for Superscript {
67-
fn location(&self) -> &Location { &self.location }
68-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
69-
fn content(&self) -> &Vec<InlineNode> { &self.content }
70-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
71-
fn form(&self) -> &Form { &self.form }
117+
fn location(&self) -> &Location {
118+
&self.location
119+
}
120+
fn location_mut(&mut self) -> &mut Location {
121+
&mut self.location
122+
}
123+
fn content(&self) -> &Vec<InlineNode> {
124+
&self.content
125+
}
126+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
127+
&mut self.content
128+
}
129+
fn form(&self) -> &Form {
130+
&self.form
131+
}
72132
}
73133

74134
impl FormattedInline for CurvedQuotation {
75-
fn location(&self) -> &Location { &self.location }
76-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
77-
fn content(&self) -> &Vec<InlineNode> { &self.content }
78-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
79-
fn form(&self) -> &Form { &self.form }
135+
fn location(&self) -> &Location {
136+
&self.location
137+
}
138+
fn location_mut(&mut self) -> &mut Location {
139+
&mut self.location
140+
}
141+
fn content(&self) -> &Vec<InlineNode> {
142+
&self.content
143+
}
144+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
145+
&mut self.content
146+
}
147+
fn form(&self) -> &Form {
148+
&self.form
149+
}
80150
}
81151

82152
impl FormattedInline for CurvedApostrophe {
83-
fn location(&self) -> &Location { &self.location }
84-
fn location_mut(&mut self) -> &mut Location { &mut self.location }
85-
fn content(&self) -> &Vec<InlineNode> { &self.content }
86-
fn content_mut(&mut self) -> &mut Vec<InlineNode> { &mut self.content }
87-
fn form(&self) -> &Form { &self.form }
153+
fn location(&self) -> &Location {
154+
&self.location
155+
}
156+
fn location_mut(&mut self) -> &mut Location {
157+
&mut self.location
158+
}
159+
fn content(&self) -> &Vec<InlineNode> {
160+
&self.content
161+
}
162+
fn content_mut(&mut self) -> &mut Vec<InlineNode> {
163+
&mut self.content
164+
}
165+
fn form(&self) -> &Form {
166+
&self.form
167+
}
88168
}
89169

90170
/// Generic function for mapping formatted inline locations with form-awareness
@@ -317,85 +397,20 @@ pub(crate) fn map_inner_content_locations(
317397
extend_attribute_location_if_needed(state, processed, mapped);
318398
InlineNode::PlainText(inner_plain)
319399
}
320-
InlineNode::ItalicText(italic_text) => {
321-
// Handle nested italic text by recursively mapping its locations
322-
let mapping_ctx = LocationMappingContext {
323-
state,
324-
processed,
325-
base_location,
326-
};
327-
let mapped = map_formatted_inline_locations(italic_text, &mapping_ctx);
328-
InlineNode::ItalicText(mapped)
329-
}
330-
InlineNode::BoldText(bold_text) => {
331-
// Handle nested bold text by recursively mapping its locations
332-
let mapping_ctx = LocationMappingContext {
333-
state,
334-
processed,
335-
base_location,
336-
};
337-
let mapped = map_formatted_inline_locations(bold_text, &mapping_ctx);
338-
InlineNode::BoldText(mapped)
339-
}
340-
InlineNode::MonospaceText(monospace_text) => {
341-
// Handle nested monospace text by recursively mapping its locations
342-
let mapping_ctx = LocationMappingContext {
343-
state,
344-
processed,
345-
base_location,
346-
};
347-
let mapped = map_formatted_inline_locations(monospace_text, &mapping_ctx);
348-
InlineNode::MonospaceText(mapped)
349-
}
350-
InlineNode::HighlightText(highlight_text) => {
351-
// Handle nested highlight text by recursively mapping its locations
352-
let mapping_ctx = LocationMappingContext {
353-
state,
354-
processed,
355-
base_location,
356-
};
357-
let mapped = map_formatted_inline_locations(highlight_text, &mapping_ctx);
358-
InlineNode::HighlightText(mapped)
359-
}
360-
InlineNode::SubscriptText(subscript_text) => {
361-
// Handle nested subscript text by recursively mapping its locations
362-
let mapping_ctx = LocationMappingContext {
363-
state,
364-
processed,
365-
base_location,
366-
};
367-
let mapped = map_formatted_inline_locations(subscript_text, &mapping_ctx);
368-
InlineNode::SubscriptText(mapped)
369-
}
370-
InlineNode::SuperscriptText(superscript_text) => {
371-
// Handle nested superscript text by recursively mapping its locations
400+
marked_text @ (InlineNode::ItalicText(_)
401+
| InlineNode::BoldText(_)
402+
| InlineNode::MonospaceText(_)
403+
| InlineNode::HighlightText(_)
404+
| InlineNode::SubscriptText(_)
405+
| InlineNode::SuperscriptText(_)
406+
| InlineNode::CurvedQuotationText(_)
407+
| InlineNode::CurvedApostropheText(_)) => {
372408
let mapping_ctx = LocationMappingContext {
373409
state,
374410
processed,
375411
base_location,
376412
};
377-
let mapped = map_formatted_inline_locations(superscript_text, &mapping_ctx);
378-
InlineNode::SuperscriptText(mapped)
379-
}
380-
InlineNode::CurvedQuotationText(curved_quotation_text) => {
381-
// Handle nested curved quotation text by recursively mapping its locations
382-
let mapping_ctx = LocationMappingContext {
383-
state,
384-
processed,
385-
base_location,
386-
};
387-
let mapped = map_formatted_inline_locations(curved_quotation_text, &mapping_ctx);
388-
InlineNode::CurvedQuotationText(mapped)
389-
}
390-
InlineNode::CurvedApostropheText(curved_apostrophe_text) => {
391-
// Handle nested curved apostrophe text by recursively mapping its locations
392-
let mapping_ctx = LocationMappingContext {
393-
state,
394-
processed,
395-
base_location,
396-
};
397-
let mapped = map_formatted_inline_locations(curved_apostrophe_text, &mapping_ctx);
398-
InlineNode::CurvedApostropheText(mapped)
413+
marked_text.map_formatted_inline_locations(&mapping_ctx)
399414
}
400415
other => other,
401416
})
@@ -588,77 +603,20 @@ pub(crate) fn map_inline_locations(
588603
})]
589604
}
590605
}
591-
InlineNode::ItalicText(italic_text) => {
592-
let mapping_ctx = LocationMappingContext {
593-
state,
594-
processed,
595-
base_location: location,
596-
};
597-
let mapped = map_formatted_inline_locations(italic_text.clone(), &mapping_ctx);
598-
vec![InlineNode::ItalicText(mapped)]
599-
}
600-
InlineNode::BoldText(bold_text) => {
601-
let mapping_ctx = LocationMappingContext {
602-
state,
603-
processed,
604-
base_location: location,
605-
};
606-
let mapped = map_formatted_inline_locations(bold_text.clone(), &mapping_ctx);
607-
vec![InlineNode::BoldText(mapped)]
608-
}
609-
InlineNode::MonospaceText(monospace_text) => {
610-
let mapping_ctx = LocationMappingContext {
611-
state,
612-
processed,
613-
base_location: location,
614-
};
615-
let mapped = map_formatted_inline_locations(monospace_text.clone(), &mapping_ctx);
616-
vec![InlineNode::MonospaceText(mapped)]
617-
}
618-
InlineNode::HighlightText(highlight_text) => {
619-
let mapping_ctx = LocationMappingContext {
620-
state,
621-
processed,
622-
base_location: location,
623-
};
624-
let mapped = map_formatted_inline_locations(highlight_text.clone(), &mapping_ctx);
625-
vec![InlineNode::HighlightText(mapped)]
626-
}
627-
InlineNode::SubscriptText(subscript_text) => {
628-
let mapping_ctx = LocationMappingContext {
629-
state,
630-
processed,
631-
base_location: location,
632-
};
633-
let mapped = map_formatted_inline_locations(subscript_text.clone(), &mapping_ctx);
634-
vec![InlineNode::SubscriptText(mapped)]
635-
}
636-
InlineNode::SuperscriptText(superscript_text) => {
637-
let mapping_ctx = LocationMappingContext {
638-
state,
639-
processed,
640-
base_location: location,
641-
};
642-
let mapped = map_formatted_inline_locations(superscript_text.clone(), &mapping_ctx);
643-
vec![InlineNode::SuperscriptText(mapped)]
644-
}
645-
InlineNode::CurvedQuotationText(curved_quotation_text) => {
646-
let mapping_ctx = LocationMappingContext {
647-
state,
648-
processed,
649-
base_location: location,
650-
};
651-
let mapped = map_formatted_inline_locations(curved_quotation_text.clone(), &mapping_ctx);
652-
vec![InlineNode::CurvedQuotationText(mapped)]
653-
}
654-
InlineNode::CurvedApostropheText(curved_apostrophe_text) => {
606+
marked_text @ (InlineNode::ItalicText(_)
607+
| InlineNode::BoldText(_)
608+
| InlineNode::MonospaceText(_)
609+
| InlineNode::HighlightText(_)
610+
| InlineNode::SubscriptText(_)
611+
| InlineNode::SuperscriptText(_)
612+
| InlineNode::CurvedQuotationText(_)
613+
| InlineNode::CurvedApostropheText(_)) => {
655614
let mapping_ctx = LocationMappingContext {
656615
state,
657616
processed,
658617
base_location: location,
659618
};
660-
let mapped = map_formatted_inline_locations(curved_apostrophe_text.clone(), &mapping_ctx);
661-
vec![InlineNode::CurvedApostropheText(mapped)]
619+
vec![marked_text.clone().map_formatted_inline_locations(&mapping_ctx)]
662620
}
663621
InlineNode::StandaloneCurvedApostrophe(standalone) => {
664622
let mut mapped_standalone = standalone.clone();

acdc-parser/src/grammar/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ pub(crate) use document::{ParserState, document_parser};
1111
pub(crate) use inline_preprocessor::{
1212
InlinePreprocessorParserState, ProcessedContent, inline_preprocessing,
1313
};
14+
pub(crate) use location_mapping::{LocationMappingContext, map_formatted_inline_locations};
1415
pub(crate) use position_tracker::{LineMap, PositionTracker};

0 commit comments

Comments
 (0)