Skip to content

Commit d204f2c

Browse files
committed
🔵 Refactor error result
1 parent 9192d18 commit d204f2c

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

exercises/tiered_pricing/solutions/isaac1024_baby-steps/src/tiered_pricing.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
fn get_unit_price(number_of_subscriptions: u32) -> Result<u32, String> {
1+
use std::fmt;
2+
3+
type SubscriptionResult<T> = Result<T, NumberSubscriptionsError>;
4+
5+
#[derive(Debug)]
6+
struct NumberSubscriptionsError;
7+
8+
impl fmt::Display for NumberSubscriptionsError {
9+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10+
write!(f, "Subscriptions must be greater than 0")
11+
}
12+
}
13+
14+
fn get_unit_price(number_of_subscriptions: u32) -> SubscriptionResult<u32> {
215
const FIRST_UNIT_PRICE: u32 = 299;
316
const SECOND_UNIT_PRICE: u32 = 239;
417
const THIRD_UNIT_PRICE: u32 = 219;
518
const FOURTH_UNIT_PRICE: u32 = 199;
619
const FIFTH_UNIT_PRICE: u32 = 149;
720

821
match number_of_subscriptions {
9-
0 => Err("Error".to_string()),
22+
0 => Err(NumberSubscriptionsError),
1023
1|2 => Ok(FIRST_UNIT_PRICE),
1124
3..=10 => Ok(SECOND_UNIT_PRICE),
1225
11..=25 => Ok(THIRD_UNIT_PRICE),
@@ -15,70 +28,70 @@ fn get_unit_price(number_of_subscriptions: u32) -> Result<u32, String> {
1528
}
1629
}
1730

18-
fn get_total_subscription_price(number_of_subscriptions: u32) -> Result<u32, String> {
31+
fn get_total_subscription_price(number_of_subscriptions: u32) -> SubscriptionResult<u32> {
1932
Ok(number_of_subscriptions * get_unit_price(number_of_subscriptions)?)
2033
}
2134

2235
#[cfg(test)]
2336
mod tests {
24-
use crate::tiered_pricing::get_total_subscription_price;
37+
use crate::tiered_pricing::{get_total_subscription_price, NumberSubscriptionsError};
2538

2639
#[test]
27-
fn when_get_a_subscription_return_299_euros() -> Result<(), String> {
40+
fn when_get_a_subscription_return_299_euros() -> Result<(), NumberSubscriptionsError> {
2841
assert_eq!(299, get_total_subscription_price(1)?);
2942
Ok(())
3043
}
3144

3245
#[test]
33-
fn when_get_2_subscriptions_return_598_euros() -> Result<(), String> {
46+
fn when_get_2_subscriptions_return_598_euros() -> Result<(), NumberSubscriptionsError> {
3447
assert_eq!(598, get_total_subscription_price(2)?);
3548
Ok(())
3649
}
3750

3851
#[test]
39-
fn when_get_3_subscriptions_return_717_euros() -> Result<(), String> {
52+
fn when_get_3_subscriptions_return_717_euros() -> Result<(), NumberSubscriptionsError> {
4053
assert_eq!(717, get_total_subscription_price(3)?);
4154
Ok(())
4255
}
4356

4457
#[test]
45-
fn when_get_10_subscriptions_return_2390_euros() -> Result<(), String> {
58+
fn when_get_10_subscriptions_return_2390_euros() -> Result<(), NumberSubscriptionsError> {
4659
assert_eq!(2390, get_total_subscription_price(10)?);
4760
Ok(())
4861
}
4962

5063
#[test]
51-
fn when_get_11_subscriptions_return_2409_euros() -> Result<(), String> {
64+
fn when_get_11_subscriptions_return_2409_euros() -> Result<(), NumberSubscriptionsError> {
5265
assert_eq!(2409, get_total_subscription_price(11)?);
5366
Ok(())
5467
}
5568

5669
#[test]
57-
fn when_get_25_subscriptions_return_5475_euros() -> Result<(), String> {
70+
fn when_get_25_subscriptions_return_5475_euros() -> Result<(), NumberSubscriptionsError> {
5871
assert_eq!(5475, get_total_subscription_price(25)?);
5972
Ok(())
6073
}
6174

6275
#[test]
63-
fn when_get_26_subscriptions_return_5174_euros() -> Result<(), String> {
76+
fn when_get_26_subscriptions_return_5174_euros() -> Result<(), NumberSubscriptionsError> {
6477
assert_eq!(5174, get_total_subscription_price(26)?);
6578
Ok(())
6679
}
6780

6881
#[test]
69-
fn when_get_50_subscriptions_return_9950_euros() -> Result<(), String> {
82+
fn when_get_50_subscriptions_return_9950_euros() -> Result<(), NumberSubscriptionsError> {
7083
assert_eq!(9950, get_total_subscription_price(50)?);
7184
Ok(())
7285
}
7386

7487
#[test]
75-
fn when_get_51_subscriptions_return_7599_euros() -> Result<(), String> {
88+
fn when_get_51_subscriptions_return_7599_euros() -> Result<(), NumberSubscriptionsError> {
7689
assert_eq!(7599, get_total_subscription_price(51)?);
7790
Ok(())
7891
}
7992

8093
#[test]
81-
fn when_get_52_subscriptions_return_7748_euros() -> Result<(), String> {
94+
fn when_get_52_subscriptions_return_7748_euros() -> Result<(), NumberSubscriptionsError> {
8295
assert_eq!(7748, get_total_subscription_price(52)?);
8396
Ok(())
8497
}

0 commit comments

Comments
 (0)