Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize IdentifierError variants #942

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading