Skip to content

Use anyhow::Error in place of String #130

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
186 changes: 184 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ miniscript = "12.3.1"
either = "1.12.0"
itertools = "0.13.0"
arbitrary = { version = "1", optional = true, features = ["derive"] }
clap = "4.5.37"
anyhow = "1.0.98"

[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand Down
2 changes: 0 additions & 2 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
msrv = "1.78.0"
# We have an error type, `RichError`, of size 144. This is pushing it but probably fine.
large-error-threshold = 145
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<T> WithFile<T> for Result<T, RichError> {
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct RichError {
/// The error that occurred.
error: Error,
error: Box<Error>,
/// Area that the error spans inside the file.
span: Span,
/// File in which the error occurred.
Expand All @@ -208,7 +208,7 @@ impl RichError {
/// Create a new error with context.
pub fn new(error: Error, span: Span) -> RichError {
RichError {
error,
error: Box::new(error),
span,
file: None,
}
Expand Down Expand Up @@ -266,7 +266,7 @@ impl std::error::Error for RichError {}

impl From<RichError> for Error {
fn from(error: RichError) -> Self {
error.error
*error.error
}
}

Expand Down
26 changes: 12 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TemplateProgram {
/// ## Errors
///
/// The string is not a valid Simfony program.
pub fn new<Str: Into<Arc<str>>>(s: Str) -> Result<Self, String> {
pub fn new<Str: Into<Arc<str>>>(s: Str) -> anyhow::Result<Self> {
let file = s.into();
let parse_program = parse::Program::parse_from_str(&file)?;
let ast_program = ast::Program::analyze(&parse_program).with_file(Arc::clone(&file))?;
Expand All @@ -76,10 +76,9 @@ impl TemplateProgram {
&self,
arguments: Arguments,
include_debug_symbols: bool,
) -> Result<CompiledProgram, String> {
) -> anyhow::Result<CompiledProgram> {
arguments
.is_consistent(self.simfony.parameters())
.map_err(|error| error.to_string())?;
.is_consistent(self.simfony.parameters())?;
Ok(CompiledProgram {
debug_symbols: self.simfony.debug_symbols(self.file.as_ref()),
simplicity: self
Expand Down Expand Up @@ -121,7 +120,7 @@ impl CompiledProgram {
s: Str,
arguments: Arguments,
include_debug_symbols: bool,
) -> Result<Self, String> {
) -> anyhow::Result<Self> {
TemplateProgram::new(s)
.and_then(|template| template.instantiate(arguments, include_debug_symbols))
}
Expand All @@ -142,7 +141,7 @@ impl CompiledProgram {
///
/// - Witness values have a different type than declared in the Simfony program.
/// - There are missing witness values.
pub fn satisfy(&self, witness_values: WitnessValues) -> Result<SatisfiedProgram, String> {
pub fn satisfy(&self, witness_values: WitnessValues) -> anyhow::Result<SatisfiedProgram> {
self.satisfy_with_env(witness_values, None)
}

Expand All @@ -157,17 +156,16 @@ impl CompiledProgram {
&self,
witness_values: WitnessValues,
env: Option<&ElementsEnv<Arc<elements::Transaction>>>,
) -> Result<SatisfiedProgram, String> {
) -> anyhow::Result<SatisfiedProgram> {
witness_values
.is_consistent(&self.witness_types)
.map_err(|e| e.to_string())?;
.is_consistent(&self.witness_types)?;
let simplicity_witness = named::to_witness_node(&self.simplicity, witness_values);
let simplicity_redeem = match env {
Some(env) => simplicity_witness.finalize_pruned(env),
None => simplicity_witness.finalize_unpruned(),
Some(env) => simplicity_witness.finalize_pruned(env)?,
None => simplicity_witness.finalize_unpruned()?,
};
Ok(SatisfiedProgram {
simplicity: simplicity_redeem.map_err(|e| e.to_string())?,
simplicity: simplicity_redeem,
debug_symbols: self.debug_symbols.clone(),
})
}
Expand All @@ -193,7 +191,7 @@ impl SatisfiedProgram {
arguments: Arguments,
witness_values: WitnessValues,
include_debug_symbols: bool,
) -> Result<Self, String> {
) -> anyhow::Result<Self> {
let compiled = CompiledProgram::new(s, arguments, include_debug_symbols)?;
compiled.satisfy(witness_values)
}
Expand Down Expand Up @@ -620,7 +618,7 @@ fn main() {
) {
Ok(_) => panic!("Accepted faulty program"),
Err(error) => {
if !error.contains("Expected expression of type `bool`, found type `()`") {
if !error.to_string().contains("Expected expression of type `bool`, found type `()`") {
panic!("Unexpected error: {error}")
}
}
Expand Down
Loading
Loading