Skip to main content

CompileErrorProvider

Trait CompileErrorProvider 

Source
pub trait CompileErrorProvider {
    // Required methods
    fn no_errors(&self) -> bool;
    fn error_data(&mut self) -> Vec<String>;
}
Available on crate feature _helpers only.
Expand description

Helper utilities for building procedural macros Collect and provide error information for expr_error_wrap.

Types implementing this trait can accumulate error messages and then show errors at the problematic code block by using expr_error_wrap.

§Examples

§Basic Usage with Vec<String>

let mut errors = Vec::<String>::new();
errors.push("Invalid syntax".to_string());
errors.push("Missing required field".to_string());

assert!(!errors.no_errors());
let error_messages = errors.error_data();
assert!(errors.no_errors());

§Custom Implementation

#[derive(Default)]
struct ValidationErrors {
    errors: Vec<String>,
    other_data: String,
}

impl ValidationErrors {
    fn add_error(&mut self, msg: &str) {
        self.errors.push(msg.to_string());
    }
}

impl CompileErrorProvider for ValidationErrors {
    fn no_errors(&self) -> bool {
        self.errors.no_errors()
    }

    fn error_data(&mut self) -> Vec<String> {
        self.errors.error_data()
    }
}

let mut validator = ValidationErrors::default();
validator.add_error("Field 'name' is required");
validator.add_error("Field 'age' must be a positive number");

assert!(!validator.no_errors());
let messages = validator.error_data();
assert_eq!(messages.len(), 2);
assert!(validator.no_errors());

Required Methods§

Source

fn no_errors(&self) -> bool

Returns true if there are no errors, false otherwise.

This method is used to check whether error wrapping is needed.

Source

fn error_data(&mut self) -> Vec<String>

Removes and returns all error data from the collection.

After calling this method, the error collection should be empty. The returned vector contains all accumulated error messages.

§Note

Vec<String> implements this trait, so you can use it directly for simple error collection.

§Returns

A vector of error messages that were accumulated

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl CompileErrorProvider for Vec<String>

Source§

fn no_errors(&self) -> bool

Source§

fn error_data(&mut self) -> Vec<String>

Implementors§