Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrating outgoing.rs to snafu error handling #3854

Merged
merged 17 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use bitcoin::address::Error as AddressError;

#[derive(Debug, Snafu)]
#[snafu(context(suffix(false)), visibility(pub(crate)))]
Expand Down Expand Up @@ -45,6 +46,39 @@ pub enum SnafuError {
},
#[snafu(display("Unrecognized representation `{}`", input))]
UnrecognizedRepresentation { source: error::Error, input: String },
#[snafu(display("Unrecognized outgoing amount: `{}`", input))]
UnrecognizedAmount {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would call these *Parse instead of Unrecognized*.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Casey - I'm pushing changes now that rename all of the Unrecognized* errors, except for UnrecognizedRepresentation, because that was already merged as part of #3192.

source: bitcoin::amount::ParseAmountError,
input: String,
},
#[snafu(display("Unrecognized outgoing rune: `{}`", input))]
UnrecognizedRune {
source: ordinals::spaced_rune::Error,
input: String,
},
#[snafu(display("Unrecognized outgoing sat: `{}`", input))]
UnrecognizedSat {
source: ordinals::sat::Error,
input: String,
},
#[snafu(display("Unrecognized outgoing sat point: `{}`", input))]
UnrecognizedSatPoint {
source: ordinals::sat_point::Error,
input: String,
},
#[snafu(display("Unrecognized outgoing inscription ID: `{}`", input))]
UnrecognizedInscriptionId {
source: inscriptions::inscription_id::ParseError,
input: String,
},
#[snafu(display("Unrecognized outgoing: `{}`", input))]
UnrecognizedOutgoing { input: String },
#[snafu(display("Failed to parse decimal: {}", source))]
DecimalParse { source: error::Error, input: String },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only used in parsing rune amounts, then maybe RuneAmountParse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing, pushing changes now. Are you ok with having the following errors be so similar, or would you prefer to merge them?

OutgoingRuneParse and RuneParse
OutgoingSatParse and SatParse
OutgoingInscriptionIdParse and InscriptionIdParse

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, sorry, I wasn't looking at those other errors. Those errors can all be combined. In general, existing errors should be reused.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, updates pushed.

#[snafu(display("Invalid chain `{}`", chain))]
InvalidChain { chain: String },
#[snafu(display("Failed to convert script to address: {}", source))]
AddressConversion { source: AddressError },
#[snafu(display("{err}"))]
Anyhow { err: anyhow::Error },
#[snafu(display("environment variable `{variable}` not valid unicode: `{}`", value.to_string_lossy()))]
Expand Down
55 changes: 38 additions & 17 deletions src/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ impl Display for Outgoing {
}

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

fn from_str(s: &str) -> Result<Self, Self::Err> {
fn from_str(input: &str) -> Result<Self, Self::Err> {
lazy_static! {
static ref AMOUNT: Regex = Regex::new(
r"(?x)
Expand Down Expand Up @@ -63,22 +63,43 @@ impl FromStr for Outgoing {
.unwrap();
}

Ok(if re::SAT_NAME.is_match(s) {
Self::Sat(s.parse()?)
} else if re::SATPOINT.is_match(s) {
Self::SatPoint(s.parse()?)
} else if re::INSCRIPTION_ID.is_match(s) {
Self::InscriptionId(s.parse()?)
} else if AMOUNT.is_match(s) {
Self::Amount(s.parse()?)
} else if let Some(captures) = RUNE.captures(s) {
Self::Rune {
decimal: captures[1].parse()?,
rune: captures[2].parse()?,
}
if re::SAT_NAME.is_match(input) {
Ok(Outgoing::Sat(
input
.parse()
.snafu_context(error::UnrecognizedSat { input })?,
))
} else if re::SATPOINT.is_match(input) {
Ok(Outgoing::SatPoint(
input
.parse()
.snafu_context(error::UnrecognizedSatPoint { input })?,
))
} else if re::INSCRIPTION_ID.is_match(input) {
Ok(Outgoing::InscriptionId(
input
.parse()
.snafu_context(error::UnrecognizedInscriptionId { input })?,
))
} else if AMOUNT.is_match(input) {
Ok(Outgoing::Amount(
input
.parse()
.snafu_context(error::UnrecognizedAmount { input })?,
))
} else if let Some(captures) = RUNE.captures(input) {
let decimal = captures[1]
.parse::<Decimal>()
.snafu_context(error::DecimalParse { input })?;
let rune = captures[2]
.parse()
.snafu_context(error::RuneParse { input })?;
Ok(Self::Rune { decimal, rune })
} else {
bail!("unrecognized outgoing: {s}");
})
Err(SnafuError::UnrecognizedOutgoing {
input: input.to_string(),
})
}
}
}

Expand Down
Loading