From 8b5a803d44a534f0722d3aa8b675edf29d655e08 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Tue, 9 Jul 2024 13:07:39 +0800 Subject: [PATCH] chore: trait support for plan and recurring interval --- libs/shared-entity/src/dto/billing_dto.rs | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/libs/shared-entity/src/dto/billing_dto.rs b/libs/shared-entity/src/dto/billing_dto.rs index 74c3309f..8d25cc50 100644 --- a/libs/shared-entity/src/dto/billing_dto.rs +++ b/libs/shared-entity/src/dto/billing_dto.rs @@ -16,12 +16,22 @@ impl RecurringInterval { } } +impl TryFrom for RecurringInterval { + type Error = String; + + fn try_from(value: i16) -> Result { + match value { + 0 => Ok(RecurringInterval::Month), + 1 => Ok(RecurringInterval::Year), + _ => Err(format!("Invalid RecurringInterval value: {}", value)), + } + } +} + #[derive(Deserialize, Debug)] #[serde(rename_all = "snake_case")] #[repr(i16)] pub enum SubscriptionPlan { - Unknown = -1, - Free = 0, Pro = 1, Team = 2, @@ -30,15 +40,17 @@ pub enum SubscriptionPlan { AiLocal = 4, } -impl From for SubscriptionPlan { - fn from(value: i16) -> Self { +impl TryFrom for SubscriptionPlan { + type Error = String; + + fn try_from(value: i16) -> Result { match value { - 0 => SubscriptionPlan::Free, - 1 => SubscriptionPlan::Pro, - 2 => SubscriptionPlan::Team, - 3 => SubscriptionPlan::AiMax, - 4 => SubscriptionPlan::AiLocal, - _ => SubscriptionPlan::Unknown, + 0 => Ok(SubscriptionPlan::Free), + 1 => Ok(SubscriptionPlan::Pro), + 2 => Ok(SubscriptionPlan::Team), + 3 => Ok(SubscriptionPlan::AiMax), + 4 => Ok(SubscriptionPlan::AiLocal), + _ => Err(format!("Invalid SubscriptionPlan value: {}", value)), } } } @@ -51,7 +63,6 @@ impl AsRef for SubscriptionPlan { SubscriptionPlan::Team => "team", SubscriptionPlan::AiMax => "ai_max", SubscriptionPlan::AiLocal => "ai_local", - SubscriptionPlan::Unknown => "unknown", } } }