Hone logo
Hone
Problems

Deserialize a String into a Custom Data Structure in Rust

Deserialization is the process of converting a string representation of data into a usable data structure within your program. This is a fundamental task in many applications, such as parsing configuration files, handling API responses, or reading data from files. This challenge asks you to implement a deserialization function in Rust that converts a string into a custom data structure representing a simple product.

Problem Description

You are tasked with creating a function deserialize_product that takes a string as input and attempts to parse it into a Product struct. The input string will follow a specific format: "name=product_name,price=product_price". The product_name is a string, and the product_price is a floating-point number.

The function should return a Result<Product, String>. If the deserialization is successful, it should return Ok(Product) containing the parsed data. If any error occurs during parsing (e.g., invalid format, non-numeric price), it should return Err(String) with a descriptive error message.

Key Requirements:

  • Error Handling: Robustly handle potential errors during parsing, providing informative error messages.
  • Data Type Conversion: Correctly convert the string representations of the product name and price into their respective data types.
  • Format Adherence: Strictly adhere to the specified input format.
  • Return Type: Return a Result<Product, String> to indicate success or failure.

Expected Behavior:

The function should parse the input string, extract the product name and price, and create a Product struct. If the input string is malformed or contains invalid data, it should return an error.

Edge Cases to Consider:

  • Empty input string.
  • Missing = signs.
  • Missing , sign.
  • Non-numeric price.
  • Whitespace around values.
  • Invalid characters in the product name.

Examples

Example 1:

Input: "name=Laptop,price=1200.50"
Output: Ok(Product { name: "Laptop", price: 1200.5 })
Explanation: The input string is correctly parsed, and a `Product` struct is created with the extracted values.

Example 2:

Input: "name=Keyboard,price=invalid"
Output: Err("Invalid price format: invalid")
Explanation: The price "invalid" cannot be converted to a float, so an error is returned.

Example 3:

Input: "name=Mouse"
Output: Err("Missing price")
Explanation: The input string is missing the price, so an error is returned.

Example 4:

Input: ""
Output: Err("Empty input string")
Explanation: The input string is empty, so an error is returned.

Constraints

  • The product name can contain any printable ASCII characters.
  • The product price will always be a positive floating-point number.
  • The input string will be no longer than 256 characters.
  • The function should be reasonably efficient; avoid unnecessary allocations or complex operations.

Notes

Consider using Rust's split method to separate the key-value pairs. The parse method can be used to convert strings to numbers. Remember to handle potential errors at each step of the parsing process. Think about how to construct informative error messages to aid in debugging. You'll need to define a Product struct.

Loading editor...
rust