//! Shared utility functions for reference name validation.

use crate::GitError;

/// # Rules
/// - Must not be empty
/// - Must not start with '.'
/// - Must not end with '/'
/// - Must not contain '..'
/// - Must not contain spaces, '~', '^', ':', '?', '*', '[', or '\'
///
/// # Returns
/// - `Ok(())` if name is valid
/// - `Err(GitError::InvalidRefName)` if name is invalid
pub fn validate_ref_name(name: &str) -> Result<(), GitError> {
    if name.is_empty()
        || name.starts_with('.')
        || name.ends_with('/')
        || name.contains("..")
        || name.contains(' ')
        || name.contains('~')
        || name.contains('^')
        || name.contains(':')
        || name.contains('?')
        || name.contains('*')
        || name.contains('[')
        || name.contains('\\')
    {
        return Err(GitError::InvalidRefName(format!(
            "invalid ref name: {}",
            name
        )));
    }
    Ok(())
}
