-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add poise::StrArg for FromStr arguments
- Loading branch information
Showing
4 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::{ | ||
serenity_prelude as serenity, PopArgument, PopArgumentResult, SlashArgError, SlashArgument, | ||
}; | ||
|
||
/// A wrapper for `T` to implement [`SlashArgument`] and [`PopArgument`] via [`FromStr`]. | ||
/// | ||
/// This is useful if you need to take an argument via a string, but immediately convert it via [`FromStr`]. | ||
pub struct StrArg<T>(pub T); | ||
|
||
#[async_trait::async_trait] | ||
impl<T> SlashArgument for StrArg<T> | ||
where | ||
T: FromStr, | ||
T::Err: std::error::Error + Send + Sync + 'static, | ||
{ | ||
fn create(builder: serenity::CreateCommandOption) -> serenity::CreateCommandOption { | ||
builder.kind(serenity::CommandOptionType::String) | ||
} | ||
|
||
async fn extract( | ||
_: &serenity::Context, | ||
_: &serenity::CommandInteraction, | ||
value: &serenity::ResolvedValue<'_>, | ||
) -> Result<Self, SlashArgError> { | ||
let serenity::ResolvedValue::String(value) = value else { | ||
return Err(SlashArgError::new_command_structure_mismatch( | ||
"expected a String", | ||
)); | ||
}; | ||
|
||
match T::from_str(value) { | ||
Ok(value) => Ok(Self(value)), | ||
Err(err) => Err(SlashArgError::Parse { | ||
error: err.into(), | ||
input: String::from(*value), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<'a, T> PopArgument<'a> for StrArg<T> | ||
where | ||
T: FromStr, | ||
T::Err: std::error::Error + Send + Sync + 'static, | ||
{ | ||
async fn pop_from( | ||
args: &'a str, | ||
attachment_index: usize, | ||
ctx: &serenity::Context, | ||
msg: &serenity::Message, | ||
) -> PopArgumentResult<'a, Self> { | ||
let (args, attach_idx, value) = String::pop_from(args, attachment_index, ctx, msg).await?; | ||
match T::from_str(&value) { | ||
Ok(value) => Ok((args, attach_idx, Self(value))), | ||
Err(err) => Err((Box::new(err), Some(value))), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters