From 9b7120bf73895daba25b10cafbe572e457fed13b Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Tue, 20 Apr 2021 16:23:11 -0600 Subject: [PATCH] SDK: More conversions for `Pubkey` --- sdk/program/src/pubkey.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sdk/program/src/pubkey.rs b/sdk/program/src/pubkey.rs index fdabab1e8a..e088cc65fe 100644 --- a/sdk/program/src/pubkey.rs +++ b/sdk/program/src/pubkey.rs @@ -2,7 +2,11 @@ use crate::{decode_error::DecodeError, hash::hashv}; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use num_derive::{FromPrimitive, ToPrimitive}; -use std::{convert::TryFrom, fmt, mem, str::FromStr}; +use std::{ + convert::{Infallible, TryFrom}, + fmt, mem, + str::FromStr, +}; use thiserror::Error; /// Number of bytes in a pubkey @@ -64,7 +68,16 @@ pub enum ParsePubkeyError { WrongSize, #[error("Invalid Base58 string")] Invalid, + #[error("Infallible")] + Infallible, } + +impl From for ParsePubkeyError { + fn from(_: Infallible) -> Self { + Self::Infallible + } +} + impl DecodeError for ParsePubkeyError { fn type_of() -> &'static str { "ParsePubkeyError" @@ -89,6 +102,13 @@ impl FromStr for Pubkey { } } +impl TryFrom<&str> for Pubkey { + type Error = ParsePubkeyError; + fn try_from(s: &str) -> Result { + Pubkey::from_str(s) + } +} + impl Pubkey { pub fn new(pubkey_vec: &[u8]) -> Self { Self(