Skip to content

Commit

Permalink
Allow block stdin with url code input
Browse files Browse the repository at this point in the history
  • Loading branch information
Headline committed Sep 22, 2023
1 parent da9dc17 commit a1ed769
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
25 changes: 25 additions & 0 deletions src/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,28 @@ async fn standard_parse_args_one_line() {
assert_eq!(parser_result.url, "");
assert_eq!(parser_result.code, "int main() {return 232;}");
}

#[tokio::test]
async fn parse_url_with_block_stdin() {
let dummy_user = User::default();
let input = indoc::indoc!(
";compile rust < https://pastebin.com/raw/ERqDRZva
```
testing 1 2 3
```"
);

let reply = None;
let result = get_components(input, &dummy_user, None, &reply).await;
if result.is_err() {
panic!("Parser failed.");
}

let parser_result = result.unwrap();
assert_eq!(parser_result.target, "rust");
assert_eq!(parser_result.args.len(), 0);
assert_eq!(parser_result.options.len(), 0);
assert_eq!(parser_result.stdin, "testing 1 2 3\n");
assert_eq!(parser_result.url, "https://pastebin.com/raw/ERqDRZva");
assert_eq!(parser_result.code, "int main() {}");
}
51 changes: 28 additions & 23 deletions src/utls/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ pub async fn get_components(
}
result.args = shell_words::split(&cmdline_args)?;

if !result.url.is_empty() {
let code = get_url_code(&result.url, author).await?;
result.code = code;
} else if find_code_block(&mut result, input, author).await? {
if find_code_block(&mut result, input, author).await? {
if !result.url.is_empty() {
let code = get_url_code(&result.url, author).await?;
result.stdin = result.code;
result.code = code;
}

// If we find a code block from our executor's message, and it's also a reply
// let's assume we found the stdin and what they're replying to is the code.
// Anything else probably doesn't make sense.
Expand All @@ -151,29 +154,31 @@ pub async fn get_components(
result.target = fake_result.target
}
}
} else {
// Unable to parse a code block from our executor's message, lets see if we have a
// reply to grab some code from.
if let Some(replied_msg) = reply {
let attachment = get_message_attachment(&replied_msg.attachments).await?;
if !attachment.0.is_empty() {
if !result.target.is_empty() {
result.target = attachment.1;
}
result.code = attachment.0;
}
// no attachment in the reply, lets check for a code-block..
else if !find_code_block(&mut result, &replied_msg.content, author).await? {
return Err(CommandError::from(
"You must attach a code-block containing code to your message or reply to a message that has one.",
));
} else if !result.url.is_empty() {
let code = get_url_code(&result.url, author).await?;
result.code = code;
}
// Unable to parse a code block from our executor's message, lets see if we have a
// reply to grab some code from.
else if let Some(replied_msg) = reply {
let attachment = get_message_attachment(&replied_msg.attachments).await?;
if !attachment.0.is_empty() {
if !result.target.is_empty() {
result.target = attachment.1;
}
} else {
// We were really given nothing, lets fail now.
result.code = attachment.0;
}
// no attachment in the reply, lets check for a code-block..
else if !find_code_block(&mut result, &replied_msg.content, author).await? {
return Err(CommandError::from(
"You must attach a code-block containing code to your message or quote a message that has one.",
"You must attach a code-block containing code to your message or reply to a message that has one.",
));
}
} else {
// We were really given nothing, lets fail now.
return Err(CommandError::from(
"You must attach a code-block containing code to your message or quote a message that has one.",
));
}

if result.target.is_empty() {
Expand Down

0 comments on commit a1ed769

Please sign in to comment.