Hone logo
Hone
Problems

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 i32 is 0, it should be successfully converted to AppError::Success.
  • If the input i32 is 1, it should be successfully converted to AppError::Warning.
  • If the input i32 is -1, it should be successfully converted to AppError::Critical.
  • For any other i32 value, the conversion should fail, returning an error. You should define a custom error type for this failure.

Key Requirements:

  • Define an AppError enum with variants Success, Warning, and Critical.
  • Define a custom error type, let's call it ConversionError, to represent conversion failures. This error should indicate the i32 value that caused the failure.
  • Implement TryFrom<i32> for AppError.
  • The try_from method should return Result<AppError, ConversionError>.

Expected Behavior:

  • AppError::try_from(0) should return Ok(AppError::Success).
  • AppError::try_from(1) should return Ok(AppError::Warning).
  • AppError::try_from(-1) should return Ok(AppError::Critical).
  • AppError::try_from(5) should return Err(ConversionError { invalid_value: 5 }).
  • AppError::try_from(-10) should return Err(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_from will always be an i32.
  • The AppError enum should have exactly three variants: Success, Warning, and Critical.
  • The ConversionError struct should have a single field named invalid_value of type i32.
  • The implementation must use the std::convert::TryFrom trait.

Notes

  • Remember to derive Debug and PartialEq for your AppError and ConversionError types to facilitate testing.
  • Think about how to structure your ConversionError to hold the invalid input value.
  • The try_from method's return type is Result<Self, Self::Error>. You need to specify Self::Error correctly.
Loading editor...
rust