-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up error handling, part one (#35)
* refactor using thiserror crate * Created an opaque error type to hide internal error representation details * Collected internal errors under one private enum `ErrorRepr` for future use * rewrite error messages and attach context to public types for greater clarity * Internal code cleanup where possible (simplifying code, reorganization for better readability, making more idiomatic) * reduce code duplication in demo by adding trait bounds to the parser helper `process_input` requiring the output type's `FromStr` Error to implement `Display`. This also allows the demo writer to pass through errors from the crypto library transparently instead of rewriting similar content. * Add tests: *Writing failing test for encryption/decryption, caused by lack of validation in RingElement construction. Relates to #1. This test only works within the library bc `RingElement` is private. * add test that demonstrates expected, but potentially surprising behavior reading RingElements from i8s
- Loading branch information
1 parent
444a279
commit 7e16b04
Showing
9 changed files
with
258 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ edition = "2021" | |
|
||
[dependencies] | ||
rand = "0.8" | ||
thiserror = "1" | ||
|
||
[dev-dependencies] | ||
rand_chacha = "0.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! Contains custom error types. | ||
use thiserror::Error; | ||
|
||
/// An opaque error type that hides the implementation details of internal | ||
/// errors. | ||
// This is a technique that is easy to use with the `thiserror` crate. The attribute | ||
// `error(transparent)` forwards the source and display methods straight through to the underlying | ||
// internal error representations. | ||
#[derive(Error, Debug, PartialEq)] | ||
#[error(transparent)] | ||
pub struct InternalError(#[from] ErrorRepr); | ||
|
||
/// Internal errors. | ||
#[derive(Clone, Debug, PartialEq, Error)] | ||
pub(super) enum ErrorRepr { | ||
/// Thrown when a conversion between the Latin | ||
/// Alphabet and the ring of integers modulo [`RingElement::MODULUS`] fails. | ||
/// | ||
/// This error should only be thrown if: | ||
/// - There is a mistake in the definition of the constant | ||
/// [`RingElement::ALPH_ENCODING`]; | ||
/// - The input was not a lowercase letter from the Latin Alphabet. | ||
#[error("Failed to encode the following characters as ring elements: {0}")] | ||
RingElementEncodingError(String), | ||
} | ||
|
||
// TODO: Are these usable for other ciphers? | ||
/// An error type that indicates a failure to parse a string. | ||
#[derive(Debug, PartialEq, thiserror::Error)] | ||
pub enum EncodingError { | ||
/// Error thrown when parsing a string as a message. This error is thrown | ||
/// when the string included one or more characters that are not | ||
/// lowercase letters from the Latin Alphabet. | ||
#[error("Invalid Message. {0}")] | ||
InvalidMessage(InternalError), | ||
/// Error thrown when parsing a string as a ciphertext. This error is thrown | ||
/// when the string included one or more characters that are not letters | ||
/// from the Latin Alphabet. We allow for strings containing both | ||
/// capitalized and lowercase letters when parsing as string as a | ||
/// ciphertext. | ||
#[error("Invalid Ciphertext. {0}")] | ||
InvalidCiphertext(InternalError), | ||
/// Error thrown when parsing a string as a key. This error is thrown when | ||
/// the string does not represent a number in the appropriate | ||
/// range. e.g., for the Latin Shift Cipher, keys are in the range 0 to 25, | ||
/// inclusive. | ||
#[error("Input \"{0}\" does not represent a valid key")] | ||
InvalidKey(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.