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

migrate object.rs to snafu error handling #3858

Merged
merged 11 commits into from
Aug 7, 2024
6 changes: 3 additions & 3 deletions crates/ordinals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
44 changes: 43 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,49 @@ 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))]
AddressParse {
source: bitcoin::address::Error,
input: String,
},
#[snafu(display("Failed to parse hash `{}`", input))]
HashParse {
source: bitcoin::hashes::hex::Error,
input: String,
},
#[snafu(display("Failed to parse inscription ID `{}`", input))]
InscriptionIdParse {
source: inscriptions::inscription_id::ParseError,
input: String,
},
#[snafu(display("Failed to parse integer `{}`", input))]
IntegerParse {
source: std::num::ParseIntError,
input: String,
},
#[snafu(display("Failed to parse out point `{}`", input))]
OutPointParse {
source: bitcoin::transaction::ParseOutPointError,
input: String,
},
#[snafu(display("Failed to parse rune `{}`", input))]
RuneParse {
source: ordinals::spaced_rune::Error,
input: String,
},
#[snafu(display("Failed to parse sat `{}`", input))]
SatParse {
source: ordinals::sat::Error,
input: String,
},
#[snafu(display("Failed to parse sat point `{}`", input))]
SatPointParse {
source: ordinals::sat_point::Error,
input: String,
},
#[snafu(display("Unrecognized representation `{}`", input))]
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()))]
Expand Down
41 changes: 30 additions & 11 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,41 @@ pub enum Object {
}

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

fn from_str(s: &str) -> Result<Self> {
fn from_str(s: &str) -> Result<Self, Self::Err> {
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)
.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)?.to_byte_array(),
bitcoin::hashes::sha256::Hash::from_str(s)
.snafu_context(error::HashParse { input: s })?
.to_byte_array(),
)),
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 })?,
)),
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()?)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Loading