Skip to content

Commit

Permalink
feat: provide Into<String> for identifier types (#974)
Browse files Browse the repository at this point in the history
* feat: provide `Into<String>` and `AsRef<str>` for identifier types

* fix: use as_str on chain_id

---------

Co-authored-by: Farhad Shabani <[email protected]>
  • Loading branch information
mina86 and Farhad-Shabani authored Nov 22, 2023
1 parent 71df7e8 commit 45b1c97
Show file tree
Hide file tree
Showing 6 changed files with 15 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>` for all identifiers types.
([\#974](https://github.com/cosmos/ibc-rs/pull/974))
6 changes: 6 additions & 0 deletions ibc-core/ics24-host/types/src/identifiers/chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ impl FromStr for ChainId {
}
}

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

impl Display for ChainId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
write!(f, "{}", self.id)
Expand Down
3 changes: 2 additions & 1 deletion ibc-core/ics24-host/types/src/identifiers/channel_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt::{Debug, Display, Error as FmtError, Formatter};
use core::str::FromStr;

use derive_more::Into;
use ibc_primitives::prelude::*;

use crate::error::IdentifierError;
Expand All @@ -22,7 +23,7 @@ const CHANNEL_ID_PREFIX: &str = "channel";
)]
#[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
3 changes: 2 additions & 1 deletion ibc-core/ics24-host/types/src/identifiers/connection_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt::{Display, Error as FmtError, Formatter};
use core::str::FromStr;

use derive_more::Into;
use ibc_primitives::prelude::*;

use crate::error::IdentifierError;
Expand All @@ -22,7 +23,7 @@ const CONNECTION_ID_PREFIX: &str = "connection";
)]
#[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
3 changes: 2 additions & 1 deletion ibc-core/ics24-host/types/src/identifiers/port_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt::{Display, Error as FmtError, Formatter};
use core::str::FromStr;

use derive_more::Into;
use ibc_primitives::prelude::*;

use crate::error::IdentifierError;
Expand All @@ -22,7 +23,7 @@ const TRANSFER_PORT_ID: &str = "transfer";
)]
#[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
2 changes: 1 addition & 1 deletion 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_str())
.next_validators(next_validators)
.time(timestamp.into_tm_time().expect("Never fails")),
)
Expand Down

0 comments on commit 45b1c97

Please sign in to comment.