Let's improve previous example a little bit by returning a custom error type instead of a plain string. This will allow us to define specific error types and provide more structured error handling.
The logic of the function remains the same as the previous challenge, but the returned error type is what you need to change.
Define an enum ParsePercentageError with the following variants:
InvalidInput: for inputs that cannot be parsed as numbers.OutOfRange: for numbers that are not in the range 0-100.Implement the Error trait for ParsePercentageError. Use the std::error::Error trait and provide human-readable descriptions for each error.
Update the parse_percentage function to:
Ok(u8) if the input is a valid percentage (between 0 and 100).Err(ParsePercentageError::OutOfRange) if the number is out of range.Err(ParsePercentageError::InvalidInput) if the input is not a valid number.std::error::Error trait requires implementing the Display and Debug traits. You can derive Debug by #[derive(Debug)] and implement Display manually.std::fmt module to implement Display for the error enum, which is required for the Error trait.use std::{error::Error, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input. Enter a number between 0 and 100"), ParsePercentageError::OutOfRange => write!(f, "Out of range input. Enter a number between 0 and 100"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::error::Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let value = input.parse(); match value { Ok(value) => { if value >= 0 && value <= 100 { return Ok(value); } else { return Err(ParsePercentageError::OutOfRange); } } Err(_) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::{Debug, Display, Formatter};// 1. Finish the definition#[derive(Debug)]#[derive(PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result { todo!() }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let n = input.parse::<u8>(); if n.is_ok() { let num = n.unwrap(); if num <= 100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } } else { Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError { }impl std::fmt::Display for ParsePercentageError { fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { Ok(()) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.trim().parse::<i32>() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(n) if n > 100 || n < 0 => Err(ParsePercentageError::OutOfRange), Ok(n) => Ok(n as u8), } }// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error;use std::fmt;#[derive(PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Debug for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { Self::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), Self::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), } }}impl fmt::Display for ParsePercentageError { fn fmt(&self, _: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { Ok(()) }}impl error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let mut target: u8 = 0; for &b in input.as_bytes() { if (48..=57).contains(&b) { target *= 10; target += b - 48; } else { return Err(ParsePercentageError::InvalidInput); } } if target > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(target)}use std::error;use std::fmt;#[derive(PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Debug for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { Self::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), Self::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), } }}impl fmt::Display for ParsePercentageError { fn fmt(&self, _: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { Ok(()) }}impl error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let mut target: u8 = 0; for &b in input.as_bytes() { if (48..=57).contains(&b) { target *= 10; target += b - 48; } else { return Err(ParsePercentageError::InvalidInput); } } if target > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(target)}use std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num = input.parse::<i32>().map_err(|_| ParsePercentageError::InvalidInput)?; if (0..=100).contains(&num) { Ok(num as u8) } else { Err(ParsePercentageError::OutOfRange) }}#[cfg(test)]mod tests { use super::*; #[test] fn test_parse_percentage() { // Test valid input assert_eq!(parse_percentage("75"), Ok(75)); assert_eq!(parse_percentage("0"), Ok(0)); assert_eq!(parse_percentage("100"), Ok(100)); // Test out of range input assert!(matches!(parse_percentage("150"), Err(ParsePercentageError::OutOfRange))); // Test invalid input assert!(matches!(parse_percentage("abc"), Err(ParsePercentageError::InvalidInput))); }}use std::error::Error;use std::fmt::{Display, Formatter};// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(format!("{:?}", self).as_str()) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Ok(parsed) = i32::from_str_radix(input, 10) { return match parsed { 0..=100 => Ok(parsed as u8), _ => Err(ParsePercentageError::OutOfRange), }; } return Err(ParsePercentageError::InvalidInput);}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::{Display, Formatter};// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => f.write_str("InvalidInput"), ParsePercentageError::OutOfRange => f.write_str("OutOfRange"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Ok(parsed) = i32::from_str_radix(input, 10) { return match parsed { 0..=100 => Ok(parsed as u8), _ => Err(ParsePercentageError::OutOfRange), }; } return Err(ParsePercentageError::InvalidInput);}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definitionuse std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), } }}impl Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(i) => if i <= 100 { Ok(i) } else { Err(ParsePercentageError::OutOfRange)}, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(a) => if a <= 100 {Ok(a)} else {Err(ParsePercentageError::OutOfRange)}, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::InvalidInput)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let percentage = input.parse::<u8>().map_err(|_| ParsePercentageError::InvalidInput)?; if percentage <= 100 { Ok(percentage) } else { Err(ParsePercentageError::OutOfRange) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), Self::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) => { if value <= 100 { Ok(value) } else { Err(ParsePercentageError::OutOfRange) } } Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt::Display};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input"), ParsePercentageError::OutOfRange => write!(f, "Out of Range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { if value > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(value) } Err(_) => Err(ParsePercentageError::InvalidInput), }}use std::{ error::Error, fmt::Display,};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Test") }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { if value > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(value) } Err(_) => Err(ParsePercentageError::InvalidInput), }}use std::{error::Error, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}use ParsePercentageError::{InvalidInput, OutOfRange};impl Error for ParsePercentageError { fn description(&self) -> &str { match self { InvalidInput => "Err(ParsePercentageError::InvalidInput)", OutOfRange => "Err(ParsePercentageError::OutOfRange)", } }}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.description()) }}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse() { Ok(n) => { if n > 100 { Err(ParsePercentageError::OutOfRange) }else{ Ok(n) } }, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => { write!(f, "Err(ParsePercentageError::InvalidInput)") } ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Ok(n) => { if n > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(n) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}use ParsePercentageError::{InvalidInput, OutOfRange};impl Error for ParsePercentageError { fn description(&self) -> &str { match self { InvalidInput => "Err(ParsePercentageError::InvalidInput)", OutOfRange => "Err(ParsePercentageError::OutOfRange)", } }}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.description()) }}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let Ok(n): Result<i32, _> = input.parse() else { return Err(ParsePercentageError::InvalidInput); }; if n < 0 || n > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(n as u8)}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::{Display,Result as FmtResult,Formatter};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl Display for ParsePercentageError{ fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match self { Self::InvalidInput => write!(f, "Invalid input: not a valid number"), Self::OutOfRange => write!(f, "Value out of range: must be between 0 and 100"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ Ok(value) => { if (0..=100).contains(&value){ return Ok(value); }else{ return Err(ParsePercentageError::OutOfRange); } }, _ => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num @ 0..=100) => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num > 100 => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), Ok(num) => Ok(num), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input" ), ParsePercentageError::OutOfRange => write!(f, "Percentage must be between 0-100" ) } }}impl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => return Ok(n), Ok(n) if n > 100 => return Err(ParsePercentageError::OutOfRange), _ => return Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input" ), ParsePercentageError::OutOfRange => write!(f, "Percentage must be between 0-100" ) } }}impl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => return Ok(n), Ok(n) if n > 100 => return Err(ParsePercentageError::OutOfRange), _ => return Err(ParsePercentageError::InvalidInput) }}pub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt};#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Invalid Input"), Self::OutOfRange => write!(f, "Percentage out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let value = input.parse::<u8>(); match value { Ok(v) => match v { 0..=100 => Ok(v), _ => Err(ParsePercentageError::OutOfRange), }, _ => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Number is out of range (0-100)"), } }}impl Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt::Display};#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt::Display};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { todo!() }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::u8;use std::fmt;use std::error::Error;// use std::collections::HashMap;// use clap::Parser;// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::OutOfRange=>write!(f, "ParsePercentageError::OutOfRange"), ParsePercentageError::InvalidInput => write!(f,"ParsePercentageError::InvalidInput") } } }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let num = input .parse::<u8>() .map_err(|_| ("invalid input").to_string()); match num { Ok(x) => { if x <= 100 { Ok(x) }else { Err(ParsePercentageError::OutOfRange) } }, Err(_)=> Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::{self, Display};#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid string input."), ParsePercentageError::OutOfRange => write!(f, "The number is out of range."), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(x) => { if x > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(x) } } Err(_) => Err(ParsePercentageError::InvalidInput), }}pub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;// 1. Finish the definition #[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { OutOfRange, InvalidInput}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Input should be between 0 and 100"), ParsePercentageError::InvalidInput => write!(f, "Invalid input") } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { if let Some(num) = input.parse::<u8>().ok() { if num <= 100 { return Ok(num); }; return Err(ParsePercentageError::OutOfRange); } return Err(ParsePercentageError::InvalidInput);}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]// 1. Finish the definitionpub enum ParsePercentageError { InvalidInput, OutOfRange} impl fmt::Display for ParsePercentageError { fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid string input."), ParsePercentageError::OutOfRange => write!(f, "The number is out of range.") } }}impl Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { if value > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(value); }, Err(_) => Err(ParsePercentageError::InvalidInput) } // 3. Implement this function}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageE }use std::fmt;use std::error::Error;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid string input."), ParsePercentageError::OutOfRange => write!(f, "The number is out of range.") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { if value > 100 { return Err(ParsePercentageError::OutOfRange); } return Ok(value); }, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::Display;// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Err({:?})", self) }}// // 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(number) => { if number <= 100 { return Ok(number); } else{ return Err(ParsePercentageError::OutOfRange); } } Err(_) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Err({:?})", self) }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let result = input.parse::<u8>(); match result{ Ok(n) => { if n <= 100 { Ok(n) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Err({:?})", self) }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), _ => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traituse std::error::Error; // 이게 뭐에요?impl std::fmt::Display for ParsePercentageError { fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result{ match self { ParsePercentageError::InvalidInput => write!(f, "input cannot be parsed as number. "), ParsePercentageError::OutOfRange => write!(f, "input is not int the range 0-100. ") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function if input.is_empty() { return Err(ParsePercentageError::InvalidInput); } else { let mut sum: u8 = 0; for c in input.chars() { if let Some(digit) = c.to_digit(10) { sum = 10*sum + (digit as u8); if sum > 100 { return Err(ParsePercentageError::OutOfRange); } } else { return Err(ParsePercentageError::InvalidInput); } } return Ok(sum); }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}// 1. Finish the definition#[derive(Debug, PartialEq, Clone, Copy)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range (0‑100)"), } }}impl std::error::Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let result = input.parse(); if let Err(..) = result { Err(ParsePercentageError::InvalidInput) } else { let parsed_percentage = result.unwrap(); if parsed_percentage > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(parsed_percentage) } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input"), ParsePercentageError::OutOfRange => write!(f, "Out of range (0-100)"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;/// Define the custom error type with two variants#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}/// Implement Display to provide human-readable messagesimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "The input is not a valid number."), ParsePercentageError::OutOfRange => write!(f, "The number is not in the range 0-100."), } }}/// Implement the Error trait to integrate with standard error handlingimpl Error for ParsePercentageError {}/// Parses a string into a u8 percentage with custom error handlingpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.trim().parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{ error::Error, fmt::Formatter};#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid Input."), ParsePercentageError::OutOfRange => write!(f, "Percentage is out of range (0-100)") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let num: u8 = input.parse().map_err(|_| ParsePercentageError::InvalidInput)?; if num <= 100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use core::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}impl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::OutOfRange") } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let number = input.parse::<u8>().map_err(|_| ParsePercentageError::InvalidInput)?; if number > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(number)}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let out = match self { ParsePercentageError::InvalidInput => "Input is not a valid number", ParsePercentageError::OutOfRange => "Number is out of range (0-100)", }; write!(f, "{:?}", out) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Number is out of range (0-100)"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::{error::Error, fmt::Display, num::IntErrorKind};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => writeln!(f, "Input cannot be parsed"), ParsePercentageError::OutOfRange => writeln!(f, "Number not in range, 0 - 100"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) if num > 100 => Err(ParsePercentageError::OutOfRange), Ok(num) => Ok(num), Err(e) if *e.kind() == IntErrorKind::InvalidDigit => Err(ParsePercentageError::InvalidInput), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::{Display, Formatter};// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let output = match *self { ParsePercentageError::InvalidInput => "Input is not a valid number", ParsePercentageError::OutOfRange => "Number is not in the range 0-100" }; write!(f, "{:?}", output) }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt::{Display, Formatter};// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let output = match *self { ParsePercentageError::InvalidInput => "Input is not a valid number", ParsePercentageError::OutOfRange => "Number is not in the range 0-100" }; write!(f, "{:?}", output) }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) => { if value <= 100 { return Ok(value); } return Err(ParsePercentageError::OutOfRange); }, Err(_) => { return Err(ParsePercentageError::InvalidInput); } }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt::{self, Display};use std::error::Error;#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { OutOfRange, InvalidInput}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::OutOfRange => write!(f, "number out of range, should be between 0 and 100"), Self::InvalidInput => write!(f, "invalid input") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(value) if value <=100 => Ok(value), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::fmt::{self, Display};use std::error::Error;#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { OutOfRange, InvalidInput}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "number out of range, should be between 0 and 100"), ParsePercentageError::InvalidInput => write!(f, "invalid input") } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let result = input.parse::<u8>(); match result { Ok(value) => { if value <= 100 { Ok(value) } else { Err(ParsePercentageError::OutOfRange) } } Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input format"), ParsePercentageError::OutOfRange => write!(f, "Input out of range"), } }}impl Error for ParsePercentageError{}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) => { match num <= 100 { true => Ok(num), false => Err(ParsePercentageError::OutOfRange), } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}use std::error::Error;use std::fmt;use std::fmt::{Display,Formatter};// 1. Finish the definition#[derive(Debug,PartialEq,Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input: not a number"), ParsePercentageError::OutOfRange => write!(f, "Out of range: must be 0 to 100"), } }}impl Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(num) => { match num <= 100 { true => Ok(num), false => Err(ParsePercentageError::OutOfRange), } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}