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 f716de5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 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))
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
}
}

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()
}
}

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()
}
}

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 f716de5

Please sign in to comment.