Skip to content

Commit

Permalink
Migrate object.rs to snafu error handling (ordinals#3858)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoni9n authored Aug 7, 2024
1 parent 7682e91 commit c24d136
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
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
46 changes: 35 additions & 11 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,46 @@ pub enum Object {
}

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

fn from_str(s: &str) -> Result<Self> {
fn from_str(input: &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(input)
.snafu_context(error::UnrecognizedRepresentation { input })?
{
Address => Ok(Self::Address(
input.parse().snafu_context(error::AddressParse { input })?,
)),
Decimal | Degree | Percentile | Name => Ok(Self::Sat(
input.parse().snafu_context(error::SatParse { input })?,
)),
Hash => Ok(Self::Hash(
bitcoin::hashes::sha256::Hash::from_str(s)?.to_byte_array(),
bitcoin::hashes::sha256::Hash::from_str(input)
.snafu_context(error::HashParse { input })?
.to_byte_array(),
)),
InscriptionId => Ok(Self::InscriptionId(
input
.parse()
.snafu_context(error::InscriptionIdParse { input })?,
)),
Integer => Ok(Self::Integer(
input.parse().snafu_context(error::IntegerParse { input })?,
)),
OutPoint => Ok(Self::OutPoint(
input
.parse()
.snafu_context(error::OutPointParse { input })?,
)),
Rune => Ok(Self::Rune(
input.parse().snafu_context(error::RuneParse { input })?,
)),
SatPoint => Ok(Self::SatPoint(
input
.parse()
.snafu_context(error::SatPointParse { input })?,
)),
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();
}

0 comments on commit c24d136

Please sign in to comment.