From 055109cb61b7f8e17d88e7b6a38660d45277fc61 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Mon, 27 Nov 2023 17:40:27 +0100 Subject: [PATCH] feat: optimize `IdentifierError` variants (#942) * remove ContainsSeparator * remove length * remove Empty * changelog * update changelog entry Signed-off-by: Rano | Ranadeep --------- Signed-off-by: Rano | Ranadeep Co-authored-by: Rano | Ranadeep --- .../breaking-changes/978-identifier-error.md | 2 ++ ibc-core/ics24-host/types/src/error.rs | 13 ++----------- ibc-core/ics24-host/types/src/validate.rs | 8 -------- 3 files changed, 4 insertions(+), 19 deletions(-) create mode 100644 .changelog/unreleased/breaking-changes/978-identifier-error.md diff --git a/.changelog/unreleased/breaking-changes/978-identifier-error.md b/.changelog/unreleased/breaking-changes/978-identifier-error.md new file mode 100644 index 000000000..cd03b558e --- /dev/null +++ b/.changelog/unreleased/breaking-changes/978-identifier-error.md @@ -0,0 +1,2 @@ +- Optimize `IdentifierError` variants and make them mutually exclusive. + ([\#978](https://github.com/cosmos/ibc-rs/issues/978)) diff --git a/ibc-core/ics24-host/types/src/error.rs b/ibc-core/ics24-host/types/src/error.rs index 980a3a1c4..4df4c4dd9 100644 --- a/ibc-core/ics24-host/types/src/error.rs +++ b/ibc-core/ics24-host/types/src/error.rs @@ -4,15 +4,8 @@ use ibc_primitives::prelude::*; #[cfg_attr(feature = "serde", derive(serde::Serialize))] #[derive(Debug, Display)] pub enum IdentifierError { - /// identifier `{id}` cannot contain separator '/' - ContainSeparator { id: String }, - /// identifier `{id}` has invalid length `{length}` must be between `{min}`-`{max}` characters - InvalidLength { - id: String, - length: u64, - min: u64, - max: u64, - }, + /// identifier `{id}` has invalid length; must be between `{min}` and `{max}` characters + InvalidLength { id: String, min: u64, max: u64 }, /// identifier `{id}` must only contain alphanumeric characters or `.`, `_`, `+`, `-`, `#`, - `[`, `]`, `<`, `>` InvalidCharacter { id: String }, /// identifier prefix `{prefix}` is invalid @@ -23,8 +16,6 @@ pub enum IdentifierError { RevisionNumberOverflow, /// String `{value}` cannot be converted to packet sequence, error: `{reason}` InvalidStringAsSequence { value: String, reason: String }, - /// identifier cannot be empty - Empty, } #[cfg(feature = "std")] diff --git a/ibc-core/ics24-host/types/src/validate.rs b/ibc-core/ics24-host/types/src/validate.rs index 5fb164020..47a92ac72 100644 --- a/ibc-core/ics24-host/types/src/validate.rs +++ b/ibc-core/ics24-host/types/src/validate.rs @@ -2,19 +2,12 @@ use ibc_primitives::prelude::*; use crate::error::IdentifierError as Error; -/// Path separator (ie. forward slash '/') -const PATH_SEPARATOR: char = '/'; const VALID_SPECIAL_CHARS: &str = "._+-#[]<>"; /// Checks if the identifier only contains valid characters as specified in the /// [`ICS-24`](https://github.com/cosmos/ibc/tree/main/spec/core/ics-024-host-requirements#paths-identifiers-separators)] /// spec. pub fn validate_identifier_chars(id: &str) -> Result<(), Error> { - // Check identifier does not contain path separators - if id.contains(PATH_SEPARATOR) { - return Err(Error::ContainSeparator { id: id.into() }); - } - // Check that the identifier comprises only valid characters: // - Alphanumeric // - `.`, `_`, `+`, `-`, `#` @@ -42,7 +35,6 @@ pub fn validate_identifier_length(id: &str, min: u64, max: u64) -> Result<(), Er } else { Err(Error::InvalidLength { id: id.into(), - length, min, max, })