Skip to content

Commit

Permalink
unconditionally consume args in exec and eval
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Jul 3, 2024
1 parent f349b73 commit 017b112
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
42 changes: 42 additions & 0 deletions assyst-core/src/command/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,48 @@ impl ParseArgument for Rest {
}
}

/// The rest of a message as an argument, and asserts that there is no following flag argument. This
/// should be the last argument if used.
#[derive(Debug)]
pub struct RestNoFlags(pub String);

impl ParseArgument for RestNoFlags {
async fn parse_raw_message(ctxt: &mut RawMessageParseCtxt<'_>, label: Label) -> Result<Self, TagParseError> {
if let Some(m) = ctxt.cx.data.message {
if let Some(ref r) = m.referenced_message {
Ok(Self(r.content.clone()))
} else {
Ok(Self(ctxt.rest_all(label)))
}
} else {
Ok(Self(ctxt.rest_all(label)))
}
}

async fn parse_command_option(ctxt: &mut InteractionCommandParseCtxt<'_>) -> Result<Self, TagParseError> {
// treat Rest as same as Word because there is no option type which is just one whitespace-delimited
// word
let word = ctxt.next_option()?;

if let CommandOptionValue::String(ref option) = word.value {
Ok(RestNoFlags(option.clone()))
} else {
Err(TagParseError::MismatchedCommandOptionType((
"String (Rest)".to_owned(),
word.value.clone(),
)))
}
}

fn as_command_option(name: &str) -> CommandOption {
StringBuilder::new(name, "text input").required(true).build()
}

fn usage(name: &str) -> String {
format!("<...{name}>")
}
}

pub struct ImageUrl(pub String);

impl ImageUrl {
Expand Down
6 changes: 3 additions & 3 deletions assyst-core/src/command/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
use crate::command::Availability;
use crate::rest::eval::fake_eval;

use super::arguments::{Image, ImageUrl, Rest, Word};
use super::arguments::{Image, ImageUrl, Rest, RestNoFlags, Word};
use super::{Category, CommandCtxt};

use anyhow::Context;
Expand Down Expand Up @@ -89,7 +89,7 @@ pub async fn ping(ctxt: CommandCtxt<'_>) -> anyhow::Result<()> {
usage = "[script]",
examples = ["rm -rf /*"]
)]
pub async fn exec(ctxt: CommandCtxt<'_>, script: Rest) -> anyhow::Result<()> {
pub async fn exec(ctxt: CommandCtxt<'_>, script: RestNoFlags) -> anyhow::Result<()> {
let result = exec_sync(&script.0)?;

let mut output = "".to_owned();
Expand All @@ -113,7 +113,7 @@ pub async fn exec(ctxt: CommandCtxt<'_>, script: Rest) -> anyhow::Result<()> {
usage = "[script]",
examples = ["1"]
)]
pub async fn eval(ctxt: CommandCtxt<'_>, script: Rest) -> anyhow::Result<()> {
pub async fn eval(ctxt: CommandCtxt<'_>, script: RestNoFlags) -> anyhow::Result<()> {
let result = fake_eval(ctxt.assyst(), script.0, true, ctxt.data.message, Vec::new())
.await
.context("Evaluation failed")?;
Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn main() {
assyst.register_task(Task::new_delayed(
assyst.clone(),
Duration::from_secs(60 * 10),
Duration::from_secs(30),
Duration::from_secs(60 * 10),
function_task_callback!(refresh_web_download_urls),
));
info!("Registered web download url refreshing task");
Expand Down

0 comments on commit 017b112

Please sign in to comment.