compose_syntax/
fix.rs

1use crate::span::HasSpan;
2use crate::{Patch, PatchEngine, Span};
3use ecow::{EcoString, EcoVec};
4
5#[derive(Debug, Clone, PartialEq, Hash, Eq)]
6pub struct Fix {
7    pub message: EcoString,
8    pub patches: EcoVec<Patch>,
9    pub display: FixDisplay,
10    /// The span of the "pre-patched" source the fix is about
11    pub span: Span,
12}
13
14#[derive(Debug, Clone, PartialEq, Hash, Eq)]
15pub enum FixDisplay {
16    Inline {
17        /// The span to attach the label to
18        span: Span
19    },
20    Footer,
21}
22
23pub struct FixBuilder {
24    engine: PatchEngine,
25    message: EcoString,
26    display: FixDisplay,
27    span: Span,
28}
29
30impl FixBuilder {
31}
32
33impl<'src> FixBuilder {
34    pub fn new(message: impl Into<EcoString>, span: Span) -> Self {
35        FixBuilder {
36            engine: PatchEngine::new(),
37            message: message.into(),
38            display: FixDisplay::Footer,
39            span,
40        }
41    }
42
43    pub fn display(&mut self, display: FixDisplay) -> &mut FixBuilder {
44        self.display = display;
45        self
46    }
47
48    pub fn insert_before(&mut self, node: &impl HasSpan, text: &str) -> &mut FixBuilder {
49        _ =self.engine.insert_before(node, text);
50        self
51    }
52
53    pub fn insert_after(&mut self, node: &impl HasSpan, text: &str) -> &mut FixBuilder {
54        _ = self.engine.insert_after(node, text);
55        self
56    }
57
58    pub fn replace_node(&mut self, node: &impl HasSpan, text: &str) -> &mut FixBuilder {
59        _ = self.engine.replace_node(node, text);
60        self
61    }
62
63    pub fn delete_node(&mut self, node: &impl HasSpan) -> &mut FixBuilder {
64        _ = self.engine.delete_node(node);
65        self
66    }
67
68    pub fn add_patch(&mut self, patch: Patch) -> &mut FixBuilder {
69        self.engine.add_patch(patch);
70        self
71    }
72
73    pub fn build(&self) -> Fix {
74        Fix {
75            message: self.message.clone(),
76            patches: self.engine.get_patches(),
77            display: self.display.clone(),
78            span: self.span,
79        }
80    }
81}