Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The usecase what I'm resolving is the "error propagation", usecase derive from error types design in Rust.
Error types design in Rust
In Rust normally use the
Result<T, E>
type, and propagate error in functions using the try-operator?
, but this type is too flexible with the E generic. The E type not even needs to implement theDisplay
trait; and for this reason not always can use theBox<dyn Error>
like common place.For solve this complication, the developers use some guidelines for Error types design, for example the majory implements the
Display
andDebug
traits, and use common ways like "enum style" or "struct style", and use theFrom
trait for converting error types.Struct Style
Enum Style
*In reality, most programmers use thiserror for error convertions.
Of course the above only implementing if you considered important identify the errors individually, which it may not important in little applications in user side, but in crates, enhances the develop experience.
Just now, the
NonEmptyString
constructors returns a emptyString
that difficult theFrom
trait implementation, because its necessary discriminate it to othersResult<T, String>
that provides explicit information for error handling. And the justification for return the input if this is empty, just cover an extreme edge case and forget the common cases.In this PR I use a Cargo feature to enable the ErrorEmptyString type, but some different answers that I propose:
Err("Error: zero-value string".to_string())
.ErrorEmptyString
(in this PR) or other.std::io::ErrorKind::InvalidInput
.std::io::Error
, for example:std::io::Error::new(std::io::ErrorKind::InvalidInput, "zero-value string")
Option
inNonEmptyString::new
and implement one of the above answers.