Skip to content

Commit

Permalink
feat: Add NamedError
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Mar 4, 2025
1 parent 5e2359b commit 7b6a09f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 111 deletions.
47 changes: 28 additions & 19 deletions crates/miden-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use regex::Regex;
use walkdir::WalkDir;

/// A map where the key is the error name and the value is the error code with the message.
type ErrorCategoryMap = BTreeMap<ErrorCategory, Vec<(ErrorName, ExtractedError)>>;
type ErrorCategoryMap = BTreeMap<ErrorCategory, Vec<NamedError>>;

// CONSTANTS
// ================================================================================================
Expand Down Expand Up @@ -489,7 +489,7 @@ fn generate_error_constants(asm_source_dir: &Path) -> Result<()> {

for (category, mut errors) in categories {
// Sort by error code.
errors.sort_by_key(|(_, error)| error.code.clone());
errors.sort_by_key(|error| error.code);

// Generate the errors file.
let error_file_content = generate_error_file_content(category, errors)?;
Expand Down Expand Up @@ -526,7 +526,7 @@ fn extract_all_masm_errors(asm_source_dir: &Path) -> Result<ErrorCategoryMap> {
error_codes.insert(error.code.clone(), error_name);
}

let mut category_map: BTreeMap<ErrorCategory, Vec<(String, ExtractedError)>> = BTreeMap::new();
let mut category_map: BTreeMap<ErrorCategory, Vec<NamedError>> = BTreeMap::new();
for (error_name, error) in errors.into_iter() {
let error_num = u32::from_str_radix(&error.code, 16)
.into_diagnostic()
Expand All @@ -541,12 +541,13 @@ fn extract_all_masm_errors(asm_source_dir: &Path) -> Result<ErrorCategoryMap> {

validate_error_category(*category, error_num, &error_name)?;

category_map
.entry(*category)
.and_modify(|entry| {
entry.push((error_name.clone(), error.clone()));
})
.or_insert_with(|| vec![(error_name, error)]);
let named_error = NamedError {
name: error_name,
code: error_num,
message: error.message,
};

category_map.entry(*category).or_default().push(named_error);
}

Ok(category_map)
Expand Down Expand Up @@ -643,10 +644,7 @@ fn is_new_error_category<'a>(last_error: &mut Option<&'a str>, current_error: &'
}

/// Generates the content of an error file for the given category and the set of errors.
fn generate_error_file_content(
category: ErrorCategory,
errors: Vec<(ErrorName, ExtractedError)>,
) -> Result<String> {
fn generate_error_file_content(category: ErrorCategory, errors: Vec<NamedError>) -> Result<String> {
let mut output = String::new();

writeln!(
Expand Down Expand Up @@ -675,14 +673,16 @@ fn generate_error_file_content(
.unwrap();

let mut last_error = None;
for (error_name, ExtractedError { code, message }) in errors.iter() {
for named_error in errors.iter() {
let NamedError { name, code, message } = named_error;

// Group errors into blocks separate by newlines.
if is_new_error_category(&mut last_error, error_name) {
if is_new_error_category(&mut last_error, name) {
writeln!(output).into_diagnostic()?;
}

writeln!(output, "/// {message}").into_diagnostic()?;
writeln!(output, "pub const ERR_{error_name}: u32 = 0x{code};").into_diagnostic()?;
writeln!(output, "pub const ERR_{name}: u32 = 0x{code:x};").into_diagnostic()?;
}
writeln!(output).into_diagnostic()?;

Expand All @@ -695,12 +695,14 @@ fn generate_error_file_content(
.into_diagnostic()?;

let mut last_error = None;
for (error_name, ExtractedError { message, .. }) in errors.iter() {
for named_error in errors.iter() {
let NamedError { name, message, .. } = named_error;

// Group errors into blocks separate by newlines.
if is_new_error_category(&mut last_error, error_name) {
if is_new_error_category(&mut last_error, name) {
writeln!(output).into_diagnostic()?;
}
writeln!(output, r#" (ERR_{error_name}, "{message}"),"#).into_diagnostic()?;
writeln!(output, r#" (ERR_{name}, "{message}"),"#).into_diagnostic()?;
}

writeln!(output, "];").into_diagnostic()?;
Expand All @@ -716,6 +718,13 @@ struct ExtractedError {
message: String,
}

#[derive(Debug, Clone)]
struct NamedError {
name: ErrorName,
code: u32,
message: String,
}

// Later we can extend this with:
// batch kernel: 0x2_4000..0x2_8000
// block kernel: 0x2_8000..0x2_c000
Expand Down
14 changes: 7 additions & 7 deletions crates/miden-lib/src/errors/note_script_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
// ================================================================================================

/// P2ID script expects exactly 2 note inputs
pub const ERR_P2ID_WRONG_NUMBER_OF_INPUTS: u32 = 0x0002c000;
pub const ERR_P2ID_WRONG_NUMBER_OF_INPUTS: u32 = 0x2c000;
/// P2ID's target account address and transaction address do not match
pub const ERR_P2ID_TARGET_ACCT_MISMATCH: u32 = 0x0002c001;
pub const ERR_P2ID_TARGET_ACCT_MISMATCH: u32 = 0x2c001;

/// P2IDR scripts expect exactly 3 note inputs
pub const ERR_P2IDR_WRONG_NUMBER_OF_INPUTS: u32 = 0x0002c002;
pub const ERR_P2IDR_WRONG_NUMBER_OF_INPUTS: u32 = 0x2c002;
/// P2IDR's reclaimer is not the original sender
pub const ERR_P2IDR_RECLAIM_ACCT_IS_NOT_SENDER: u32 = 0x0002c003;
pub const ERR_P2IDR_RECLAIM_ACCT_IS_NOT_SENDER: u32 = 0x2c003;
/// P2IDR can not be reclaimed as the transaction's reference block is lower than the reclaim height
pub const ERR_P2IDR_RECLAIM_HEIGHT_NOT_REACHED: u32 = 0x0002c004;
pub const ERR_P2IDR_RECLAIM_HEIGHT_NOT_REACHED: u32 = 0x2c004;

/// SWAP script expects exactly 10 note inputs
pub const ERR_SWAP_WRONG_NUMBER_OF_INPUTS: u32 = 0x0002c005;
pub const ERR_SWAP_WRONG_NUMBER_OF_INPUTS: u32 = 0x2c005;
/// SWAP script requires exactly 1 note asset
pub const ERR_SWAP_WRONG_NUMBER_OF_ASSETS: u32 = 0x0002c006;
pub const ERR_SWAP_WRONG_NUMBER_OF_ASSETS: u32 = 0x2c006;

pub const NOTE_SCRIPT_ERRORS: [(u32, &str); 7] = [
(ERR_P2ID_WRONG_NUMBER_OF_INPUTS, "P2ID script expects exactly 2 note inputs"),
Expand Down
Loading

0 comments on commit 7b6a09f

Please sign in to comment.