| 1 | //! `/profile` command. |
| 2 | |
| 3 | use codewhale_command_contract::handler::CommandHandler; |
| 4 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 5 | use codewhale_command_contract::outcome::{CommandResult, CoreAction}; |
| 6 | |
| 7 | const COMMAND_INFO: CommandInfo = CommandInfo { |
| 8 | name: "profile", |
| 9 | aliases: &["dangan"], |
| 10 | usage: "/profile <name>", |
| 11 | description_key: "cmd_profile_description", |
| 12 | }; |
| 13 | |
| 14 | pub struct ProfileCmd; |
| 15 | |
| 16 | impl<A: From<CoreAction>> RegisterCommand<CommandResult<A>> for ProfileCmd { |
| 17 | fn info() -> &'static CommandInfo { |
| 18 | &COMMAND_INFO |
| 19 | } |
| 20 | |
| 21 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 22 | CommandHandler::Contextual { |
| 23 | capabilities: codewhale_command_contract::handler::CommandCapabilities::PRESENTATION, |
| 24 | handler: |contexts, args| { |
| 25 | let Some(presentation) = contexts.into_parts().presentation else { |
| 26 | return CommandResult::error("Command capability unavailable: presentation"); |
| 27 | }; |
| 28 | let Some(profile) = args.map(str::trim).filter(|name| !name.is_empty()) else { |
| 29 | return CommandResult::error( |
| 30 | presentation |
| 31 | .translate("profile_usage", &[]) |
| 32 | .unwrap_or_else(|err| err), |
| 33 | ); |
| 34 | }; |
| 35 | match presentation.translate("profile_switching", &[("name", profile)]) { |
| 36 | Ok(message) => CommandResult::with_message_and_action( |
| 37 | message, |
| 38 | CoreAction::SwitchProfile { |
| 39 | profile: profile.to_owned(), |
| 40 | } |
| 41 | .into(), |
| 42 | ), |
| 43 | Err(error) => CommandResult::error(error), |
| 44 | } |
| 45 | }, |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 |