Skip to content

Commit

Permalink
feat: add from_str for ChainKind (#192)
Browse files Browse the repository at this point in the history
* ChainKind: implement from string error

* Fix from_str parser

* Fix clippy
  • Loading branch information
karim-en authored Jan 16, 2025
1 parent 314da6f commit b0ca4e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions near/omni-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ pub enum ChainKind {
Base,
}

impl FromStr for ChainKind {
type Err = String;

fn from_str(s: &str) -> Result<ChainKind, Self::Err> {
near_sdk::serde_json::from_str(&format!("\"{s}\"")).map_err(stringify)
}
}

impl From<&OmniAddress> for ChainKind {
fn from(input: &OmniAddress) -> Self {
input.get_chain()
Expand Down
9 changes: 9 additions & 0 deletions near/omni-types/src/tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,12 @@ fn test_get_evm_token_prefix() {
);
}
}

#[test]
fn test_chain_kind_from_str() {
let chain: ChainKind = "Eth".parse().unwrap();
assert_eq!(chain, ChainKind::Eth);

let chain: ChainKind = "Base".parse().unwrap();
assert_eq!(chain, ChainKind::Base);
}

0 comments on commit b0ca4e1

Please sign in to comment.