SDK: More conversion for `DerivationPath`

This commit is contained in:
Trent Nelson 2021-04-24 01:49:35 -06:00 committed by Trent Nelson
parent 9b7120bf73
commit 722de942ca
1 changed files with 18 additions and 3 deletions

View File

@ -1,7 +1,7 @@
use {
core::{iter::IntoIterator, slice::Iter},
derivation_path::{ChildIndex, DerivationPath as DerivationPathInner},
std::{fmt, str::FromStr},
std::{convert::{Infallible, TryFrom}, fmt, str::FromStr},
thiserror::Error,
};
@ -9,13 +9,21 @@ const ACCOUNT_INDEX: usize = 2;
const CHANGE_INDEX: usize = 3;
/// Derivation path error.
#[derive(Error, Debug, Clone)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum DerivationPathError {
#[error("invalid derivation path: {0}")]
InvalidDerivationPath(String),
#[error("infallible")]
Infallible,
}
#[derive(PartialEq)]
impl From<Infallible> for DerivationPathError {
fn from(_: Infallible) -> Self {
Self::Infallible
}
}
#[derive(Clone, PartialEq)]
pub struct DerivationPath(DerivationPathInner);
impl Default for DerivationPath {
@ -24,6 +32,13 @@ impl Default for DerivationPath {
}
}
impl TryFrom<&str> for DerivationPath {
type Error = DerivationPathError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::from_key_str(s)
}
}
impl DerivationPath {
fn new<P: Into<Box<[ChildIndex]>>>(path: P) -> Self {
Self(DerivationPathInner::new(path))