Implement TryFrom for Custom Enum Conversion
The TryFrom trait in Rust allows for fallible conversions between types. This challenge will test your understanding of implementing this trait for a custom scenario where a conversion might not always succeed. You will be converting a simple integer into a more descriptive enum.
Problem Description
Your task is to implement the std::convert::TryFrom trait for a custom AppError enum. The AppError enum will represent different types of errors that can occur in an application. You need to create a TryFrom implementation that attempts to convert an i32 into an AppError.
Specifically:
- If the input
i32is0, it should be successfully converted toAppError::Success. - If the input
i32is1, it should be successfully converted toAppError::Warning. - If the input
i32is-1, it should be successfully converted toAppError::Critical. - For any other
i32value, the conversion should fail, returning an error. You should define a custom error type for this failure.
Key Requirements:
- Define an
AppErrorenum with variantsSuccess,Warning, andCritical. - Define a custom error type, let's call it
ConversionError, to represent conversion failures. This error should indicate thei32value that caused the failure. - Implement
TryFrom<i32>forAppError. - The
try_frommethod should returnResult<AppError, ConversionError>.
Expected Behavior:
AppError::try_from(0)should returnOk(AppError::Success).AppError::try_from(1)should returnOk(AppError::Warning).AppError::try_from(-1)should returnOk(AppError::Critical).AppError::try_from(5)should returnErr(ConversionError { invalid_value: 5 }).AppError::try_from(-10)should returnErr(ConversionError { invalid_value: -10 }).
Edge Cases:
- Consider negative numbers other than -1.
- Consider large positive numbers.
Examples
Example 1:
Input: 0
Output: Ok(AppError::Success)
Explanation: The integer 0 is a valid input for AppError::Success.
Example 2:
Input: 1
Output: Ok(AppError::Warning)
Explanation: The integer 1 is a valid input for AppError::Warning.
Example 3:
Input: -1
Output: Ok(AppError::Critical)
Explanation: The integer -1 is a valid input for AppError::Critical.
Example 4:
Input: 100
Output: Err(ConversionError { invalid_value: 100 })
Explanation: The integer 100 is not a recognized value for AppError and thus the conversion fails.
Example 5:
Input: -5
Output: Err(ConversionError { invalid_value: -5 })
Explanation: The integer -5 is not a recognized value for AppError and thus the conversion fails.
Constraints
- The input to
try_fromwill always be ani32. - The
AppErrorenum should have exactly three variants:Success,Warning, andCritical. - The
ConversionErrorstruct should have a single field namedinvalid_valueof typei32. - The implementation must use the
std::convert::TryFromtrait.
Notes
- Remember to derive
DebugandPartialEqfor yourAppErrorandConversionErrortypes to facilitate testing. - Think about how to structure your
ConversionErrorto hold the invalid input value. - The
try_frommethod's return type isResult<Self, Self::Error>. You need to specifySelf::Errorcorrectly.