euv-macros 0.49.4

Zero-overhead procedural macros for the cross-platform euv UI framework, providing view!, style!, var!, watch!, computed!, state!, #[store], #[component], #[event], emit!, on!, and #[euv].
Documentation
use super::*;

/// Computes the Levenshtein edit distance between two ASCII property names.
///
/// # Arguments
///
/// - `&str` - First string slice.
/// - `&str` - Second string slice.
///
/// # Returns
///
/// - `usize` - Minimum single-character edits required to transform `left_text` into `right_text`.
pub(crate) fn edit_distance(left_text: &str, right_text: &str) -> usize {
    let left_bytes: &[u8] = left_text.as_bytes();
    let right_bytes: &[u8] = right_text.as_bytes();
    let left_len: usize = left_bytes.len();
    let right_len: usize = right_bytes.len();
    if left_len == 0 {
        return right_len;
    }
    if right_len == 0 {
        return left_len;
    }
    let mut previous_row: Vec<usize> = (0..=right_len).collect();
    let mut current_row: Vec<usize> = vec![0; right_len + 1];
    for row_index in 1..=left_len {
        current_row[0] = row_index;
        for col_index in 1..=right_len {
            let substitution_cost: usize =
                if left_bytes[row_index - 1] == right_bytes[col_index - 1] {
                    0
                } else {
                    1
                };
            let deletion_cost: usize = previous_row[col_index] + 1;
            let insertion_cost: usize = current_row[col_index - 1] + 1;
            let replacement_cost: usize = previous_row[col_index - 1] + substitution_cost;
            current_row[col_index] = deletion_cost.min(insertion_cost).min(replacement_cost);
        }
        previous_row.copy_from_slice(&current_row);
    }
    previous_row[right_len]
}

/// Finds the closest known property candidate to `input_name` for friendly compiler suggestions.
///
/// # Arguments
///
/// - `&str` - Unknown identifier entered by the user.
/// - `&[&'a str]` - Exhaustive list of valid property names.
///
/// # Returns
///
/// - `Option<&'a str>` - Closest candidate within edit distance 2 (or prefix match), if any.
pub(crate) fn suggest_closest<'a>(input_name: &str, candidates: &[&'a str]) -> Option<&'a str> {
    let mut best_match: Option<(&'a str, usize)> = None;
    for candidate in candidates.iter().copied() {
        let distance: usize = edit_distance(input_name, candidate);
        let is_prefix: bool = input_name.len() >= 2
            && (candidate.starts_with(input_name) || input_name.starts_with(candidate));
        if distance <= 2 || is_prefix {
            match best_match {
                Some((_, best_distance)) if distance >= best_distance => {}
                _ => {
                    best_match = Some((candidate, distance));
                }
            }
        }
    }
    best_match.map(|(candidate_name, _): (&'a str, usize)| candidate_name)
}

/// Builds a span-accurate `syn::Error` for an unknown macro property with a typo suggestion and valid list.
///
/// # Arguments
///
/// - `&Ident` - Offending property identifier carrying its exact source span.
/// - `&str` - Macro context label (e.g. `"view! element \`row\`"` or `"style!"`).
/// - `&[&str]` - List of supported property names.
///
/// # Returns
///
/// - `Error` - Actionable compiler error pointing directly at `property_ident`.
pub(crate) fn unknown_prop_error(
    property_ident: &Ident,
    scope_label: &str,
    candidates: &[&str],
) -> Error {
    let property_name: String = property_ident.to_string();
    let supported_list: String = candidates.join(", ");
    let message: String = match suggest_closest(&property_name, candidates) {
        Some(suggestion) => format!(
            "unknown property `{property_name}` in {scope_label} — did you mean `{suggestion}`? Supported properties: {supported_list}"
        ),
        None => format!(
            "unknown property `{property_name}` in {scope_label}. Supported properties: {supported_list}"
        ),
    };
    Error::new(property_ident.span(), message)
}