|
1 |
| -use std::collections::HashSet; |
2 |
| -use std::rc::Rc; |
3 |
| - |
4 |
| -use dockerfile_parser::Dockerfile; |
5 |
| -use dockerfile_parser::Span; |
6 |
| - |
7 |
| -use super::helpers::parse_comments; |
8 |
| -use super::helpers::Node; |
9 |
| -use crate::configuration::Configuration; |
10 |
| - |
11 |
| -pub struct Context<'a> { |
12 |
| - pub config: &'a Configuration, |
13 |
| - pub dockerfile: &'a Dockerfile, |
14 |
| - pub text: &'a str, |
15 |
| - pub handled_comments: HashSet<usize>, |
16 |
| - current_node: Option<Node<'a>>, |
17 |
| - parent_stack: Vec<Node<'a>>, |
18 |
| - pub parse_string_content: bool, |
19 |
| -} |
20 |
| - |
21 |
| -impl<'a> Context<'a> { |
22 |
| - pub fn new(text: &'a str, dockerfile: &'a Dockerfile, config: &'a Configuration) -> Self { |
23 |
| - Self { |
24 |
| - config, |
25 |
| - text, |
26 |
| - dockerfile, |
27 |
| - handled_comments: HashSet::new(), |
28 |
| - current_node: None, |
29 |
| - parent_stack: Vec::new(), |
30 |
| - parse_string_content: false, |
31 |
| - } |
32 |
| - } |
33 |
| - |
34 |
| - pub fn span_text(&self, span: &Span) -> &str { |
35 |
| - &self.text[span.start..span.end] |
36 |
| - } |
37 |
| - |
38 |
| - pub fn set_current_node(&mut self, node: Node<'a>) { |
39 |
| - if let Some(parent) = self.current_node.take() { |
40 |
| - self.parent_stack.push(parent); |
41 |
| - } |
42 |
| - self.current_node = Some(node); |
43 |
| - } |
44 |
| - |
45 |
| - pub fn pop_current_node(&mut self) { |
46 |
| - self.current_node = self.parent_stack.pop(); |
47 |
| - } |
48 |
| - |
49 |
| - pub fn parent(&self) -> Option<&Node<'a>> { |
50 |
| - self.parent_stack.last() |
51 |
| - } |
52 |
| - |
53 |
| - pub fn parse_nodes_with_comments(&mut self, start_pos: usize, end_pos: usize, nodes: impl Iterator<Item = Node<'a>>) -> Vec<Node<'a>> { |
54 |
| - let mut result = Vec::new(); |
55 |
| - let mut last_pos = start_pos; |
56 |
| - for node in nodes { |
57 |
| - let text = &self.text[last_pos..node.span().start]; |
58 |
| - for comment in parse_comments(text, last_pos) { |
59 |
| - result.push(Node::CommentRc(Rc::new(comment))); |
60 |
| - } |
61 |
| - let node_end = node.span().end; |
62 |
| - result.push(node); |
63 |
| - last_pos = node_end; |
64 |
| - } |
65 |
| - let text = &self.text[last_pos..end_pos]; |
66 |
| - for comment in parse_comments(text, last_pos) { |
67 |
| - result.push(Node::CommentRc(Rc::new(comment))); |
68 |
| - } |
69 |
| - result |
70 |
| - } |
71 |
| -} |
| 1 | +use std::collections::HashSet; |
| 2 | +use std::rc::Rc; |
| 3 | + |
| 4 | +use dockerfile_parser::Dockerfile; |
| 5 | +use dockerfile_parser::Span; |
| 6 | + |
| 7 | +use super::helpers::parse_comments; |
| 8 | +use super::helpers::Node; |
| 9 | +use crate::configuration::Configuration; |
| 10 | + |
| 11 | +pub struct Context<'a> { |
| 12 | + pub config: &'a Configuration, |
| 13 | + pub dockerfile: &'a Dockerfile, |
| 14 | + pub text: &'a str, |
| 15 | + pub handled_comments: HashSet<usize>, |
| 16 | + current_node: Option<Node<'a>>, |
| 17 | + parent_stack: Vec<Node<'a>>, |
| 18 | + pub parse_string_content: bool, |
| 19 | +} |
| 20 | + |
| 21 | +impl<'a> Context<'a> { |
| 22 | + pub fn new(text: &'a str, dockerfile: &'a Dockerfile, config: &'a Configuration) -> Self { |
| 23 | + Self { |
| 24 | + config, |
| 25 | + text, |
| 26 | + dockerfile, |
| 27 | + handled_comments: HashSet::new(), |
| 28 | + current_node: None, |
| 29 | + parent_stack: Vec::new(), |
| 30 | + parse_string_content: false, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + pub fn span_text(&self, span: &Span) -> &str { |
| 35 | + &self.text[span.start..span.end] |
| 36 | + } |
| 37 | + |
| 38 | + pub fn set_current_node(&mut self, node: Node<'a>) { |
| 39 | + if let Some(parent) = self.current_node.take() { |
| 40 | + self.parent_stack.push(parent); |
| 41 | + } |
| 42 | + self.current_node = Some(node); |
| 43 | + } |
| 44 | + |
| 45 | + pub fn pop_current_node(&mut self) { |
| 46 | + self.current_node = self.parent_stack.pop(); |
| 47 | + } |
| 48 | + |
| 49 | + pub fn parent(&self) -> Option<&Node<'a>> { |
| 50 | + self.parent_stack.last() |
| 51 | + } |
| 52 | + |
| 53 | + pub fn parse_nodes_with_comments(&mut self, start_pos: usize, end_pos: usize, nodes: impl Iterator<Item = Node<'a>>) -> Vec<Node<'a>> { |
| 54 | + let mut result = Vec::new(); |
| 55 | + let mut last_pos = start_pos; |
| 56 | + for node in nodes { |
| 57 | + let text = &self.text[last_pos..node.span().start]; |
| 58 | + for comment in parse_comments(text, last_pos) { |
| 59 | + result.push(Node::CommentRc(Rc::new(comment))); |
| 60 | + } |
| 61 | + let node_end = node.span().end; |
| 62 | + result.push(node); |
| 63 | + last_pos = node_end; |
| 64 | + } |
| 65 | + let text = &self.text[last_pos..end_pos]; |
| 66 | + for comment in parse_comments(text, last_pos) { |
| 67 | + result.push(Node::CommentRc(Rc::new(comment))); |
| 68 | + } |
| 69 | + result |
| 70 | + } |
| 71 | +} |
0 commit comments