use crate::ConfigEasyError;

/// Validates that a given string is a valid SQL identifier, which can be used for table or column names in the database.
/// A valid identifier must start with a letter (A-Z, a-z) or an underscore, and can only contain letters, digits (0-9), or underscores.
///
/// # Arguments
/// * `identifier` - The string to validate as a SQL identifier.
///
/// # Returns
/// A `Result` indicating whether the identifier is valid or if an error occurred.  
pub(crate) fn validate_identifier(identifier: &str) -> Result<(), ConfigEasyError> {
    let mut chars = identifier.chars();

    match chars.next() {
        Some(first) if first.is_ascii_alphabetic() || first == '_' => {}
        _ => return Err(ConfigEasyError::InvalidIdentifier(identifier.to_string())),
    }

    if chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_') {
        Ok(())
    } else {
        Err(ConfigEasyError::InvalidIdentifier(identifier.to_string()))
    }
}

#[cfg(test)]
mod tests;
