Skip to content

Commit

Permalink
feat: optimize IdentifierError variants (#942)
Browse files Browse the repository at this point in the history
* remove ContainsSeparator

* remove length

* remove Empty

* changelog

* update changelog entry

Signed-off-by: Rano | Ranadeep <[email protected]>

---------

Signed-off-by: Rano | Ranadeep <[email protected]>
Co-authored-by: Rano | Ranadeep <[email protected]>
  • Loading branch information
mina86 and rnbguy authored Nov 27, 2023
1 parent 3be16b2 commit 8f2ddfe
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Optimize `IdentifierError` variants and make them mutually exclusive.
([\#978](https://github.com/cosmos/ibc-rs/issues/978))
13 changes: 2 additions & 11 deletions ibc-core/ics24-host/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")]
Expand Down
8 changes: 0 additions & 8 deletions ibc-core/ics24-host/types/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// - `.`, `_`, `+`, `-`, `#`
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit 8f2ddfe

Please sign in to comment.