Consider making getter methods on enums return Result<T, Error> instead of panicking
Hey there,
First of all, thanks for writing this library and making it open source! I'm using it in a small project of mine and it's great to be able to extract values from serialized Java data files without actually needing to write my tool in Java.
One thing I noticed while using jaded is that the convenience getter methods on enums (Content, Value) panic when they don't see the expected enum variant. I generally prefer to avoid panics in my Rust apps, which makes these methods not a great fit for my use case. As such, I'd like to suggest a tweak to these methods to make the library more flexible.
Before:
impl Content {
/// Get the value of the object represented by this instance
///
/// panics if this Content is raw block data
pub fn value(&self) -> &Value {
match self {
Content::Object(val) => val,
_ => panic!("Can't unwrap block data to value"),
}
}
}
After:
impl Content {
/// Get the value of the object represented by this instance
pub fn value(&self) -> Result<&Value, Error> {
match self {
Content::Object(val) => Ok(val),
_ => Err("Can't unwrap block data to value"), // or whichever error type, it doesn't need to be a string
}
}
}
Making these methods return a Result
would provide more flexibility to the caller:
- They can still choose to panic by calling
.unwrap()
- They can choose to handle the error case differently
- They can propagate the error using
?
I'd be more than happy to contribute a Merge Request for this change, though I must mention, I've recently started a new job and my schedule is a bit tight. It might take me a while to get around to it. Nevertheless, I'm excited about the potential improvement and am willing to chip in when I can.
Thanks again for the amazing work on this library!
Cheers,
Mick