forked from foundry-rs/starknet-foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support overriding
block id/tag/hash
with a named fork binding (f…
…oundry-rs#2475) <!-- Reference any GitHub issues resolved by this PR --> Closes foundry-rs#2450 ## Introduced changes <!-- A brief description of the changes --> - Added new fork config resolving option ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [ ] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
- Loading branch information
Showing
11 changed files
with
309 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,55 @@ | ||
use cairo_lang_macro::{Diagnostic, Severity}; | ||
use indoc::formatdoc; | ||
|
||
fn higher_severity(a: Severity, b: Severity) -> Severity { | ||
pub fn higher_severity(a: Severity, b: Severity) -> Severity { | ||
match (a, b) { | ||
(Severity::Warning, Severity::Warning) => Severity::Warning, | ||
_ => Severity::Error, | ||
} | ||
} | ||
pub fn format_error_message(variants: &[Diagnostic]) -> String { | ||
let formatted_variants: Vec<String> = variants | ||
.iter() | ||
.map(|variant| format!("- variant: {}", variant.message)) | ||
.collect(); | ||
|
||
pub fn branch( | ||
left: Result<String, Diagnostic>, | ||
right: impl Fn() -> Result<String, Diagnostic>, | ||
) -> Result<String, Diagnostic> { | ||
left.or_else(|error| { | ||
right().map_err(|next_error| Diagnostic { | ||
severity: higher_severity(error.severity, next_error.severity), | ||
message: formatdoc!( | ||
" | ||
Both options failed | ||
First variant: {} | ||
Second variant: {} | ||
Resolve at least one of them | ||
", | ||
error.message, | ||
next_error.message | ||
), | ||
}) | ||
}) | ||
formatdoc! {" | ||
All options failed | ||
{} | ||
Resolve at least one of them | ||
", formatted_variants.join("\n")} | ||
} | ||
|
||
/// The `branch` macro is used to evaluate multiple expressions and return the first successful result. | ||
/// If all expressions fail, it collects the error messages and returns a combined error. | ||
/// | ||
/// This macro is used instead of a function because it can perform lazy evaluation and has better readability. | ||
#[macro_export] | ||
macro_rules! branch { | ||
($($result:expr),+) => {{ | ||
let mut messages = Vec::new(); | ||
let mut result = None; | ||
|
||
$( | ||
if result.is_none() { | ||
match $result { | ||
Ok(val) => { | ||
result = Some(val); | ||
}, | ||
Err(err) => { | ||
messages.push(err); | ||
}, | ||
} | ||
} | ||
)+ | ||
|
||
if let Some(result) = result { | ||
Ok(result) | ||
} else { | ||
Err(Diagnostic { | ||
message: $crate::utils::format_error_message(&messages), | ||
severity: messages.into_iter().fold(Severity::Warning, |acc, diagnostic| $crate::utils::higher_severity(acc, diagnostic.severity)) | ||
}) | ||
} | ||
}}; | ||
} |
Oops, something went wrong.