|
| 1 | +//! Module for crawling the SQL documents based on the parser and |
| 2 | +//! parsing/extracting the leading comments. |
| 3 | +//! |
| 4 | +//! *leading comment* a comment that |
| 5 | +//! precedes an SQL Statement. |
| 6 | +
|
| 7 | +/// Structure for holding a location in the file. Assumes file is first split by |
| 8 | +/// lines and then split by characters (column) |
| 9 | +pub struct Location { |
| 10 | + line: u64, |
| 11 | + column: u64, |
| 12 | +} |
| 13 | + |
| 14 | +impl Location { |
| 15 | + /// Method for instantiating a new [`Location`] |
| 16 | + /// |
| 17 | + /// # Parameters |
| 18 | + /// - line: the [`u64`] value of the line location |
| 19 | + /// - column: the [`u64`] value of the column location |
| 20 | + #[must_use] |
| 21 | + pub const fn new(line: u64, column: u64) -> Self { |
| 22 | + Self { line, column } |
| 23 | + } |
| 24 | + |
| 25 | + /// Getter method for getting the line value |
| 26 | + #[must_use] |
| 27 | + pub const fn line(&self) -> u64 { |
| 28 | + self.line |
| 29 | + } |
| 30 | + |
| 31 | + /// Getter method for getting the column value |
| 32 | + #[must_use] |
| 33 | + pub const fn column(&self) -> u64 { |
| 34 | + self.column |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// A structure for holding the span of comments found |
| 39 | +pub struct CommentSpan { |
| 40 | + start: Location, |
| 41 | + end: Location, |
| 42 | +} |
| 43 | + |
| 44 | +impl CommentSpan { |
| 45 | + /// Method for creating a new instance of the [`CommentSpan`] for a |
| 46 | + /// comment's span |
| 47 | + /// |
| 48 | + /// # Parameters |
| 49 | + /// - the [`Location`] where the comment starts in the file |
| 50 | + /// - the [`Location`] where the comment ends in the file |
| 51 | + #[must_use] |
| 52 | + pub const fn new(start: Location, end: Location) -> Self { |
| 53 | + Self { start, end } |
| 54 | + } |
| 55 | + |
| 56 | + /// Getter for the start location of a [`CommentSpan`] |
| 57 | + #[must_use] |
| 58 | + pub const fn start(&self) -> &Location { |
| 59 | + &self.start |
| 60 | + } |
| 61 | + |
| 62 | + /// Getter for the end location of a [`CommentSpan`] |
| 63 | + #[must_use] |
| 64 | + pub const fn end(&self) -> &Location { |
| 65 | + &self.end |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Structure that holds the comment along with its location in the file |
| 70 | +pub struct Comment { |
| 71 | + comment: String, |
| 72 | + span: CommentSpan, |
| 73 | +} |
| 74 | + |
| 75 | +impl Comment { |
| 76 | + /// Method for creating a new [`Comment`] from the comment [`String`] and |
| 77 | + /// the [`CommentSpan`] |
| 78 | + /// |
| 79 | + /// # Parameters |
| 80 | + /// - the comment as a [`String`] |
| 81 | + /// - the span of the comment as a [`CommentSpan`] |
| 82 | + #[must_use] |
| 83 | + pub const fn new(comment: String, span: CommentSpan) -> Self { |
| 84 | + Self { comment, span } |
| 85 | + } |
| 86 | + |
| 87 | + /// Getter method for retrieving the comment content |
| 88 | + #[must_use] |
| 89 | + pub fn comment(&self) -> &str { |
| 90 | + &self.comment |
| 91 | + } |
| 92 | + |
| 93 | + /// Getter method for retrieving the [`CommentSpan`] of the comment |
| 94 | + #[must_use] |
| 95 | + pub const fn span(&self) -> &CommentSpan { |
| 96 | + &self.span |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Structure for holding all comments found in the document |
| 101 | +pub struct Comments { |
| 102 | + comments: Vec<Comment>, |
| 103 | +} |
| 104 | + |
| 105 | +impl Comments { |
| 106 | + /// Method for generating a new [`Comments`] struct, which sorts comments |
| 107 | + /// based on their starting span location |
| 108 | + /// |
| 109 | + /// # Parameters |
| 110 | + /// - `comments`: mutable [`Vec<Comment>`] that will be sorted by span start |
| 111 | + #[must_use] |
| 112 | + pub fn new(mut comments: Vec<Comment>) -> Self { |
| 113 | + // Always keep comments ordered by their span |
| 114 | + comments.sort_by(|a, b| { |
| 115 | + let a_start = a.span().start(); |
| 116 | + let b_start = b.span().start(); |
| 117 | + |
| 118 | + a_start |
| 119 | + .line() |
| 120 | + .cmp(&b_start.line()) |
| 121 | + .then_with(|| a_start.column().cmp(&b_start.column())) |
| 122 | + }); |
| 123 | + |
| 124 | + Self { comments } |
| 125 | + } |
| 126 | + |
| 127 | + /// Getter method for retrieving the Vec of [`Comment`] |
| 128 | + #[must_use] |
| 129 | + pub fn comments(&self) -> &[Comment] { |
| 130 | + &self.comments |
| 131 | + } |
| 132 | +} |
0 commit comments