98 lines
3.1 KiB
Rust
98 lines
3.1 KiB
Rust
|
|
//! Phase 170-ultrathink: Error Message Utilities
|
||
|
|
//!
|
||
|
|
//! Common error message formatting functions for loop pattern validation.
|
||
|
|
//! This module centralizes error messages to ensure consistency across
|
||
|
|
//! Pattern 2, Pattern 4, and future patterns.
|
||
|
|
|
||
|
|
use super::loop_condition_scope::CondVarScope;
|
||
|
|
|
||
|
|
/// Format an error message for unsupported loop-body-local variables in conditions
|
||
|
|
///
|
||
|
|
/// # Arguments
|
||
|
|
///
|
||
|
|
/// * `pattern_name` - Name of the pattern (e.g., "pattern2", "pattern4")
|
||
|
|
/// * `body_local_names` - Names of loop-body-local variables found in condition
|
||
|
|
///
|
||
|
|
/// # Returns
|
||
|
|
///
|
||
|
|
/// Formatted error string with:
|
||
|
|
/// - Clear identification of the problem
|
||
|
|
/// - List of problematic variable names
|
||
|
|
/// - Explanation of what is supported
|
||
|
|
/// - Suggestion for alternative patterns
|
||
|
|
///
|
||
|
|
/// # Example
|
||
|
|
///
|
||
|
|
/// ```
|
||
|
|
/// let err = format_unsupported_condition_error(
|
||
|
|
/// "pattern2",
|
||
|
|
/// &["ch", "temp"]
|
||
|
|
/// );
|
||
|
|
/// // Returns:
|
||
|
|
/// // "[joinir/pattern2] Unsupported condition: uses loop-body-local variables: ["ch", "temp"].
|
||
|
|
/// // Pattern 2 supports only loop parameters and outer-scope variables.
|
||
|
|
/// // Consider using Pattern 5+ for complex loop conditions."
|
||
|
|
/// ```
|
||
|
|
pub fn format_unsupported_condition_error(
|
||
|
|
pattern_name: &str,
|
||
|
|
body_local_names: &[&String],
|
||
|
|
) -> String {
|
||
|
|
format!(
|
||
|
|
"[joinir/{}] Unsupported condition: uses loop-body-local variables: {:?}. \
|
||
|
|
Pattern {} supports only loop parameters and outer-scope variables. \
|
||
|
|
Consider using Pattern 5+ for complex loop conditions.",
|
||
|
|
pattern_name,
|
||
|
|
body_local_names,
|
||
|
|
pattern_name.chars().filter(|c| c.is_numeric()).collect::<String>()
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Extract loop-body-local variable names from a LoopConditionScope
|
||
|
|
///
|
||
|
|
/// Helper function to filter variables by LoopBodyLocal scope.
|
||
|
|
///
|
||
|
|
/// # Arguments
|
||
|
|
///
|
||
|
|
/// * `vars` - Slice of CondVarInfo to filter
|
||
|
|
///
|
||
|
|
/// # Returns
|
||
|
|
///
|
||
|
|
/// Vector of references to variable names that are LoopBodyLocal
|
||
|
|
pub fn extract_body_local_names(vars: &[super::loop_condition_scope::CondVarInfo]) -> Vec<&String> {
|
||
|
|
vars.iter()
|
||
|
|
.filter(|v| v.scope == CondVarScope::LoopBodyLocal)
|
||
|
|
.map(|v| &v.name)
|
||
|
|
.collect()
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_format_error_pattern2() {
|
||
|
|
let names = vec!["ch".to_string(), "temp".to_string()];
|
||
|
|
let refs: Vec<&String> = names.iter().collect();
|
||
|
|
let err = format_unsupported_condition_error("pattern2", &refs);
|
||
|
|
|
||
|
|
assert!(err.contains("[joinir/pattern2]"));
|
||
|
|
assert!(err.contains("loop-body-local variables"));
|
||
|
|
assert!(err.contains("ch"));
|
||
|
|
assert!(err.contains("temp"));
|
||
|
|
assert!(err.contains("Pattern 2 supports"));
|
||
|
|
assert!(err.contains("Pattern 5+"));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_format_error_pattern4() {
|
||
|
|
let names = vec!["x".to_string()];
|
||
|
|
let refs: Vec<&String> = names.iter().collect();
|
||
|
|
let err = format_unsupported_condition_error("pattern4", &refs);
|
||
|
|
|
||
|
|
assert!(err.contains("[joinir/pattern4]"));
|
||
|
|
assert!(err.contains("loop-body-local variables"));
|
||
|
|
assert!(err.contains("x"));
|
||
|
|
assert!(err.contains("Pattern 4 supports"));
|
||
|
|
}
|
||
|
|
}
|