mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-05-24 09:10:49 +00:00
Generalize UsernameArg
This commit is contained in:
parent
5b64d1e535
commit
adc33076f3
3 changed files with 51 additions and 44 deletions
|
@ -1,4 +1,5 @@
|
|||
pub use duration::Duration;
|
||||
pub use username_arg::UsernameArg;
|
||||
|
||||
mod duration {
|
||||
use std::fmt;
|
||||
|
@ -169,3 +170,31 @@ mod duration {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod username_arg {
|
||||
use serenity::model::id::UserId;
|
||||
use std::str::FromStr;
|
||||
/// An argument that can be either a tagged user, or a raw string.
|
||||
pub enum UsernameArg {
|
||||
Tagged(UserId),
|
||||
Raw(String),
|
||||
}
|
||||
|
||||
impl FromStr for UsernameArg {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.parse::<UserId>() {
|
||||
Ok(v) => Ok(UsernameArg::Tagged(v)),
|
||||
Err(_) if !s.is_empty() => Ok(UsernameArg::Raw(s.to_owned())),
|
||||
Err(_) => Err("username arg cannot be empty".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UsernameArg {
|
||||
/// Mention yourself.
|
||||
pub fn mention<T: Into<UserId>>(v: T) -> Self {
|
||||
Self::Tagged(v.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ pub mod reaction_watch;
|
|||
pub mod setup;
|
||||
|
||||
pub use announcer::Announcer;
|
||||
pub use args::Duration;
|
||||
pub use args::{Duration, UsernameArg};
|
||||
pub use pagination::Pagination;
|
||||
pub use reaction_watch::{ReactionHandler, ReactionWatcher};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue