Skip to content

Commit

Permalink
problem: cannot parse default to_string of account hdpath
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Nov 5, 2020
1 parent 123d7bb commit 5762d69
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/path_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ use crate::traits::HDPath;
/// ```
/// use hdpath::{AccountHDPath, Purpose};
///
/// //creates path m/84'/0'/0'/0/0
/// //creates path m/84'/0'/0'
/// let hd_account = AccountHDPath::new(Purpose::Witness, 0, 0);
/// ```
/// # Parse string
/// ```
/// use hdpath::{AccountHDPath, Purpose};
/// use hdpath::{AccountHDPath};
/// # use std::str::FromStr;
///
/// //creates path m/84'/0'/0'/0/0
/// //creates path m/84'/0'/0'
/// let hd_account = AccountHDPath::from_str("m/84'/0'/0'").unwrap();
/// ```
///
/// Internal type and index can be explicitly market as unused (which is the default format for converting it into a string).
///
/// ```
/// use hdpath::{AccountHDPath};
/// # use std::str::FromStr;
///
/// //creates path m/84'/0'/0'
/// let hd_account = AccountHDPath::from_str("m/84'/0'/0'/x/x").unwrap();
/// ```
///
/// # Create actial path
/// ```
/// use hdpath::{AccountHDPath, Purpose, StandardHDPath};
Expand Down Expand Up @@ -181,7 +191,12 @@ impl FromStr for AccountHDPath {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = CustomHDPath::from_str(s)?;
let clean = if s.ends_with("/x/x") {
&s[0..s.len() - 4]
} else {
s
};
let value = CustomHDPath::from_str(clean)?;
AccountHDPath::try_from(value)
}
}
Expand Down Expand Up @@ -257,6 +272,16 @@ mod tests {
assert_eq!(5, hd_account.account);
}

#[test]
fn create_from_acc_string() {
let hd_account = AccountHDPath::from_str("m/84'/0'/5'/x/x");
assert!(hd_account.is_ok());
let hd_account = hd_account.unwrap();
assert_eq!(Purpose::Witness, hd_account.purpose);
assert_eq!(0, hd_account.coin_type);
assert_eq!(5, hd_account.account);
}

#[test]
fn create_from_string_sh() {
let hd_account = AccountHDPath::try_from("m/49'/0'/5'");
Expand Down

0 comments on commit 5762d69

Please sign in to comment.