Skip to content

Commit

Permalink
Migrate chain.rs to snafu error (ordinals#3904)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoni9n authored Aug 28, 2024
1 parent f35e6db commit d13dcef
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ impl Chain {
}
}

pub(crate) fn address_from_script(
self,
script: &Script,
) -> Result<Address, bitcoin::address::Error> {
Address::from_script(script, self.network())
pub(crate) fn address_from_script(self, script: &Script) -> Result<Address, SnafuError> {
Address::from_script(script, self.network()).snafu_context(error::AddressConversion)
}

pub(crate) fn join_with_data_dir(self, data_dir: impl AsRef<Path>) -> PathBuf {
Expand Down Expand Up @@ -110,15 +107,17 @@ impl Display for Chain {
}

impl FromStr for Chain {
type Err = Error;
type Err = SnafuError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mainnet" => Ok(Self::Mainnet),
"regtest" => Ok(Self::Regtest),
"signet" => Ok(Self::Signet),
"testnet" => Ok(Self::Testnet),
_ => bail!("invalid chain `{s}`"),
_ => Err(SnafuError::InvalidChain {
chain: s.to_string(),
}),
}
}
}
Expand All @@ -135,7 +134,7 @@ mod tests {
assert_eq!("testnet".parse::<Chain>().unwrap(), Chain::Testnet);
assert_eq!(
"foo".parse::<Chain>().unwrap_err().to_string(),
"invalid chain `foo`"
"Invalid chain `foo`"
);
}
}

0 comments on commit d13dcef

Please sign in to comment.