Skip to content

Commit

Permalink
make commit_if_ok a macro instead
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jun 12, 2024
1 parent 8a05968 commit a873acf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
37 changes: 10 additions & 27 deletions assyst-core/src/command/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use twilight_model::id::Id;
use twilight_util::builder::command::{AttachmentBuilder, IntegerBuilder, NumberBuilder, StringBuilder};

use crate::assyst::Assyst;
use crate::commit_if_ok;
use crate::downloader::{self, ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES};
use crate::gateway_handler::message_parser::error::{ErrorSeverity, GetErrorSeverity};

Expand Down Expand Up @@ -502,21 +503,12 @@ impl ParseArgument for ImageUrl {
};
}

handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_mention_raw_message(v).await)
.await
);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_url_argument_raw_message(v).await)
.await
);
handle!(ctxt.commit_if_ok(async |v| ImageUrl::from_attachment(v).await).await);
handle!(ctxt.commit_if_ok(async |v| ImageUrl::from_reply(v).await).await);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_emoji_raw_message(v).await)
.await
);
handle!(ctxt.commit_if_ok(async |v| ImageUrl::from_sticker(v).await).await);
handle!(commit_if_ok!(ctxt, ImageUrl::from_mention_raw_message));
handle!(commit_if_ok!(ctxt, ImageUrl::from_url_argument_raw_message));
handle!(commit_if_ok!(ctxt, ImageUrl::from_attachment));
handle!(commit_if_ok!(ctxt, ImageUrl::from_reply));
handle!(commit_if_ok!(ctxt, ImageUrl::from_emoji_raw_message));
handle!(commit_if_ok!(ctxt, ImageUrl::from_sticker));
handle!(ImageUrl::from_channel_history(ctxt.cx.assyst(), ctxt.cx.data.message.channel_id).await);
Err(TagParseError::NoImageFound)
}
Expand Down Expand Up @@ -547,18 +539,9 @@ impl ParseArgument for ImageUrl {
};
}

handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_mention_command_option(v).await)
.await
);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_url_argument_command_option(v).await)
.await
);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_emoji_command_option(v).await)
.await
);
handle!(commit_if_ok!(ctxt, ImageUrl::from_mention_command_option));
handle!(commit_if_ok!(ctxt, ImageUrl::from_url_argument_command_option));
handle!(commit_if_ok!(ctxt, ImageUrl::from_emoji_command_option));
handle!(ImageUrl::from_channel_history(ctxt.cx.assyst(), ctxt.cx.data.message.channel_id).await);
Err(TagParseError::NoImageFound)
}
Expand Down
30 changes: 19 additions & 11 deletions assyst-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,27 @@ impl<'a, T: Clone> ParseCtxt<'a, T> {
args: self.args.clone(),
}
}
}

/// Calls the function with a fork of this context (allowing some arbitrary mutations)
/// and only actually applies the changes made to the fork if it returns `Ok`.
pub async fn commit_if_ok<F, R, E>(&mut self, f: F) -> Result<R, E>
where
F: async FnOnce(&mut ParseCtxt<'a, T>) -> Result<R, E>,
{
let mut fork: ParseCtxt<'a, T> = self.fork();
let res = f(&mut fork).await?;
*self = fork;
Ok(res)
}
/// Calls the function with a fork of this context (allowing some arbitrary mutations)
/// and only actually applies the changes made to the fork if it returns `Ok`.
///
/// This used to be a function, however due to compiler bugs and the inability to properly express
/// this pattern with bounds, this was ultimately just made into a macro where no such bounds need
/// to be specified.
#[macro_export]
macro_rules! commit_if_ok {
($ctxt:expr, $f:expr) => {{
let ctxt: &mut crate::command::ParseCtxt<'_, _> = $ctxt;
let mut fork = ctxt.fork();
let res = ($f)(&mut fork).await;
if res.is_ok() {
*ctxt = fork;
}
res
}};
}

impl<'a> ParseCtxt<'a, RawMessageArgsIter<'a>> {
pub fn new(ctxt: CommandCtxt<'a>, args: &'a str) -> Self {
Self {
Expand Down

0 comments on commit a873acf

Please sign in to comment.