From 95d13370c2b796024566ca4763303f6078dfb555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6lfer?= Date: Thu, 2 Oct 2025 21:21:45 +0200 Subject: [PATCH] Added try into for DanceRole --- src/dance_info.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/dance_info.rs b/src/dance_info.rs index 7eba2eb..ec3b869 100644 --- a/src/dance_info.rs +++ b/src/dance_info.rs @@ -21,6 +21,28 @@ impl DanceRole { } } +#[derive(Debug)] +pub struct ParseDanceRoleError; + +impl std::fmt::Display for ParseDanceRoleError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "failed to parse DanceRole") + } +} +impl std::error::Error for ParseDanceRoleError {} + +impl TryFrom<&str> for DanceRole { + type Error = ParseDanceRoleError; + + fn try_from(value: &str) -> Result { + match value.to_lowercase().as_str() { + "leader" => Ok(DanceRole::Leader), + "follower" => Ok(DanceRole::Follower), + _ => Err(ParseDanceRoleError), + } + } +} + #[derive(serde::Deserialize, Debug, PartialEq, Eq)] pub enum DanceRank { Newcomer,