Skip to content

Commit fea5551

Browse files
authored
Merge pull request #3552 from eagletmt/pm-comment-type-t
Make pm_comment_type_t field accessible from Rust wrapper
2 parents 399bf31 + 46c13f9 commit fea5551

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

rust/ruby-prism/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::mem::MaybeUninit;
1919
use std::ptr::NonNull;
2020

2121
pub use self::bindings::*;
22-
use ruby_prism_sys::{pm_comment_t, pm_constant_id_list_t, pm_constant_id_t, pm_diagnostic_t, pm_integer_t, pm_location_t, pm_magic_comment_t, pm_node_destroy, pm_node_list, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t};
22+
use ruby_prism_sys::{pm_comment_t, pm_comment_type_t, pm_constant_id_list_t, pm_constant_id_t, pm_diagnostic_t, pm_integer_t, pm_location_t, pm_magic_comment_t, pm_node_destroy, pm_node_list, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t};
2323

2424
/// A range in the source file.
2525
pub struct Location<'pr> {
@@ -351,6 +351,15 @@ pub struct Comment<'pr> {
351351
marker: PhantomData<&'pr pm_comment_t>,
352352
}
353353

354+
/// The type of the comment
355+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
356+
pub enum CommentType {
357+
/// InlineComment corresponds to comments that start with #.
358+
InlineComment,
359+
/// EmbDocComment corresponds to comments that are surrounded by =begin and =end.
360+
EmbDocComment,
361+
}
362+
354363
impl<'pr> Comment<'pr> {
355364
/// Returns the text of the comment.
356365
///
@@ -361,6 +370,16 @@ impl<'pr> Comment<'pr> {
361370
self.location().as_slice()
362371
}
363372

373+
/// Returns the type of the comment.
374+
pub fn type_(&self) -> CommentType {
375+
let type_ = unsafe { self.comment.as_ref().type_ };
376+
if type_ == pm_comment_type_t::PM_COMMENT_EMBDOC {
377+
CommentType::EmbDocComment
378+
} else {
379+
CommentType::InlineComment
380+
}
381+
}
382+
364383
/// The location of the comment in the source.
365384
#[must_use]
366385
pub const fn location(&self) -> Location<'pr> {
@@ -623,6 +642,7 @@ mod tests {
623642
let result = parse(source.as_ref());
624643

625644
for comment in result.comments() {
645+
assert_eq!(super::CommentType::InlineComment, comment.type_());
626646
let text = std::str::from_utf8(comment.text()).unwrap();
627647
assert!(text.starts_with("# comment"));
628648
}

0 commit comments

Comments
 (0)