Skip to content

Commit

Permalink
feat: provide Into<String> and AsRef<str> for identifier types
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Nov 20, 2023
1 parent bf77378 commit 9175255
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/974-id-into-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Provide `Into<String>` and `AsRef<str>` for all identifiers types.
([#974](https://github.com/cosmos/ibc-rs/pull/974))
2 changes: 1 addition & 1 deletion crates/ibc-testkit/src/hosts/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl HostBlock {
let light_block = TestgenLightBlock::new_default_with_header(
TestgenHeader::new(validators)
.height(height)
.chain_id(&chain_id.to_string())
.chain_id(chain_id.as_ref())
.next_validators(next_validators)
.time(timestamp.into_tm_time().expect("Never fails")),
)
Expand Down
30 changes: 27 additions & 3 deletions crates/ibc/src/core/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ impl FromStr for ChainId {
}
}

impl AsRef<str> for ChainId {
fn as_ref(&self) -> &str {
&self.id
}
}

impl From<ChainId> for String {
fn from(chain_id: ChainId) -> String {
chain_id.id
}

Check warning on line 169 in crates/ibc/src/core/ics24_host/identifier.rs

View check run for this annotation

Codecov / codecov/patch

crates/ibc/src/core/ics24_host/identifier.rs#L167-L169

Added lines #L167 - L169 were not covered by tests
}

impl Display for ChainId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
write!(f, "{}", self.id)
Expand Down Expand Up @@ -240,6 +252,12 @@ impl Display for ClientId {
}
}

impl AsRef<str> for ClientId {
fn as_ref(&self) -> &str {
self.0.as_str()
}

Check warning on line 258 in crates/ibc/src/core/ics24_host/identifier.rs

View check run for this annotation

Codecov / codecov/patch

crates/ibc/src/core/ics24_host/identifier.rs#L256-L258

Added lines #L256 - L258 were not covered by tests
}

impl FromStr for ClientId {
type Err = IdentifierError;

Expand Down Expand Up @@ -282,7 +300,7 @@ impl PartialEq<str> for ClientId {
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)]
pub struct ConnectionId(String);

impl ConnectionId {
Expand Down Expand Up @@ -324,6 +342,12 @@ impl Display for ConnectionId {
}
}

impl AsRef<str> for ConnectionId {
fn as_ref(&self) -> &str {
self.0.as_str()
}

Check warning on line 348 in crates/ibc/src/core/ics24_host/identifier.rs

View check run for this annotation

Codecov / codecov/patch

crates/ibc/src/core/ics24_host/identifier.rs#L346-L348

Added lines #L346 - L348 were not covered by tests
}

impl FromStr for ConnectionId {
type Err = IdentifierError;

Expand Down Expand Up @@ -366,7 +390,7 @@ impl PartialEq<str> for ConnectionId {
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)]
pub struct PortId(String);

impl PortId {
Expand Down Expand Up @@ -429,7 +453,7 @@ impl AsRef<str> for PortId {
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)]
pub struct ChannelId(String);

impl ChannelId {
Expand Down

0 comments on commit 9175255

Please sign in to comment.