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 all 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
17 changes: 15 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))]
Expand Down
4 changes: 1 addition & 3 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ impl FromStr for Object {
fn from_str(input: &str) -> Result<Self, Self::Err> {
use Representation::*;

match Representation::from_str(input)
.snafu_context(error::UnrecognizedRepresentation { input })?
{
match input.parse::<Representation>()? {
Address => Ok(Self::Address(
input.parse().snafu_context(error::AddressParse { input })?,
)),
Expand Down
51 changes: 34 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,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::<Decimal>()
.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(),
})
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ impl Representation {
}

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

fn from_str(s: &str) -> Result<Self> {
if let Some(i) = REGEX_SET.matches(s).into_iter().next() {
fn from_str(input: &str) -> Result<Self, Self::Err> {
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())
}
}
}
Expand Down
Loading