Skip to content

Commit 15faf7f

Browse files
committed
reverted and implemented restructure of Comment struct
1 parent 6e69f10 commit 15faf7f

1 file changed

Lines changed: 25 additions & 34 deletions

File tree

src/comments.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,20 @@ impl Default for Span {
8484
}
8585
}
8686

87-
/// Enum for holding the comment content, differentiated by single line `--` and
87+
/// Enum for differentiating comments by single line `--` and
8888
/// multiline `/* */`
8989
#[derive(Clone, Debug, Eq, PartialEq)]
9090
pub enum CommentKind {
9191
/// Enum variant for Multiline Comments
92-
MultiLine(String),
92+
MultiLine,
9393
/// Enum variant for Single Line Comments
94-
SingleLine(String),
95-
}
96-
97-
impl CommentKind {
98-
/// Getter for returning the comment from within the enum
99-
#[must_use]
100-
pub fn comment(&self) -> &str {
101-
match self {
102-
Self::MultiLine(comment) | Self::SingleLine(comment) => comment,
103-
}
104-
}
94+
SingleLine,
10595
}
10696

10797
/// Structure for containing the [`CommentKind`] and the [`Span`] for a comment
10898
#[derive(Clone, Debug, Eq, PartialEq)]
10999
pub struct Comment {
100+
text: String,
110101
kind: CommentKind,
111102
span: Span,
112103
}
@@ -118,8 +109,8 @@ impl Comment {
118109
/// - `kind` where the type of comment is passed as a [`CommentKind`]
119110
/// - `span` where the [`Span`] of the comment is passed
120111
#[must_use]
121-
pub const fn new(kind: CommentKind, span: Span) -> Self {
122-
Self { kind, span }
112+
pub const fn new(text: String, kind: CommentKind, span: Span) -> Self {
113+
Self { text, kind, span }
123114
}
124115

125116
/// Getter method to get the [`CommentKind`]
@@ -138,7 +129,7 @@ impl Comment {
138129
/// regardless of [`CommentKind`]
139130
#[must_use]
140131
pub fn text(&self) -> &str {
141-
self.kind().comment()
132+
&self.text
142133
}
143134
}
144135

@@ -298,8 +289,8 @@ impl Comments {
298289
_ => "\n".to_owned() + line.trim(),
299290
})
300291
.collect();
301-
comments.push(Comment::new(
302-
CommentKind::MultiLine(normalized_comment),
292+
comments.push(Comment::new(normalized_comment,
293+
CommentKind::MultiLine,
303294
Span::new(
304295
Location { line: start_line, column: start_col },
305296
end_loc,
@@ -325,8 +316,8 @@ impl Comments {
325316
if in_single {
326317
in_single = false;
327318
let end_loc = Location::new(line_num, col);
328-
comments.push(Comment::new(
329-
CommentKind::SingleLine(buf.trim().to_owned()),
319+
comments.push(Comment::new(buf.trim().to_owned(),
320+
CommentKind::SingleLine,
330321
Span::new(Location { line: start_line, column: start_col }, end_loc),
331322
));
332323
buf.clear();
@@ -398,11 +389,11 @@ mod tests {
398389
let raw_comment = "-- a comment";
399390
let len = raw_comment.len() as u64;
400391

401-
let singleline = CommentKind::SingleLine(raw_comment.to_owned());
392+
let singleline = CommentKind::SingleLine;
402393
let mut span = Span::default();
403394
span.end.column = len - 1;
404395

405-
let comment = Comment::new(singleline.clone(), span);
396+
let comment = Comment::new(raw_comment.to_owned(), singleline.clone(), span);
406397

407398
assert_eq!(comment.kind, singleline);
408399

@@ -414,10 +405,10 @@ mod tests {
414405

415406
#[test]
416407
fn multiline_comment_span() {
417-
let kind = CommentKind::MultiLine("/* hello world */".to_owned());
408+
let kind = CommentKind::MultiLine;
418409
let span = Span::new(Location { line: 1, column: 1 }, Location { line: 2, column: 9 });
419410

420-
let comment = Comment::new(kind.clone(), span);
411+
let comment = Comment::new("/* hello world */".to_owned(),kind.clone(), span);
421412

422413
assert_eq!(comment.kind, kind);
423414
assert_eq!(comment.span.start.line, 1);
@@ -489,7 +480,7 @@ mod tests {
489480
);
490481

491482
for (i, comment) in comments.iter().enumerate() {
492-
assert_eq!(expected[i], comment.kind().comment(), "comment at index {i} did not match");
483+
assert_eq!(expected[i], comment.text(), "comment at index {i} did not match");
493484
}
494485
}
495486

@@ -683,11 +674,11 @@ CREATE TABLE posts (
683674
let comments = comments.comments();
684675
assert_eq!(comments.len(), 11);
685676
let first = &comments[0];
686-
assert_eq!(first.kind().comment(), "Users table stores user account information");
677+
assert_eq!(first.text(), "Users table stores user account information");
687678
assert_eq!(first.span().start(), &Location::new(1, 1));
688679
assert_eq!(first.span().end(), &Location::new(1, 47));
689680
let primary_key = &comments[1];
690-
assert_eq!(primary_key.kind().comment(), "Primary key");
681+
assert_eq!(primary_key.text(), "Primary key");
691682
assert_eq!(primary_key.span().start(), &Location::new(3, 5));
692683
assert_eq!(primary_key.span().end(), &Location::new(3, 19));
693684
assert!(
@@ -722,7 +713,7 @@ CREATE TABLE posts (
722713
assert_eq!(comments.len(), 11);
723714
let first = &comments[0];
724715
assert_eq!(
725-
first.kind().comment(),
716+
first.text(),
726717
"Users table stores user account information\nmultiline"
727718
);
728719
assert_eq!(first.span().start(), &Location::new(1, 1));
@@ -732,7 +723,7 @@ CREATE TABLE posts (
732723
"end column should be after start column for first multiline comment",
733724
);
734725
let primary_key = &comments[1];
735-
assert_eq!(primary_key.kind().comment(), "Primary key\nmultiline");
726+
assert_eq!(primary_key.text(), "Primary key\nmultiline");
736727
assert_eq!(primary_key.span().start(), &Location::new(4, 5));
737728
assert_eq!(primary_key.span().end().line(), 5);
738729
assert!(
@@ -759,20 +750,20 @@ CREATE TABLE posts (
759750
#[test]
760751
fn test_comments() {
761752
let comment_vec = vec![
762-
Comment::new(
763-
CommentKind::SingleLine("a comment".to_owned()),
753+
Comment::new("a comment".to_owned(),
754+
CommentKind::SingleLine,
764755
Span { start: Location::new(1, 1), end: Location::new(1, 12) },
765756
),
766-
Comment::new(
767-
CommentKind::SingleLine("a second comment".to_owned()),
757+
Comment::new("a second comment".to_owned(),
758+
CommentKind::SingleLine,
768759
Span { start: Location::new(1, 1), end: Location::new(2, 19) },
769760
),
770761
];
771762
let length = comment_vec.len();
772763
let comments = Comments::new(comment_vec.clone());
773764
assert!(comments.comments().len() == length);
774765
for (i, comment) in comments.comments().iter().enumerate() {
775-
assert_eq!(comment.kind().comment(), comment_vec[i].kind().comment());
766+
assert_eq!(comment.text(), comment_vec[i].text());
776767
assert_eq!(comment.span().start(), comment_vec[i].span().start());
777768
assert_eq!(comment.span().end(), comment_vec[i].span().end());
778769
}

0 commit comments

Comments
 (0)