From 6a3fa89ff4e4db60c0d4ad09a0d0a0f8ce9ca595 Mon Sep 17 00:00:00 2001 From: nine Date: Mon, 15 Jul 2024 21:41:49 -0400 Subject: [PATCH 1/5] migrate-object.rs-to-snafu-error-handling --- src/error.rs | 20 +++++++++++++++++- src/object.rs | 56 ++++++++++++++++++++++++++++++++++++++++---------- tests/parse.rs | 2 +- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/error.rs b/src/error.rs index 09254db1af..879f79e123 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,7 +2,25 @@ use super::*; #[derive(Debug, Snafu)] #[snafu(context(suffix(false)), visibility(pub(crate)))] -pub(crate) enum SnafuError { +pub enum SnafuError { + #[snafu(display("Failed to parse address `{}`", input))] + AddressParseError { input: String }, + #[snafu(display("Failed to parse hash `{}`", input))] + HashParseError { input: String }, + #[snafu(display("Failed to parse inscription ID `{}`", input))] + InscriptionIdParseError { input: String }, + #[snafu(display("Failed to parse integer `{}`", input))] + IntegerParseError { input: String }, + #[snafu(display("Failed to parse out point `{}`", input))] + OutPointParseError { input: String }, + #[snafu(display("Failed to parse rune `{}`", input))] + RuneParseError { input: String }, + #[snafu(display("Failed to parse sat `{}`", input))] + SatParseError { input: String }, + #[snafu(display("Failed to parse sat point `{}`", input))] + SatPointParseError { input: String }, + #[snafu(display("Unrecognized representation `{}`", input))] + UnrecognizedRepresentation { input: String }, #[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 159acb16e2..e27e744c54 100644 --- a/src/object.rs +++ b/src/object.rs @@ -13,22 +13,56 @@ pub enum Object { } impl FromStr for Object { - type Err = Error; + type Err = SnafuError; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { use Representation::*; - match Representation::from_str(s)? { - Address => Ok(Self::Address(s.parse()?)), - Decimal | Degree | Percentile | Name => Ok(Self::Sat(s.parse()?)), + match Representation::from_str(s).context(SnafuError::UnrecognizedRepresentation { + input: s.to_string(), + })? { + Address => Ok(Self::Address(s.parse().context( + SnafuError::AddressParseError { + input: s.to_string(), + }, + )?)), + Decimal | Degree | Percentile | Name => { + Ok(Self::Sat(s.parse().context(SnafuError::SatParseError { + input: s.to_string(), + })?)) + } Hash => Ok(Self::Hash( - bitcoin::hashes::sha256::Hash::from_str(s)?.to_byte_array(), + bitcoin::hashes::sha256::Hash::from_str(s) + .context(SnafuError::HashParseError { + input: s.to_string(), + })? + .to_byte_array(), )), - InscriptionId => Ok(Self::InscriptionId(s.parse()?)), - Integer => Ok(Self::Integer(s.parse()?)), - OutPoint => Ok(Self::OutPoint(s.parse()?)), - Rune => Ok(Self::Rune(s.parse()?)), - SatPoint => Ok(Self::SatPoint(s.parse()?)), + InscriptionId => Ok(Self::InscriptionId(s.parse().context( + SnafuError::InscriptionIdParseError { + input: s.to_string(), + }, + )?)), + Integer => Ok(Self::Integer(s.parse().context( + SnafuError::IntegerParseError { + input: s.to_string(), + }, + )?)), + OutPoint => Ok(Self::OutPoint(s.parse().context( + SnafuError::OutPointParseError { + input: s.to_string(), + }, + )?)), + Rune => Ok(Self::Rune(s.parse().context( + SnafuError::RuneParseError { + input: s.to_string(), + }, + )?)), + SatPoint => Ok(Self::SatPoint(s.parse().context( + SnafuError::SatPointParseError { + input: s.to_string(), + }, + )?)), } } } diff --git a/tests/parse.rs b/tests/parse.rs index b94625374a..fd5c6842ae 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -26,7 +26,7 @@ fn hash() { #[test] fn unrecognized_object() { CommandBuilder::new("parse Az") - .stderr_regex(r"error: .*: unrecognized object\n.*") + .stderr_regex(r"error: .*: Unrecognized representation.*") .expected_exit_code(2) .run_and_extract_stdout(); } From c7163ff4ea311ee5a94b0d96bb0228be4119b9b1 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 5 Aug 2024 15:00:40 -0700 Subject: [PATCH 2/5] Fix `AddressParseError` --- src/error.rs | 5 ++++- src/object.rs | 8 +++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index 879f79e123..6284b2eb97 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,7 +4,10 @@ use super::*; #[snafu(context(suffix(false)), visibility(pub(crate)))] pub enum SnafuError { #[snafu(display("Failed to parse address `{}`", input))] - AddressParseError { input: String }, + AddressParse { + source: bitcoin::address::Error, + input: String, + }, #[snafu(display("Failed to parse hash `{}`", input))] HashParseError { input: String }, #[snafu(display("Failed to parse inscription ID `{}`", input))] diff --git a/src/object.rs b/src/object.rs index e27e744c54..2c4972318d 100644 --- a/src/object.rs +++ b/src/object.rs @@ -21,11 +21,9 @@ impl FromStr for Object { match Representation::from_str(s).context(SnafuError::UnrecognizedRepresentation { input: s.to_string(), })? { - Address => Ok(Self::Address(s.parse().context( - SnafuError::AddressParseError { - input: s.to_string(), - }, - )?)), + Address => Ok(Self::Address( + s.parse().snafu_context(error::AddressParse { input: s })?, + )), Decimal | Degree | Percentile | Name => { Ok(Self::Sat(s.parse().context(SnafuError::SatParseError { input: s.to_string(), From d6b3b0fd8510863d1d02957fb64c49bf0cd6f1da Mon Sep 17 00:00:00 2001 From: nine Date: Wed, 7 Aug 2024 10:03:31 -0400 Subject: [PATCH 3/5] adding-snafu_context-for-object.rs --- crates/ordinals/src/lib.rs | 6 ++-- src/error.rs | 44 ++++++++++++++++++------ src/object.rs | 69 +++++++++++++++----------------------- tests/parse.rs | 2 +- 4 files changed, 65 insertions(+), 56 deletions(-) diff --git a/crates/ordinals/src/lib.rs b/crates/ordinals/src/lib.rs index 7fb17fd154..cd1244a083 100644 --- a/crates/ordinals/src/lib.rs +++ b/crates/ordinals/src/lib.rs @@ -54,8 +54,8 @@ mod rarity; mod rune; mod rune_id; mod runestone; -mod sat; -mod sat_point; -mod spaced_rune; +pub mod sat; +pub mod sat_point; +pub mod spaced_rune; mod terms; pub mod varint; diff --git a/src/error.rs b/src/error.rs index 879f79e123..1faa44a06c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,23 +4,47 @@ use super::*; #[snafu(context(suffix(false)), visibility(pub(crate)))] pub enum SnafuError { #[snafu(display("Failed to parse address `{}`", input))] - AddressParseError { input: String }, + AddressParse { + source: bitcoin::address::Error, + input: String, + }, #[snafu(display("Failed to parse hash `{}`", input))] - HashParseError { input: String }, + HashParse { + source: bitcoin::hashes::hex::Error, + input: String, + }, #[snafu(display("Failed to parse inscription ID `{}`", input))] - InscriptionIdParseError { input: String }, + InscriptionIdParse { + source: inscriptions::inscription_id::ParseError, + input: String, + }, #[snafu(display("Failed to parse integer `{}`", input))] - IntegerParseError { input: String }, + IntegerParse { + source: std::num::ParseIntError, + input: String, + }, #[snafu(display("Failed to parse out point `{}`", input))] - OutPointParseError { input: String }, + OutPointParse { + source: bitcoin::transaction::ParseOutPointError, + input: String, + }, #[snafu(display("Failed to parse rune `{}`", input))] - RuneParseError { input: String }, + RuneParse { + source: ordinals::spaced_rune::Error, + input: String, + }, #[snafu(display("Failed to parse sat `{}`", input))] - SatParseError { input: String }, + SatParse { + source: ordinals::sat::Error, + input: String, + }, #[snafu(display("Failed to parse sat point `{}`", input))] - SatPointParseError { input: String }, + SatPointParse { + source: ordinals::sat_point::Error, + input: String, + }, #[snafu(display("Unrecognized representation `{}`", input))] - UnrecognizedRepresentation { input: String }, + UnrecognizedRepresentation { source: error::Error, input: String }, #[snafu(display("{err}"))] Anyhow { err: anyhow::Error }, #[snafu(display("environment variable `{variable}` not valid unicode: `{}`", value.to_string_lossy()))] @@ -81,4 +105,4 @@ impl ResultExt for std::result::Result { use snafu::ResultExt; self.with_context(context) } -} +} \ No newline at end of file diff --git a/src/object.rs b/src/object.rs index e27e744c54..fbc33537e0 100644 --- a/src/object.rs +++ b/src/object.rs @@ -18,51 +18,36 @@ impl FromStr for Object { fn from_str(s: &str) -> Result { use Representation::*; - match Representation::from_str(s).context(SnafuError::UnrecognizedRepresentation { - input: s.to_string(), - })? { - Address => Ok(Self::Address(s.parse().context( - SnafuError::AddressParseError { - input: s.to_string(), - }, - )?)), - Decimal | Degree | Percentile | Name => { - Ok(Self::Sat(s.parse().context(SnafuError::SatParseError { - input: s.to_string(), - })?)) - } + match Representation::from_str(s) + .snafu_context(error::UnrecognizedRepresentation { input: s })? + { + Address => Ok(Self::Address( + s.parse().snafu_context(error::AddressParse { input: s })?, + )), + Decimal | Degree | Percentile | Name => Ok(Self::Sat( + s.parse().snafu_context(error::SatParse { input: s })?, + )), Hash => Ok(Self::Hash( bitcoin::hashes::sha256::Hash::from_str(s) - .context(SnafuError::HashParseError { - input: s.to_string(), - })? + .snafu_context(error::HashParse { input: s })? .to_byte_array(), )), - InscriptionId => Ok(Self::InscriptionId(s.parse().context( - SnafuError::InscriptionIdParseError { - input: s.to_string(), - }, - )?)), - Integer => Ok(Self::Integer(s.parse().context( - SnafuError::IntegerParseError { - input: s.to_string(), - }, - )?)), - OutPoint => Ok(Self::OutPoint(s.parse().context( - SnafuError::OutPointParseError { - input: s.to_string(), - }, - )?)), - Rune => Ok(Self::Rune(s.parse().context( - SnafuError::RuneParseError { - input: s.to_string(), - }, - )?)), - SatPoint => Ok(Self::SatPoint(s.parse().context( - SnafuError::SatPointParseError { - input: s.to_string(), - }, - )?)), + InscriptionId => Ok(Self::InscriptionId( + s.parse() + .snafu_context(error::InscriptionIdParse { input: s })?, + )), + Integer => Ok(Self::Integer( + s.parse().snafu_context(error::IntegerParse { input: s })?, + )), + OutPoint => Ok(Self::OutPoint( + s.parse().snafu_context(error::OutPointParse { input: s })?, + )), + Rune => Ok(Self::Rune( + s.parse().snafu_context(error::RuneParse { input: s })?, + )), + SatPoint => Ok(Self::SatPoint( + s.parse().snafu_context(error::SatPointParse { input: s })?, + )), } } } @@ -230,4 +215,4 @@ mod tests { }), ); } -} +} \ No newline at end of file diff --git a/tests/parse.rs b/tests/parse.rs index fd5c6842ae..ab43933b8a 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -26,7 +26,7 @@ fn hash() { #[test] fn unrecognized_object() { CommandBuilder::new("parse Az") - .stderr_regex(r"error: .*: Unrecognized representation.*") + .stderr_regex(r"error: .*: unrecognized representation.*") .expected_exit_code(2) .run_and_extract_stdout(); } From a0820955a8dc7b91ffc7de150f1ab00832cec205 Mon Sep 17 00:00:00 2001 From: nine Date: Wed, 7 Aug 2024 10:18:07 -0400 Subject: [PATCH 4/5] fix-formatting-issues --- src/error.rs | 2 +- tests/parse.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index 1faa44a06c..dd214a6347 100644 --- a/src/error.rs +++ b/src/error.rs @@ -105,4 +105,4 @@ impl ResultExt for std::result::Result { use snafu::ResultExt; self.with_context(context) } -} \ No newline at end of file +} diff --git a/tests/parse.rs b/tests/parse.rs index ab43933b8a..fd5c6842ae 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -26,7 +26,7 @@ fn hash() { #[test] fn unrecognized_object() { CommandBuilder::new("parse Az") - .stderr_regex(r"error: .*: unrecognized representation.*") + .stderr_regex(r"error: .*: Unrecognized representation.*") .expected_exit_code(2) .run_and_extract_stdout(); } From fa8e6f78afef63c247e09d7b542d94a516441f52 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 7 Aug 2024 15:31:51 -0700 Subject: [PATCH 5/5] Adjust --- src/object.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/object.rs b/src/object.rs index bce6643874..4769bfa89c 100644 --- a/src/object.rs +++ b/src/object.rs @@ -15,38 +15,43 @@ pub enum Object { impl FromStr for Object { type Err = SnafuError; - fn from_str(s: &str) -> Result { + fn from_str(input: &str) -> Result { use Representation::*; - match Representation::from_str(s) - .snafu_context(error::UnrecognizedRepresentation { input: s })? + match Representation::from_str(input) + .snafu_context(error::UnrecognizedRepresentation { input })? { Address => Ok(Self::Address( - s.parse().snafu_context(error::AddressParse { input: s })?, + input.parse().snafu_context(error::AddressParse { input })?, )), Decimal | Degree | Percentile | Name => Ok(Self::Sat( - s.parse().snafu_context(error::SatParse { input: s })?, + input.parse().snafu_context(error::SatParse { input })?, )), Hash => Ok(Self::Hash( - bitcoin::hashes::sha256::Hash::from_str(s) - .snafu_context(error::HashParse { input: s })? + bitcoin::hashes::sha256::Hash::from_str(input) + .snafu_context(error::HashParse { input })? .to_byte_array(), )), InscriptionId => Ok(Self::InscriptionId( - s.parse() - .snafu_context(error::InscriptionIdParse { input: s })?, + input + .parse() + .snafu_context(error::InscriptionIdParse { input })?, )), Integer => Ok(Self::Integer( - s.parse().snafu_context(error::IntegerParse { input: s })?, + input.parse().snafu_context(error::IntegerParse { input })?, )), OutPoint => Ok(Self::OutPoint( - s.parse().snafu_context(error::OutPointParse { input: s })?, + input + .parse() + .snafu_context(error::OutPointParse { input })?, )), Rune => Ok(Self::Rune( - s.parse().snafu_context(error::RuneParse { input: s })?, + input.parse().snafu_context(error::RuneParse { input })?, )), SatPoint => Ok(Self::SatPoint( - s.parse().snafu_context(error::SatPointParse { input: s })?, + input + .parse() + .snafu_context(error::SatPointParse { input })?, )), } }