From 75bf04b22107155f8f8ab6c77f6eefa8117d9ace Mon Sep 17 00:00:00 2001 From: nine <118634361+cryptoni9n@users.noreply.github.com> Date: Sat, 17 Aug 2024 16:34:04 -0400 Subject: [PATCH] Migrate Outgoing to SnafuError (#3854) --- src/error.rs | 17 +++++++++++++-- src/object.rs | 4 +--- src/outgoing.rs | 51 ++++++++++++++++++++++++++++--------------- src/representation.rs | 8 +++---- 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/error.rs b/src/error.rs index dd214a6347..f6f9396e2d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -43,8 +43,21 @@ pub enum SnafuError { source: ordinals::sat_point::Error, input: String, }, - #[snafu(display("Unrecognized representation `{}`", input))] - UnrecognizedRepresentation { source: error::Error, input: String }, + #[snafu(display("Unrecognized representation: `{}`", input))] + UnrecognizedRepresentation { input: String }, + #[snafu(display("Unrecognized outgoing amount: `{}`", input))] + AmountParse { + source: bitcoin::amount::ParseAmountError, + input: String, + }, + #[snafu(display("Unrecognized outgoing: `{}`", input))] + OutgoingParse { input: String }, + #[snafu(display("Failed to parse decimal: {}", source))] + RuneAmountParse { source: error::Error, input: String }, + #[snafu(display("Invalid chain `{}`", chain))] + InvalidChain { chain: String }, + #[snafu(display("Failed to convert script to address: {}", source))] + AddressConversion { source: bitcoin::address::Error }, #[snafu(display("{err}"))] Anyhow { err: anyhow::Error }, #[snafu(display("environment variable `{variable}` not valid unicode: `{}`", value.to_string_lossy()))] diff --git a/src/object.rs b/src/object.rs index 4769bfa89c..a7aa380009 100644 --- a/src/object.rs +++ b/src/object.rs @@ -18,9 +18,7 @@ impl FromStr for Object { fn from_str(input: &str) -> Result { use Representation::*; - match Representation::from_str(input) - .snafu_context(error::UnrecognizedRepresentation { input })? - { + match input.parse::()? { Address => Ok(Self::Address( input.parse().snafu_context(error::AddressParse { input })?, )), diff --git a/src/outgoing.rs b/src/outgoing.rs index f1cad9acde..8bb17803a1 100644 --- a/src/outgoing.rs +++ b/src/outgoing.rs @@ -22,9 +22,9 @@ impl Display for Outgoing { } impl FromStr for Outgoing { - type Err = Error; + type Err = SnafuError; - fn from_str(s: &str) -> Result { + fn from_str(input: &str) -> Result { lazy_static! { static ref AMOUNT: Regex = Regex::new( r"(?x) @@ -63,22 +63,39 @@ 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::SatParse { input })?, + )) + } else if re::SATPOINT.is_match(input) { + Ok(Outgoing::SatPoint( + input + .parse() + .snafu_context(error::SatPointParse { input })?, + )) + } else if re::INSCRIPTION_ID.is_match(input) { + Ok(Outgoing::InscriptionId( + input + .parse() + .snafu_context(error::InscriptionIdParse { input })?, + )) + } else if AMOUNT.is_match(input) { + Ok(Outgoing::Amount( + input.parse().snafu_context(error::AmountParse { input })?, + )) + } else if let Some(captures) = RUNE.captures(input) { + let decimal = captures[1] + .parse::() + .snafu_context(error::RuneAmountParse { input })?; + let rune = captures[2] + .parse() + .snafu_context(error::RuneParse { input })?; + Ok(Self::Rune { decimal, rune }) } else { - bail!("unrecognized outgoing: {s}"); - }) + Err(SnafuError::OutgoingParse { + input: input.to_string(), + }) + } } } diff --git a/src/representation.rs b/src/representation.rs index 194ec444b2..fa5de635f4 100644 --- a/src/representation.rs +++ b/src/representation.rs @@ -37,13 +37,13 @@ impl Representation { } impl FromStr for Representation { - type Err = Error; + type Err = SnafuError; - fn from_str(s: &str) -> Result { - if let Some(i) = REGEX_SET.matches(s).into_iter().next() { + fn from_str(input: &str) -> Result { + if let Some(i) = REGEX_SET.matches(input).into_iter().next() { Ok(PATTERNS[i].0) } else { - Err(anyhow!("unrecognized object")) + Err(error::UnrecognizedRepresentation { input }.build()) } } }