Skip to content

Commit f83b29d

Browse files
authored
Merge pull request #1982 from Urgau/note_improvements
Improve `@bot note` parsing and some general note improvements
2 parents d3cb2fb + eff5036 commit f83b29d

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

parser/src/command/note.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,33 @@ impl NoteCommand {
2626
let mut toks = input.clone();
2727
if let Some(Token::Word("note")) = toks.peek_token()? {
2828
toks.next_token()?;
29-
let mut remove = false;
30-
loop {
31-
match toks.next_token()? {
32-
Some(Token::Word(title)) if title == "remove" => {
33-
remove = true;
34-
continue;
35-
}
36-
Some(Token::Word(title)) | Some(Token::Quote(title)) => {
37-
let command = if remove {
38-
NoteCommand::Remove {
39-
title: title.to_string(),
40-
}
41-
} else {
42-
NoteCommand::Summary {
43-
title: title.to_string(),
44-
}
45-
};
46-
break Ok(Some(command));
47-
}
48-
_ => break Err(toks.error(ParseError::MissingTitle)),
49-
};
29+
30+
let remove = if let Some(Token::Word("remove")) = toks.peek_token()? {
31+
toks.next_token()?;
32+
true
33+
} else {
34+
false
35+
};
36+
37+
let title = toks.take_line_until_punc()?.trim();
38+
39+
// For backwards compatibility we also trim " at the start and end
40+
let title = title.trim_matches('"');
41+
42+
if title.is_empty() {
43+
return Err(toks.error(ParseError::MissingTitle));
5044
}
45+
46+
let command = if remove {
47+
NoteCommand::Remove {
48+
title: title.to_string(),
49+
}
50+
} else {
51+
NoteCommand::Summary {
52+
title: title.to_string(),
53+
}
54+
};
55+
Ok(Some(command))
5156
} else {
5257
Ok(None)
5358
}

parser/src/token.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ impl<'a> Tokenizer<'a> {
169169
self.cur_pos()
170170
}
171171

172+
pub fn take_line_until_punc(&mut self) -> Result<&'a str, Error<'a>> {
173+
let start = self.cur_pos();
174+
while self.cur_punct().is_none() && !self.at_end() {
175+
self.advance();
176+
}
177+
Ok(self.str_from(start))
178+
}
179+
172180
pub fn peek_token(&mut self) -> Result<Option<Token<'a>>, Error<'a>> {
173181
self.clone().next_token()
174182
}
@@ -349,3 +357,24 @@ fn tokenize_raw_string_prohibit_1() {
349357
(18, ErrorKind::QuoteInWord)
350358
);
351359
}
360+
361+
#[test]
362+
fn take_line_until_punc() {
363+
assert_eq!(
364+
Tokenizer::new("this is a text. this another one.").take_line_until_punc(),
365+
Ok("this is a text")
366+
);
367+
}
368+
369+
#[test]
370+
fn take_line_until_punc_2() {
371+
assert_eq!(
372+
Tokenizer::new("punc is \nnewline").take_line_until_punc(),
373+
Ok("punc is ")
374+
);
375+
}
376+
377+
#[test]
378+
fn take_line_until_punc_3() {
379+
assert_eq!(Tokenizer::new("").take_line_until_punc(), Ok(""));
380+
}

src/handlers/note.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Users can make a new summary entry by commenting the following:
44
//!
55
//! ```md
6-
//! @rustbot note summary-title
6+
//! @rustbot note Summary title
77
//! ```
88
//!
99
//! If this is the first summary entry, rustbot will amend the original post (the top-level comment) to add a "Notes" section. The section should **not** be edited by hand.
@@ -13,7 +13,7 @@
1313
//!
1414
//! ### Summary Notes
1515
//!
16-
//! - ["summary-title" by @username](link-to-comment)
16+
//! - [Summary title](link-to-comment) by [username](https://github.com/<username>)
1717
//!
1818
//! Generated by triagebot, see [help](https://forge.rust-lang.org/triagebot/note.html) for how to add more
1919
//! <!-- TRIAGEBOT_SUMMARY_END -->
@@ -26,9 +26,9 @@
2626
//!
2727
//! ### Summary Notes
2828
//!
29-
//! - ["first-note" by @username](link-to-comment)
30-
//! - ["second-note" by @username](link-to-comment)
31-
//! - ["summary-title" by @username](link-to-comment)
29+
//! - [First note](link-to-comment) by [username](https://github.com/<username>)
30+
//! - [Second note](link-to-comment) by [username](https://github.com/<username>)
31+
//! - [Summary title](link-to-comment) by [username](https://github.com/<username>)
3232
//!
3333
//! <!-- TRIAGEBOT_SUMMARY_END -->
3434
//! ```
@@ -50,7 +50,7 @@ struct NoteDataEntry {
5050
impl NoteDataEntry {
5151
pub(crate) fn to_markdown(&self) -> String {
5252
format!(
53-
"\n- [\"{title}\" by @{author}]({comment_url})",
53+
"\n- [{title}]({comment_url}) by [{author}](https://github.com/{author})",
5454
title = self.title,
5555
author = self.author,
5656
comment_url = self.comment_url
@@ -74,10 +74,10 @@ struct NoteData {
7474
}
7575

7676
impl NoteData {
77-
pub(crate) fn get_url_from_title(&self, title: &str) -> Option<String> {
77+
pub(crate) fn get_url_from_title_prefix(&self, title: &str) -> Option<String> {
7878
let tmp = self.entries_by_url.clone();
7979
tmp.iter().sorted().find_map(|(key, val)| {
80-
if val.title == title {
80+
if val.title.starts_with(title) {
8181
Some(key.to_owned())
8282
} else {
8383
None
@@ -86,7 +86,7 @@ impl NoteData {
8686
}
8787

8888
pub(crate) fn remove_by_title(&mut self, title: &str) -> Option<NoteDataEntry> {
89-
if let Some(url_to_remove) = self.get_url_from_title(title) {
89+
if let Some(url_to_remove) = self.get_url_from_title_prefix(title) {
9090
if let Some(entry) = self.entries_by_url.remove(&url_to_remove) {
9191
log::debug!("SUCCESSFULLY REMOVED ENTRY: {:#?}", &entry);
9292
Some(entry)

0 commit comments

Comments
 (0)